mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-14 12:48:01 +00:00
feat: add some api packages
This commit is contained in:
52
internal/proto/struct.go
Normal file
52
internal/proto/struct.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package proto
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/golang/protobuf/jsonpb"
|
||||
pb_struct "github.com/golang/protobuf/ptypes/struct"
|
||||
|
||||
"github.com/caos/logging"
|
||||
)
|
||||
|
||||
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) {
|
||||
fields := new(pb_struct.Struct)
|
||||
|
||||
marshalled, err := json.Marshal(object)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
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 {
|
||||
marshaller := new(jsonpb.Marshaler)
|
||||
jsonString, err := marshaller.MarshalToString(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return json.Unmarshal([]byte(jsonString), object)
|
||||
}
|
53
internal/proto/struct_test.go
Normal file
53
internal/proto/struct_test.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package proto
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
pb_struct "github.com/golang/protobuf/ptypes/struct"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestToPBStruct(t *testing.T) {
|
||||
obj := struct {
|
||||
ID string
|
||||
Name string
|
||||
Seq uint64
|
||||
}{
|
||||
ID: "asdf",
|
||||
Name: "ueli",
|
||||
Seq: 208582075,
|
||||
}
|
||||
fields, err := ToPBStruct(&obj)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, fields.Fields, 3)
|
||||
|
||||
assert.Equal(t, obj.ID, fields.Fields["ID"].GetStringValue())
|
||||
assert.Equal(t, int(obj.Seq), int(fields.Fields["Seq"].GetNumberValue()))
|
||||
assert.Equal(t, obj.Name, fields.Fields["Name"].GetStringValue())
|
||||
}
|
||||
|
||||
func TestFromPBStruct(t *testing.T) {
|
||||
name := "ueli"
|
||||
id := "asdf"
|
||||
seq := float64(208582075)
|
||||
s := &pb_struct.Struct{Fields: map[string]*pb_struct.Value{
|
||||
"ID": &pb_struct.Value{Kind: &pb_struct.Value_StringValue{StringValue: id}},
|
||||
"Name": &pb_struct.Value{Kind: &pb_struct.Value_StringValue{StringValue: name}},
|
||||
"Seq": &pb_struct.Value{Kind: &pb_struct.Value_NumberValue{NumberValue: seq}},
|
||||
}}
|
||||
|
||||
obj := struct {
|
||||
ID string
|
||||
Name string
|
||||
Seq uint64
|
||||
}{}
|
||||
|
||||
err := FromPBStruct(&obj, s)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, id, obj.ID)
|
||||
assert.Equal(t, name, obj.Name)
|
||||
assert.Equal(t, int(seq), int(obj.Seq))
|
||||
}
|
Reference in New Issue
Block a user