67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
|
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 Group) ToStorageType() storage.Group {
|
||
|
group := storage.Group{
|
||
|
ID: g.Id,
|
||
|
Namespace: g.Namespace,
|
||
|
Members: g.Members,
|
||
|
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 GroupFromStorageType(group *storage.Group) (*Group, error) {
|
||
|
|
||
|
d, err := sanitizeData(group.Data)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
data, err := structpb.NewStruct(d)
|
||
|
if err != nil {
|
||
|
fmt.Println(err)
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return &Group{
|
||
|
Id: group.ID,
|
||
|
Namespace: group.Namespace,
|
||
|
Members: group.Members,
|
||
|
Data: data,
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
|
func sanitizeData(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
|
||
|
}
|