74 lines
1.4 KiB
Go
74 lines
1.4 KiB
Go
package grpcapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"git.coopgo.io/coopgo-platform/diags/storage"
|
|
"github.com/rs/zerolog/log"
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
"google.golang.org/protobuf/types/known/structpb"
|
|
)
|
|
|
|
|
|
func (e Diag) ToStorageType() storage.Diag {
|
|
diag := storage.Diag{
|
|
ID: e.Id,
|
|
Name: e.Name,
|
|
Namespace: e.Namespace,
|
|
Json_schema: e.JsonSchema,
|
|
Ui_schema: e.UiSchema,
|
|
Data: map[string]any{},
|
|
Deleted: e.Deleted,
|
|
}
|
|
|
|
for k, d := range e.Data.GetFields() {
|
|
jsondata, err := protojson.Marshal(d)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("")
|
|
break
|
|
}
|
|
var data any
|
|
json.Unmarshal(jsondata, &data)
|
|
diag.Data[k] = data
|
|
}
|
|
|
|
return diag
|
|
}
|
|
|
|
func DiagFromStorageType(diag *storage.Diag) (*Diag, error) {
|
|
|
|
d, err := sanitizeData(diag.Data)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
data, err := structpb.NewStruct(d)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("")
|
|
return nil, err
|
|
}
|
|
|
|
return &Diag{
|
|
Id: diag.ID,
|
|
Name: diag.Name,
|
|
Namespace: diag.Namespace,
|
|
JsonSchema: diag.Json_schema,
|
|
UiSchema: diag.Ui_schema,
|
|
Data: data,
|
|
Deleted: diag.Deleted,
|
|
}, 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
|
|
}
|