Skip to content

Commit

Permalink
update for latest micro
Browse files Browse the repository at this point in the history
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
  • Loading branch information
vtolstov committed Sep 21, 2021
1 parent dffa65f commit 7125b0c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/unistack-org/micro-codec-proto/v3
go 1.16

require (
github.com/unistack-org/micro/v3 v3.7.1
github.com/unistack-org/micro/v3 v3.7.3
google.golang.org/protobuf v1.27.1
)
9 changes: 2 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/ef-ds/deque v1.0.4/go.mod h1:gXDnTC3yqvBcHbq2lcExjtAcVrOnJCbMcZXmuj8Z4tg=
github.com/golang-jwt/jwt/v4 v4.0.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/unistack-org/micro-proto v0.0.9 h1:KrWLS4FUX7UAWNAilQf70uad6ZPf/0EudeddCXllRVc=
github.com/unistack-org/micro-proto v0.0.9/go.mod h1:Cckwmzd89gvS7ThxzZp9kQR/EOdksFQcsTAtDDyKwrg=
github.com/unistack-org/micro/v3 v3.7.1 h1:gjCon1U8i9upNgw9+iEgbZh2LCeBizDYotQ+THHV0lo=
github.com/unistack-org/micro/v3 v3.7.1/go.mod h1:gBoY6gvzeFiJTZ4FgDttGNSs4Y1+1PRg2cV1yTRMSlg=
github.com/unistack-org/micro/v3 v3.7.3 h1:7dCv7WuTYp47jZ/doVCXDNMnY75imprbmwSnmchTSJY=
github.com/unistack-org/micro/v3 v3.7.3/go.mod h1:78vy7ggElJD+ayx8ruhJsY+SkGUupfR32LXrg1UObvg=
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -27,4 +23,3 @@ google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+Rur
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
26 changes: 19 additions & 7 deletions proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,27 @@ import (
"google.golang.org/protobuf/proto"
)

type protoCodec struct{}
type protoCodec struct {
opts codec.Options
}

const (
flattenTag = "flatten"
)

func (c *protoCodec) Marshal(v interface{}) ([]byte, error) {
func (c *protoCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
switch m := v.(type) {
case nil:
return nil, nil
case *codec.Frame:
return m.Data, nil
case proto.Message:
if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil {
options := c.opts
for _, o := range opts {
o(&options)
}

if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
if nm, ok := nv.(proto.Message); ok {
m = nm
}
Expand All @@ -32,7 +39,7 @@ func (c *protoCodec) Marshal(v interface{}) ([]byte, error) {
return nil, codec.ErrInvalidMessage
}

func (c *protoCodec) Unmarshal(d []byte, v interface{}) error {
func (c *protoCodec) Unmarshal(d []byte, v interface{}, opts ...codec.Option) error {
if len(d) == 0 {
return nil
}
Expand All @@ -42,7 +49,12 @@ func (c *protoCodec) Unmarshal(d []byte, v interface{}) error {
case *codec.Frame:
m.Data = d
case proto.Message:
if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil {
options := c.opts
for _, o := range opts {
o(&options)
}

if nv, nerr := rutil.StructFieldByTag(v, options.TagName, flattenTag); nerr == nil {
if nm, ok := nv.(proto.Message); ok {
m = nm
}
Expand Down Expand Up @@ -89,6 +101,6 @@ func (c *protoCodec) String() string {
return "proto"
}

func NewCodec() codec.Codec {
return &protoCodec{}
func NewCodec(opts ...codec.Option) codec.Codec {
return &protoCodec{opts: codec.NewOptions(opts...)}
}

0 comments on commit 7125b0c

Please sign in to comment.