Skip to content

Commit

Permalink
go: name collisions with oneof fields and nested messages/enums (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
rodaine authored Oct 26, 2018
1 parent 6a9d0d1 commit 2a4874b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lang/go/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,21 @@ func (c context) Name(node pgs.Node) pgs.Name {
}

func (c context) OneofOption(field pgs.Field) pgs.Name {
return pgs.Name(joinNames(c.Name(field.Message()), c.Name(field)))
n := pgs.Name(joinNames(c.Name(field.Message()), c.Name(field)))

for _, msg := range field.Message().Messages() {
if c.Name(msg) == n {
return n + "_"
}
}

for _, en := range field.Message().Enums() {
if c.Name(en) == n {
return n + "_"
}
}

return n
}

func (c context) ServerName(s pgs.Service) pgs.Name {
Expand Down
4 changes: 4 additions & 0 deletions lang/go/name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ func TestName(t *testing.T) {
{"_underscore", "XUnderscore"},
{"__DoubleUnderscore", "X_DoubleUnderscore"},
{"String", "String"},
{"MsgWith3dInside", "MsgWith3DInside"},
{"MsgEndsWith3d", "MsgEndsWith3D"},

// Nested Messages
{"Nested.Message", "Nested_Message"},
Expand Down Expand Up @@ -159,6 +161,8 @@ func TestContext_OneofOption(t *testing.T) {
{"lowerCamelCaseO", "Oneofs_LowerCamelCaseO"},
{"UpperCamelCaseO", "Oneofs_UpperCamelCaseO"},
{"reset", "Oneofs_Reset_"},
{"some_msg", "Oneofs_SomeMsg_"},
{"some_enum", "Oneofs_SomeEnum_"},
}

for _, test := range tests {
Expand Down
12 changes: 12 additions & 0 deletions lang/go/testdata/names/entities/entities.proto
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ message Oneofs {
bool UpperCamelCaseO = 11;
bool reset = 12; // protected
}

message SomeMsg {}
enum SomeEnum { VALUE = 0; }
oneof some_msg_oneof {
SomeMsg some_msg = 13;
SomeEnum some_enum = 14;
}
}

service UpperCamelService {}
Expand All @@ -110,3 +117,8 @@ service Service {
rpc SCREAMING_SNAKE(String) returns (String);
rpc Reset(String) returns (String);
}

message MsgWith3dInside {}

message MsgEndsWith3d {}

0 comments on commit 2a4874b

Please sign in to comment.