2020-03-23 07:01:59 +01:00
|
|
|
package proto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/jsonpb"
|
|
|
|
pb_struct "github.com/golang/protobuf/ptypes/struct"
|
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/logging"
|
2020-03-23 07:01:59 +01:00
|
|
|
)
|
|
|
|
|
2020-03-30 16:53:29 +02:00
|
|
|
var (
|
|
|
|
marshaller = new(jsonpb.Marshaler)
|
|
|
|
)
|
|
|
|
|
2020-03-23 07:01:59 +01:00
|
|
|
func MustToPBStruct(object interface{}) *pb_struct.Struct {
|
|
|
|
s, err := ToPBStruct(object)
|
|
|
|
logging.Log("PROTO-7Aa3t").OnError(err).Panic("unable to map object to pb-struct")
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func BytesToPBStruct(b []byte) (*pb_struct.Struct, error) {
|
|
|
|
fields := new(pb_struct.Struct)
|
|
|
|
err := jsonpb.Unmarshal(bytes.NewReader(b), fields)
|
|
|
|
return fields, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func ToPBStruct(object interface{}) (*pb_struct.Struct, error) {
|
|
|
|
marshalled, err := json.Marshal(object)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-30 07:23:43 +02:00
|
|
|
fields := new(pb_struct.Struct)
|
2020-03-23 07:01:59 +01:00
|
|
|
err = jsonpb.Unmarshal(bytes.NewReader(marshalled), fields)
|
|
|
|
return fields, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func MustFromPBStruct(object interface{}, s *pb_struct.Struct) {
|
|
|
|
err := FromPBStruct(object, s)
|
|
|
|
logging.Log("PROTO-WeMYY").OnError(err).Panic("unable to map pb-struct into object")
|
|
|
|
}
|
|
|
|
|
|
|
|
func FromPBStruct(object interface{}, s *pb_struct.Struct) error {
|
|
|
|
jsonString, err := marshaller.MarshalToString(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return json.Unmarshal([]byte(jsonString), object)
|
|
|
|
}
|