-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* build external enums * bump version * upgrade github action setup-protoc * remove some invalid tests * refactor
- Loading branch information
Showing
11 changed files
with
220 additions
and
1,456 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
syntax = "proto3"; | ||
|
||
package example.common; | ||
|
||
enum Role { | ||
OWNER = 0; | ||
EDITOR = 1; | ||
VIEWER = 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"google.golang.org/protobuf/types/descriptorpb" | ||
) | ||
|
||
type messageBuilder struct { | ||
*contentBuilder | ||
message *descriptorpb.DescriptorProto | ||
level int | ||
externalMessages []*descriptorpb.DescriptorProto | ||
externalEnums []*descriptorpb.EnumDescriptorProto | ||
} | ||
|
||
func newMessageBuilder(b *contentBuilder, message *descriptorpb.DescriptorProto, level int) *messageBuilder { | ||
return &messageBuilder{b, message, level, nil, nil} | ||
} | ||
|
||
func (b *messageBuilder) build() { | ||
fmt.Fprintf(b.output, "%smessage %s {\n", buildIndent(b.level), b.message.GetName()) | ||
b.buildFields() | ||
b.buildMessages(b.message.GetNestedType(), b.level+1) | ||
b.buildEnums(b.message.GetEnumType(), b.level+1) | ||
fmt.Fprintf(b.output, "%s}\n", buildIndent(b.level)) | ||
} | ||
|
||
func (b *messageBuilder) buildFields() { | ||
for _, field := range b.message.GetField() { | ||
fmt.Fprint(b.output, buildIndent(b.level+1)) | ||
label := field.GetLabel() | ||
if b.schemaSyntax == "proto2" || label == descriptorpb.FieldDescriptorProto_LABEL_REPEATED { | ||
fmt.Fprintf(b.output, "%s ", strings.ToLower(strings.TrimPrefix(label.String(), "LABEL_"))) | ||
} | ||
fmt.Fprintf(b.output, "%s %s = %d;\n", b.buildFieldType(field), field.GetName(), field.GetNumber()) | ||
} | ||
} | ||
|
||
func (b *messageBuilder) buildFieldType(field *descriptorpb.FieldDescriptorProto) string { | ||
typeName := field.GetTypeName() | ||
if b.isNestedType(field) { | ||
return getChildName(typeName) | ||
} | ||
switch field.GetType() { | ||
case descriptorpb.FieldDescriptorProto_TYPE_MESSAGE: | ||
if b.messageEncoding == "json" && wktMapping[typeName] != "" { | ||
return wktMapping[typeName] | ||
} | ||
internalName := pascalCase(typeName) | ||
internalMessage := b.messageTypes[field.GetTypeName()] | ||
internalMessage.Name = &internalName | ||
b.message.NestedType = append(b.message.NestedType, internalMessage) | ||
return internalName | ||
case descriptorpb.FieldDescriptorProto_TYPE_ENUM: | ||
internalName := pascalCase(typeName) | ||
internalEnum := b.enums[field.GetTypeName()] | ||
internalEnum.Name = &internalName | ||
b.message.EnumType = append(b.message.EnumType, internalEnum) | ||
return internalName | ||
default: | ||
return strings.ToLower(strings.TrimPrefix(field.GetType().String(), "TYPE_")) | ||
} | ||
} | ||
|
||
func (b *messageBuilder) isNestedType(field *descriptorpb.FieldDescriptorProto) bool { | ||
return b.messageTypes[getParentName(field.GetTypeName())] == b.message | ||
} | ||
|
||
func getParentName(name string) string { | ||
lastDotIndex := strings.LastIndexByte(name, '.') | ||
if lastDotIndex == -1 { | ||
return name | ||
} | ||
return name[:lastDotIndex] | ||
} | ||
|
||
func getChildName(name string) string { | ||
return name[strings.LastIndexByte(name, '.')+1:] | ||
} | ||
|
||
func pascalCase(name string) string { | ||
sb := new(strings.Builder) | ||
for i, c := range name { | ||
if i > 0 && name[i-1] == '.' { | ||
sb.WriteString(strings.ToUpper(string(c))) | ||
} else if c != '.' { | ||
sb.WriteRune(c) | ||
} | ||
} | ||
return sb.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func Test_getParentName(t *testing.T) { | ||
type args struct { | ||
name string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
}{ | ||
{ | ||
name: "empty name", | ||
args: args{""}, | ||
want: "", | ||
}, | ||
{ | ||
name: "normal name", | ||
args: args{".example.UserAddComment.User"}, | ||
want: ".example.UserAddComment", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := getParentName(tt.args.name); got != tt.want { | ||
t.Errorf("getParentName() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_getChildName(t *testing.T) { | ||
type args struct { | ||
name string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
}{ | ||
{ | ||
name: "empty name", | ||
args: args{""}, | ||
want: "", | ||
}, | ||
{ | ||
name: "normal name", | ||
args: args{".example.UserAddComment.User"}, | ||
want: "User", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := getChildName(tt.args.name); got != tt.want { | ||
t.Errorf("getChildName() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.