From cf0e4f96f53c8c8cf563a0598985be8f9ad35138 Mon Sep 17 00:00:00 2001 From: Eitan Yarmush Date: Thu, 2 Jan 2020 21:34:45 -0500 Subject: [PATCH] fix oneof difference (#2) * fix oneof difference * accidental line * remove ambiguity from test --- templates/go/functions.go | 32 ++++--- templates/go/msg.go | 6 +- templates/templates.go | 7 -- tests/api/hello.pb.go | 192 +++++++++++++++++++++++++++---------- tests/api/hello.pb.hash.go | 76 +++++++++++++++ tests/api/hello.proto | 9 ++ tests/equal_test.go | 9 -- tests/hash_test.go | 19 ++++ 8 files changed, 269 insertions(+), 81 deletions(-) delete mode 100644 tests/equal_test.go diff --git a/templates/go/functions.go b/templates/go/functions.go index d15b12c..fe01f94 100644 --- a/templates/go/functions.go +++ b/templates/go/functions.go @@ -18,19 +18,20 @@ func register(tpl *template.Template, params pgs.Parameters) { } tpl.Funcs(map[string]interface{}{ - "snakeCase": fns.snakeCase, - "cmt": pgs.C80, - "isBytes": fns.isBytes, - "lit": fns.lit, - "lookup": fns.lookup, - "msgTyp": fns.msgTyp, - "name": fns.Name, - "oneof": fns.oneofTypeName, - "pkg": fns.PackageName, - "typ": fns.Type, - "externalEnums": fns.externalEnums, - "enumPackages": fns.enumPackages, - "render": fns.render, + "fullPackageName": fns.fullPackageName, + "snakeCase": fns.snakeCase, + "cmt": pgs.C80, + "isBytes": fns.isBytes, + "lit": fns.lit, + "lookup": fns.lookup, + "msgTyp": fns.msgTyp, + "name": fns.Name, + "oneof": fns.oneofTypeName, + "pkg": fns.PackageName, + "typ": fns.Type, + "externalEnums": fns.externalEnums, + "enumPackages": fns.enumPackages, + "render": fns.render, }) } @@ -47,6 +48,10 @@ func (fns goSharedFuncs) pointerAccessor(field pgs.Field) string { return fmt.Sprintf("&m.%s", fns.Name(field)) } +func (fns goSharedFuncs) fullPackageName(msg pgs.Message) string { + return fmt.Sprintf("%s.%s.%s", msg.Package().ProtoName(), fns.ImportPath(msg), fns.Name(msg)) +} + func (fns goSharedFuncs) lookup(f pgs.Field, name string) string { return fmt.Sprintf( "_%s_%s_%s", @@ -102,7 +107,6 @@ func (fns goSharedFuncs) oneofTypeName(f pgs.Field) pgsgo.TypeName { return pgsgo.TypeName(fns.OneofOption(f)).Pointer() } - func (fns goSharedFuncs) msgTyp(message pgs.Message) pgsgo.TypeName { return pgsgo.TypeName(fns.Name(message)) } diff --git a/templates/go/msg.go b/templates/go/msg.go index bde4c50..a3c7700 100644 --- a/templates/go/msg.go +++ b/templates/go/msg.go @@ -5,9 +5,11 @@ const msgTpl = ` func (m {{ (msgTyp .).Pointer }}) Hash(hasher hash.Hash64) (uint64, error) { if m == nil { return 0, nil } if hasher == nil { hasher = fnv.New64() } - {{- if not (eq (len .Fields) 0) }} var err error - {{- end }} + if _, err = hasher.Write([]byte("{{ fullPackageName . }}")); err != nil { + return 0, err + } + {{ range .NonOneOfFields }} {{ render . }} {{ end }} diff --git a/templates/templates.go b/templates/templates.go index 1528141..e6e63d0 100644 --- a/templates/templates.go +++ b/templates/templates.go @@ -14,19 +14,12 @@ type FilePathFn func(f pgs.File, ctx pgsgo.Context, tpl *template.Template) *pgs func Template(params pgs.Parameters) *template.Template { tpl := template.New("go") - // shared.RegisterFunctions(tpl, params) golang.Register(tpl, params) return tpl } func FilePathFor(tpl *template.Template, moduleName string) FilePathFn { switch tpl.Name() { - // case "h": - // return cc.CcFilePath - // case "cc": - // return cc.CcFilePath - // case "java": - // return java.JavaFilePath default: return func(f pgs.File, ctx pgsgo.Context, tpl *template.Template) *pgs.FilePath { out := ctx.OutputPath(f) diff --git a/tests/api/hello.pb.go b/tests/api/hello.pb.go index 6e41adb..6554ed5 100644 --- a/tests/api/hello.pb.go +++ b/tests/api/hello.pb.go @@ -218,15 +218,19 @@ func (m *Simple) GetIntSkipped() uint32 { } type Nested struct { - Simple *Simple `protobuf:"bytes,1,opt,name=simple,proto3" json:"simple,omitempty"` - Details *_struct.Struct `protobuf:"bytes,5,opt,name=details,proto3" json:"details,omitempty"` - Test Test `protobuf:"varint,2,opt,name=test,proto3,enum=envoy.type.Test" json:"test,omitempty"` - Empty *Empty `protobuf:"bytes,3,opt,name=empty,proto3" json:"empty,omitempty"` - Hello []string `protobuf:"bytes,4,rep,name=hello,proto3" json:"hello,omitempty"` - Skipper *Simple `protobuf:"bytes,6,opt,name=skipper,proto3" json:"skipper,omitempty"` - X []*Simple `protobuf:"bytes,7,rep,name=x,proto3" json:"x,omitempty"` - Initial map[string]*Simple `protobuf:"bytes,9,rep,name=initial,proto3" json:"initial,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` - EmptyNonNullable *Empty `protobuf:"bytes,10,opt,name=emptyNonNullable,proto3" json:"emptyNonNullable,omitempty"` + Simple *Simple `protobuf:"bytes,1,opt,name=simple,proto3" json:"simple,omitempty"` + Details *_struct.Struct `protobuf:"bytes,5,opt,name=details,proto3" json:"details,omitempty"` + Test Test `protobuf:"varint,2,opt,name=test,proto3,enum=envoy.type.Test" json:"test,omitempty"` + Empty *Empty `protobuf:"bytes,3,opt,name=empty,proto3" json:"empty,omitempty"` + Hello []string `protobuf:"bytes,4,rep,name=hello,proto3" json:"hello,omitempty"` + Skipper *Simple `protobuf:"bytes,6,opt,name=skipper,proto3" json:"skipper,omitempty"` + X []*Simple `protobuf:"bytes,7,rep,name=x,proto3" json:"x,omitempty"` + Initial map[string]*Simple `protobuf:"bytes,9,rep,name=initial,proto3" json:"initial,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` + EmptyNonNullable *Empty `protobuf:"bytes,10,opt,name=emptyNonNullable,proto3" json:"emptyNonNullable,omitempty"` + // Types that are valid to be assigned to TestOneOf: + // *Nested_EmptyOneOf + // *Nested_NestedOneOf + TestOneOf isNested_TestOneOf `protobuf_oneof:"test_one_of"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` @@ -320,6 +324,51 @@ func (m *Nested) GetEmptyNonNullable() *Empty { return nil } +type isNested_TestOneOf interface { + isNested_TestOneOf() +} + +type Nested_EmptyOneOf struct { + EmptyOneOf *Empty `protobuf:"bytes,11,opt,name=empty_one_of,json=emptyOneOf,proto3,oneof"` +} + +type Nested_NestedOneOf struct { + NestedOneOf *NestedEmpty `protobuf:"bytes,12,opt,name=nested_one_of,json=nestedOneOf,proto3,oneof"` +} + +func (*Nested_EmptyOneOf) isNested_TestOneOf() {} + +func (*Nested_NestedOneOf) isNested_TestOneOf() {} + +func (m *Nested) GetTestOneOf() isNested_TestOneOf { + if m != nil { + return m.TestOneOf + } + return nil +} + +func (m *Nested) GetEmptyOneOf() *Empty { + if x, ok := m.GetTestOneOf().(*Nested_EmptyOneOf); ok { + return x.EmptyOneOf + } + return nil +} + +func (m *Nested) GetNestedOneOf() *NestedEmpty { + if x, ok := m.GetTestOneOf().(*Nested_NestedOneOf); ok { + return x.NestedOneOf + } + return nil +} + +// XXX_OneofWrappers is for the internal use of the proto package. +func (*Nested) XXX_OneofWrappers() []interface{} { + return []interface{}{ + (*Nested_EmptyOneOf)(nil), + (*Nested_NestedOneOf)(nil), + } +} + type Empty struct { XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` @@ -351,55 +400,100 @@ func (m *Empty) XXX_DiscardUnknown() { var xxx_messageInfo_Empty proto.InternalMessageInfo +type NestedEmpty struct { + Nested *Nested `protobuf:"bytes,1,opt,name=nested,proto3" json:"nested,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *NestedEmpty) Reset() { *m = NestedEmpty{} } +func (m *NestedEmpty) String() string { return proto.CompactTextString(m) } +func (*NestedEmpty) ProtoMessage() {} +func (*NestedEmpty) Descriptor() ([]byte, []int) { + return fileDescriptor_1e23aa1f3bc52fd3, []int{3} +} + +func (m *NestedEmpty) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_NestedEmpty.Unmarshal(m, b) +} +func (m *NestedEmpty) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_NestedEmpty.Marshal(b, m, deterministic) +} +func (m *NestedEmpty) XXX_Merge(src proto.Message) { + xxx_messageInfo_NestedEmpty.Merge(m, src) +} +func (m *NestedEmpty) XXX_Size() int { + return xxx_messageInfo_NestedEmpty.Size(m) +} +func (m *NestedEmpty) XXX_DiscardUnknown() { + xxx_messageInfo_NestedEmpty.DiscardUnknown(m) +} + +var xxx_messageInfo_NestedEmpty proto.InternalMessageInfo + +func (m *NestedEmpty) GetNested() *Nested { + if m != nil { + return m.Nested + } + return nil +} + func init() { proto.RegisterEnum("envoy.type.Test", Test_name, Test_value) proto.RegisterType((*Simple)(nil), "envoy.type.Simple") proto.RegisterType((*Nested)(nil), "envoy.type.Nested") proto.RegisterMapType((map[string]*Simple)(nil), "envoy.type.Nested.InitialEntry") proto.RegisterType((*Empty)(nil), "envoy.type.Empty") + proto.RegisterType((*NestedEmpty)(nil), "envoy.type.NestedEmpty") } func init() { proto.RegisterFile("tests/api/hello.proto", fileDescriptor_1e23aa1f3bc52fd3) } var fileDescriptor_1e23aa1f3bc52fd3 = []byte{ - // 624 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x74, 0x93, 0xd1, 0x6e, 0xd3, 0x30, - 0x14, 0x86, 0xe7, 0x35, 0x69, 0x96, 0xd3, 0x6e, 0xcb, 0xac, 0x21, 0xa2, 0x6a, 0x62, 0x56, 0x05, - 0xc2, 0xda, 0x45, 0x2a, 0xba, 0x69, 0x02, 0x2e, 0x0b, 0x93, 0x40, 0x9a, 0x3a, 0xc9, 0x03, 0x21, - 0x71, 0x83, 0x52, 0xea, 0x95, 0x68, 0x5e, 0x1c, 0xc5, 0xee, 0xd4, 0xbc, 0x0b, 0x0f, 0xc2, 0x23, - 0xf0, 0x14, 0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x00, 0x00, 0xff, 0xff, 0x3a, 0x2b, 0x8e, 0xd0, 0xb9, 0x04, 0x00, 0x00, + // 694 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x74, 0x94, 0xd1, 0x6a, 0xdb, 0x3c, + 0x14, 0xc7, 0xab, 0xc6, 0x49, 0xea, 0xe3, 0xa4, 0x75, 0x45, 0x3f, 0x3e, 0x13, 0xca, 0x57, 0x11, + 0xbe, 0x31, 0xd1, 0x0b, 0x87, 0xa5, 0x5d, 0x59, 0x07, 0xbb, 0xc9, 0x56, 0xe8, 0xa0, 0x24, 0xa0, + 0x6e, 0x0c, 0x76, 0x53, 0x9c, 0x45, 0xc9, 0x4c, 0x5d, 0xcb, 0xd8, 0x4a, 0x89, 0xdf, 0x65, 0x0f, + 0xb2, 0x47, 0xd8, 0x53, 0xec, 0x2d, 0xc6, 0x6e, 0x87, 0x24, 0xbb, 0x51, 0x69, 0x76, 0x27, 0xff, + 0xcf, 0xef, 0x1c, 0x9f, 0xa3, 0xf3, 0xb7, 0xe1, 0x1f, 0xc9, 0x0b, 0x59, 0x0c, 0xa2, 0x2c, 0x1e, + 0x7c, 0xe5, 0x49, 0x22, 0xc2, 0x2c, 0x17, 0x52, 0x60, 0xe0, 0xe9, 0xbd, 0x28, 0x43, 0x59, 0x66, + 0xbc, 0x77, 0xb8, 0x10, 0x62, 0x91, 0xf0, 0x81, 0x8e, 0x4c, 0x97, 0xf3, 0x41, 0x21, 0xf3, 0xe5, + 0x17, 0x69, 0xc8, 0xde, 0xc1, 0x42, 0x2c, 0x84, 0x3e, 0x0e, 0xd4, 0xa9, 0x52, 0x31, 0x5f, 0x49, + 0x23, 0xf2, 0x55, 0x45, 0xf6, 0xbf, 0x39, 0xd0, 0xba, 0x8e, 0xef, 0xb2, 0x84, 0x63, 0x1f, 0x1a, + 0x85, 0xcc, 0x03, 0x44, 0x10, 0x75, 0x99, 0x3a, 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 0x5c, 0xfb, 0x6f, 0xd3, 0x68, 0x23, 0xe7, 0xc7, 0xcf, 0xa3, 0x2d, 0xf6, + 0x24, 0x01, 0xbf, 0x84, 0x8e, 0xd6, 0x6e, 0x44, 0xca, 0x6f, 0xc4, 0x5c, 0x1b, 0x74, 0x53, 0x81, + 0xcb, 0x2d, 0x06, 0x1a, 0x9c, 0xa4, 0x7c, 0x32, 0xc7, 0x6f, 0xa0, 0x9b, 0xea, 0xde, 0xea, 0xbc, + 0x4e, 0xb5, 0xaa, 0x27, 0xcd, 0xd7, 0xd9, 0x9e, 0xe1, 0x75, 0x7a, 0x6f, 0x0c, 0x1d, 0x7b, 0x26, + 0xf5, 0xb3, 0xb8, 0xe5, 0x65, 0xfd, 0xfb, 0xb8, 0xe5, 0x25, 0xa6, 0xd0, 0xbc, 0x8f, 0x92, 0x25, + 0xd7, 0x3b, 0xdd, 0x7c, 0x7b, 0x06, 0x78, 0xbd, 0xfd, 0x0a, 0x8d, 0xba, 0xe0, 0xa9, 0x15, 0x57, + 0xcd, 0xf4, 0xdb, 0xd0, 0xd4, 0xaf, 0xed, 0x9f, 0x83, 0x67, 0x75, 0xa1, 0x6c, 0x68, 0xba, 0xd8, + 0x64, 0x43, 0x03, 0xb2, 0x8a, 0x38, 0x3e, 0x04, 0x47, 0x7f, 0x13, 0x2e, 0x34, 0x2f, 0x2f, 0xae, + 0xae, 0x26, 0xfe, 0x96, 0x3a, 0x7e, 0x9a, 0xb0, 0xab, 0x77, 0x3e, 0x1a, 0xb9, 0x6a, 0xcf, 0x9f, + 0x1b, 0x51, 0x16, 0x4f, 0x5b, 0xda, 0x96, 0x27, 0x7f, 0x02, 0x00, 0x00, 0xff, 0xff, 0x0e, 0xbc, + 0x6d, 0xd8, 0x79, 0x05, 0x00, 0x00, } diff --git a/tests/api/hello.pb.hash.go b/tests/api/hello.pb.hash.go index e9db7a4..5a7c1a7 100644 --- a/tests/api/hello.pb.hash.go +++ b/tests/api/hello.pb.hash.go @@ -34,6 +34,9 @@ func (m *Simple) Hash(hasher hash.Hash64) (uint64, error) { hasher = fnv.New64() } var err error + if _, err = hasher.Write([]byte("envoy.type.tests/api.Simple")); err != nil { + return 0, err + } if _, err = hasher.Write([]byte(m.GetStr())); err != nil { return 0, err @@ -120,6 +123,9 @@ func (m *Nested) Hash(hasher hash.Hash64) (uint64, error) { hasher = fnv.New64() } var err error + if _, err = hasher.Write([]byte("envoy.type.tests/api.Nested")); err != nil { + return 0, err + } if h, ok := interface{}(m.GetSimple()).(safe_hasher.SafeHasher); ok { if _, err = h.Hash(hasher); err != nil { @@ -241,6 +247,42 @@ func (m *Nested) Hash(hasher hash.Hash64) (uint64, error) { } } + switch m.TestOneOf.(type) { + + case *Nested_EmptyOneOf: + + if h, ok := interface{}(m.GetEmptyOneOf()).(safe_hasher.SafeHasher); ok { + if _, err = h.Hash(hasher); err != nil { + return 0, err + } + } else { + if val, err := hashstructure.Hash(m.GetEmptyOneOf(), nil); err != nil { + return 0, err + } else { + if err := binary.Write(hasher, binary.LittleEndian, val); err != nil { + return 0, err + } + } + } + + case *Nested_NestedOneOf: + + if h, ok := interface{}(m.GetNestedOneOf()).(safe_hasher.SafeHasher); ok { + if _, err = h.Hash(hasher); err != nil { + return 0, err + } + } else { + if val, err := hashstructure.Hash(m.GetNestedOneOf(), nil); err != nil { + return 0, err + } else { + if err := binary.Write(hasher, binary.LittleEndian, val); err != nil { + return 0, err + } + } + } + + } + return hasher.Sum64(), nil } @@ -252,6 +294,40 @@ func (m *Empty) Hash(hasher hash.Hash64) (uint64, error) { if hasher == nil { hasher = fnv.New64() } + var err error + if _, err = hasher.Write([]byte("envoy.type.tests/api.Empty")); err != nil { + return 0, err + } + + return hasher.Sum64(), nil +} + +// Hash function +func (m *NestedEmpty) Hash(hasher hash.Hash64) (uint64, error) { + if m == nil { + return 0, nil + } + if hasher == nil { + hasher = fnv.New64() + } + var err error + if _, err = hasher.Write([]byte("envoy.type.tests/api.NestedEmpty")); err != nil { + return 0, err + } + + if h, ok := interface{}(m.GetNested()).(safe_hasher.SafeHasher); ok { + if _, err = h.Hash(hasher); err != nil { + return 0, err + } + } else { + if val, err := hashstructure.Hash(m.GetNested(), nil); err != nil { + return 0, err + } else { + if err := binary.Write(hasher, binary.LittleEndian, val); err != nil { + return 0, err + } + } + } return hasher.Sum64(), nil } diff --git a/tests/api/hello.proto b/tests/api/hello.proto index 1e93898..8eeac53 100644 --- a/tests/api/hello.proto +++ b/tests/api/hello.proto @@ -57,8 +57,17 @@ message Nested { map initial = 9; Empty emptyNonNullable = 10 [(gogoproto.nullable) = false]; + + oneof test_one_of { + Empty empty_one_of = 11; + NestedEmpty nested_one_of = 12; + } } message Empty { +} + +message NestedEmpty { + Nested nested = 1; } \ No newline at end of file diff --git a/tests/equal_test.go b/tests/equal_test.go deleted file mode 100644 index 5b965af..0000000 --- a/tests/equal_test.go +++ /dev/null @@ -1,9 +0,0 @@ -package tests - -import ( - . "github.com/onsi/ginkgo" -) - -var _ = Describe("equal", func() { - -}) diff --git a/tests/hash_test.go b/tests/hash_test.go index 41457a0..0d3df01 100644 --- a/tests/hash_test.go +++ b/tests/hash_test.go @@ -174,6 +174,25 @@ var _ = Describe("hash", func() { } }) }) + Context("oneof", func() { + It("will treat empty oneof objects differently", func() { + objectWithEmpty := &api.Nested{ + TestOneOf: &api.Nested_EmptyOneOf{ + EmptyOneOf: &api.Empty{}, + }, + } + objectWithNested := &api.Nested{ + TestOneOf: &api.Nested_NestedOneOf{ + NestedOneOf: &api.NestedEmpty{}, + }, + } + hash1, err := objectWithEmpty.Hash(nil) + Expect(err).NotTo(HaveOccurred()) + hash2, err := objectWithNested.Hash(nil) + Expect(err).NotTo(HaveOccurred()) + Expect(hash1).NotTo(Equal(hash2)) + }) + }) }) func allDifferent(hashes []uint64) bool {