From 100a44103e32394a338e238e2fb5018e8133d8fc Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Mon, 5 Feb 2024 13:39:32 +0300 Subject: [PATCH] add ability to unmarshal to structpb.Value Signed-off-by: Vasiliy Tolstov --- go.mod | 5 ++--- go.sum | 3 ++- urlencode.go | 14 +++++++++++++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index e23c4d5..ce6da40 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,9 @@ module go.unistack.org/micro-codec-urlencode/v3 -go 1.19 +go 1.20 require ( go.unistack.org/micro-proto/v3 v3.3.1 go.unistack.org/micro/v3 v3.10.14 + google.golang.org/protobuf v1.32.0 ) - -require google.golang.org/protobuf v1.28.1 // indirect diff --git a/go.sum b/go.sum index ffac1b9..3a09de1 100644 --- a/go.sum +++ b/go.sum @@ -136,8 +136,9 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= +google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/urlencode.go b/urlencode.go index d20bdd4..2ccaa0e 100644 --- a/urlencode.go +++ b/urlencode.go @@ -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)) }