From bc30ff1b55aa7f17d3d2da7e2cdbe94579fb9e39 Mon Sep 17 00:00:00 2001 From: Alfan Nur Fauzan Date: Sun, 15 Aug 2021 16:37:21 +0700 Subject: [PATCH] Nested Type Naming (#9) * refine nested type naming * refactor * update test cases --- content_builder.go | 29 +++++++++++++++++++---------- example/user_add_comment.pps | 8 ++++---- test/user_add_comment.out | 12 ++++++------ 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/content_builder.go b/content_builder.go index 797be17..38f32a6 100644 --- a/content_builder.go +++ b/content_builder.go @@ -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 { @@ -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) +} diff --git a/example/user_add_comment.pps b/example/user_add_comment.pps index cad87c8..05aa047 100644 --- a/example/user_add_comment.pps +++ b/example/user_add_comment.pps @@ -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; } diff --git a/test/user_add_comment.out b/test/user_add_comment.out index 7e43478..97343bd 100644 --- a/test/user_add_comment.out +++ b/test/user_add_comment.out @@ -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; }