Skip to content

Commit

Permalink
Nested Type Naming (#9)
Browse files Browse the repository at this point in the history
* refine nested type naming

* refactor

* update test cases
  • Loading branch information
alpancs authored Aug 15, 2021
1 parent 4cb7763 commit bc30ff1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
29 changes: 19 additions & 10 deletions content_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ func (b *contentBuilder) getFieldType(field *descriptorpb.FieldDescriptorProto)
return strings.ToLower(strings.TrimPrefix(field.GetType().String(), "TYPE_")), ""
}
fullMessageName := field.GetTypeName()
wkt := wktMapping[fullMessageName]
if b.messageEncoding == "json" && wkt != "" {
return wkt, ""
if b.messageEncoding == "json" {
if wkt, ok := wktMapping[fullMessageName]; ok {
return wkt, ""
}
}
return getLocalName(fullMessageName), fullMessageName
return b.getLocalName(fullMessageName), fullMessageName
}

func (b *contentBuilder) getLabelPrefix(label descriptorpb.FieldDescriptorProto_Label) string {
Expand All @@ -90,21 +91,29 @@ func (b *contentBuilder) payDebts(debts []string, level int) {
func (b *contentBuilder) payDebt(debt string, level int) {
message := b.messageTypes[debt]
defer func(originalName *string) { message.Name = originalName }(message.Name)
localName := getLocalName(debt)
localName := b.getLocalName(debt)
message.Name = &localName
b.output.WriteString("\n")
b.buildMessage(message, level)
}

func buildIndent(level int) string {
return strings.Repeat(" ", level)
}

var localNamePattern = regexp.MustCompile(`\..`)

func getLocalName(fullMessageName string) string {
func (b *contentBuilder) getLocalName(fullMessageName string) string {
if b.isNestedType(fullMessageName) {
return fullMessageName[strings.LastIndexByte(fullMessageName, '.')+1:]
}
return localNamePattern.ReplaceAllStringFunc(
fullMessageName,
func(s string) string { return strings.ToUpper(s[1:]) },
)
}

func (b *contentBuilder) isNestedType(fullMessageName string) bool {
parent := fullMessageName[:strings.LastIndexByte(fullMessageName, '.')]
return b.messageTypes[parent] != nil
}

func buildIndent(level int) string {
return strings.Repeat(" ", level)
}
8 changes: 4 additions & 4 deletions example/user_add_comment.pps
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
syntax = "proto2";

message UserAddComment {
required ExampleUserAddCommentUser user = 1;
required User user = 1;
required string comment = 2;
repeated ExampleCommonLabel labels = 3;
required GoogleProtobufTimestamp timestamp = 101;

message ExampleUserAddCommentUser {
message User {
required string first_name = 1;
optional string last_name = 2;
optional bytes avatar = 3;
optional ExampleUserAddCommentUserLocation location = 4;
optional Location location = 4;
optional GoogleProtobufTimestamp created_at = 5;
optional GoogleProtobufTimestamp updated_at = 6;

message ExampleUserAddCommentUserLocation {
message Location {
required double longitude = 1;
required double latitude = 2;
}
Expand Down
12 changes: 6 additions & 6 deletions test/user_add_comment.out
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
z�
example/user_add_comment.ppsz�syntax = "proto2";
z�
example/user_add_comment.ppsz�syntax = "proto2";

message UserAddComment {
required ExampleUserAddCommentUser user = 1;
required User user = 1;
required string comment = 2;
repeated ExampleCommonLabel labels = 3;
required GoogleProtobufTimestamp timestamp = 101;

message ExampleUserAddCommentUser {
message User {
required string first_name = 1;
optional string last_name = 2;
optional bytes avatar = 3;
optional ExampleUserAddCommentUserLocation location = 4;
optional Location location = 4;
optional GoogleProtobufTimestamp created_at = 5;
optional GoogleProtobufTimestamp updated_at = 6;

message ExampleUserAddCommentUserLocation {
message Location {
required double longitude = 1;
required double latitude = 2;
}
Expand Down

0 comments on commit bc30ff1

Please sign in to comment.