groups-management/grpcapi/groupsmember.go

67 lines
1.2 KiB
Go
Raw Permalink Normal View History

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{
ID: g.Id,
Memberid: g.Memberid,
Groupid: g.Groupid,
Data: map[string]any{},
}
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{
Id: group.ID,
Memberid: group.Memberid,
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
}