Skip to content

Commit bc30ff1

Browse files
authored
Nested Type Naming (#9)
* refine nested type naming * refactor * update test cases
1 parent 4cb7763 commit bc30ff1

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
lines changed

content_builder.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,12 @@ func (b *contentBuilder) getFieldType(field *descriptorpb.FieldDescriptorProto)
6060
return strings.ToLower(strings.TrimPrefix(field.GetType().String(), "TYPE_")), ""
6161
}
6262
fullMessageName := field.GetTypeName()
63-
wkt := wktMapping[fullMessageName]
64-
if b.messageEncoding == "json" && wkt != "" {
65-
return wkt, ""
63+
if b.messageEncoding == "json" {
64+
if wkt, ok := wktMapping[fullMessageName]; ok {
65+
return wkt, ""
66+
}
6667
}
67-
return getLocalName(fullMessageName), fullMessageName
68+
return b.getLocalName(fullMessageName), fullMessageName
6869
}
6970

7071
func (b *contentBuilder) getLabelPrefix(label descriptorpb.FieldDescriptorProto_Label) string {
@@ -90,21 +91,29 @@ func (b *contentBuilder) payDebts(debts []string, level int) {
9091
func (b *contentBuilder) payDebt(debt string, level int) {
9192
message := b.messageTypes[debt]
9293
defer func(originalName *string) { message.Name = originalName }(message.Name)
93-
localName := getLocalName(debt)
94+
localName := b.getLocalName(debt)
9495
message.Name = &localName
9596
b.output.WriteString("\n")
9697
b.buildMessage(message, level)
9798
}
9899

99-
func buildIndent(level int) string {
100-
return strings.Repeat(" ", level)
101-
}
102-
103100
var localNamePattern = regexp.MustCompile(`\..`)
104101

105-
func getLocalName(fullMessageName string) string {
102+
func (b *contentBuilder) getLocalName(fullMessageName string) string {
103+
if b.isNestedType(fullMessageName) {
104+
return fullMessageName[strings.LastIndexByte(fullMessageName, '.')+1:]
105+
}
106106
return localNamePattern.ReplaceAllStringFunc(
107107
fullMessageName,
108108
func(s string) string { return strings.ToUpper(s[1:]) },
109109
)
110110
}
111+
112+
func (b *contentBuilder) isNestedType(fullMessageName string) bool {
113+
parent := fullMessageName[:strings.LastIndexByte(fullMessageName, '.')]
114+
return b.messageTypes[parent] != nil
115+
}
116+
117+
func buildIndent(level int) string {
118+
return strings.Repeat(" ", level)
119+
}

example/user_add_comment.pps

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
syntax = "proto2";
22

33
message UserAddComment {
4-
required ExampleUserAddCommentUser user = 1;
4+
required User user = 1;
55
required string comment = 2;
66
repeated ExampleCommonLabel labels = 3;
77
required GoogleProtobufTimestamp timestamp = 101;
88

9-
message ExampleUserAddCommentUser {
9+
message User {
1010
required string first_name = 1;
1111
optional string last_name = 2;
1212
optional bytes avatar = 3;
13-
optional ExampleUserAddCommentUserLocation location = 4;
13+
optional Location location = 4;
1414
optional GoogleProtobufTimestamp created_at = 5;
1515
optional GoogleProtobufTimestamp updated_at = 6;
1616

17-
message ExampleUserAddCommentUserLocation {
17+
message Location {
1818
required double longitude = 1;
1919
required double latitude = 2;
2020
}

test/user_add_comment.out

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
z�
2-
example/user_add_comment.ppsz�syntax = "proto2";
1+
z�
2+
example/user_add_comment.ppsz�syntax = "proto2";
33

44
message UserAddComment {
5-
required ExampleUserAddCommentUser user = 1;
5+
required User user = 1;
66
required string comment = 2;
77
repeated ExampleCommonLabel labels = 3;
88
required GoogleProtobufTimestamp timestamp = 101;
99

10-
message ExampleUserAddCommentUser {
10+
message User {
1111
required string first_name = 1;
1212
optional string last_name = 2;
1313
optional bytes avatar = 3;
14-
optional ExampleUserAddCommentUserLocation location = 4;
14+
optional Location location = 4;
1515
optional GoogleProtobufTimestamp created_at = 5;
1616
optional GoogleProtobufTimestamp updated_at = 6;
1717

18-
message ExampleUserAddCommentUserLocation {
18+
message Location {
1919
required double longitude = 1;
2020
required double latitude = 2;
2121
}

0 commit comments

Comments
 (0)