2022-12-19 08:14:31 +00:00
|
|
|
package grpcapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"git.coopgo.io/coopgo-platform/groups-management/storage"
|
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
|
|
structpb "google.golang.org/protobuf/types/known/structpb"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (g GroupMember) ToStorageType() storage.GroupMember {
|
|
|
|
group := storage.GroupMember{
|
2023-01-17 14:07:16 +00:00
|
|
|
ID: g.Id,
|
|
|
|
Memberid: g.Memberid,
|
|
|
|
Groupid: g.Groupid,
|
|
|
|
Data: map[string]any{},
|
2022-12-19 08:14:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, d := range g.Data.GetFields() {
|
|
|
|
jsondata, err := protojson.Marshal(d)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
var data any
|
|
|
|
json.Unmarshal(jsondata, &data)
|
|
|
|
group.Data[k] = data
|
|
|
|
}
|
|
|
|
|
|
|
|
return group
|
|
|
|
}
|
|
|
|
|
|
|
|
func GroupFromStorageTypeMember(group *storage.GroupMember) (*GroupMember, error) {
|
|
|
|
|
|
|
|
d, err := sanitizeDatta(group.Data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := structpb.NewStruct(d)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &GroupMember{
|
2023-01-17 14:07:16 +00:00
|
|
|
Id: group.ID,
|
|
|
|
Memberid: group.Memberid,
|
2022-12-19 08:14:31 +00:00
|
|
|
Groupid: group.Groupid,
|
|
|
|
Data: data,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sanitizeDatta(data map[string]any) (d map[string]any, err error) {
|
|
|
|
j, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = json.Unmarshal(j, &d); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return d, nil
|
|
|
|
}
|