add ability to unmarshal to structpb.Value

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2024-02-05 13:39:32 +03:00
parent fad6fdf178
commit 100a44103e
3 changed files with 17 additions and 5 deletions

View File

@@ -1,12 +1,14 @@
// Package urlencode provides a urlencode codec
package urlencode // import "go.unistack.org/micro-codec-urlencode/v3"
package urlencode
import (
"encoding/json"
"io"
pb "go.unistack.org/micro-proto/v3/codec"
"go.unistack.org/micro/v3/codec"
rutil "go.unistack.org/micro/v3/util/reflect"
"google.golang.org/protobuf/types/known/structpb"
)
type urlencodeCodec struct {
@@ -75,6 +77,16 @@ func (c *urlencodeCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option
return err
}
switch t := v.(type) {
case *structpb.Value:
buf, err := json.Marshal(mp)
if err == nil {
err = t.UnmarshalJSON(buf)
}
return err
}
return rutil.Merge(v, rutil.FlattenMap(mp), rutil.Tags([]string{"protobuf", "json", "xml", "yaml"}), rutil.SliceAppend(true))
}