@@ -29,7 +29,7 @@ func (b *contentBuilder) build(protoFile *descriptorpb.FileDescriptorProto) (str
2929 compVersion := b .request .GetCompilerVersion ()
3030 fmt .Fprintf (b .output , "// Code generated by protoc-gen-pubsub-schema. DO NOT EDIT.\n " )
3131 fmt .Fprintf (b .output , "// versions:\n " )
32- fmt .Fprintf (b .output , "// protoc-gen-pubsub-schema v1.4.3 \n " )
32+ fmt .Fprintf (b .output , "// protoc-gen-pubsub-schema v1.4.4 \n " )
3333 fmt .Fprintf (b .output , "// protoc v%d.%d.%d%s\n " , compVersion .GetMajor (), compVersion .GetMinor (), compVersion .GetPatch (), compVersion .GetSuffix ())
3434 fmt .Fprintf (b .output , "// source: %s\n \n " , protoFile .GetName ())
3535 fmt .Fprintf (b .output , "syntax = \" %s\" ;\n " , b .schemaSyntax )
@@ -39,110 +39,33 @@ func (b *contentBuilder) build(protoFile *descriptorpb.FileDescriptorProto) (str
3939}
4040
4141func (b * contentBuilder ) buildMessages (messages []* descriptorpb.DescriptorProto , level int ) {
42+ built := make (map [* descriptorpb.DescriptorProto ]bool )
4243 for _ , message := range messages {
43- fmt .Fprintln (b .output )
44- b .buildMessage (message , level )
45- }
46- }
47-
48- func (b * contentBuilder ) buildMessage (message * descriptorpb.DescriptorProto , level int ) {
49- fmt .Fprintf (b .output , "%smessage %s {\n " , buildIndent (level ), message .GetName ())
50- b .buildFields (message .GetField (), level + 1 )
51- b .buildMessages (message .GetNestedType (), level + 1 )
52- b .buildEnums (message .GetEnumType (), level + 1 )
53- b .buildOtherTypes (message , level + 1 )
54- fmt .Fprintf (b .output , "%s}\n " , buildIndent (level ))
55- }
56-
57- func (b * contentBuilder ) buildFields (fields []* descriptorpb.FieldDescriptorProto , level int ) {
58- for _ , field := range fields {
59- fmt .Fprint (b .output , buildIndent (level ))
60- label := field .GetLabel ()
61- if b .schemaSyntax == "proto2" || label == descriptorpb .FieldDescriptorProto_LABEL_REPEATED {
62- fmt .Fprintf (b .output , "%s " , strings .ToLower (strings .TrimPrefix (label .String (), "LABEL_" )))
63- }
64- fmt .Fprintf (b .output , "%s %s = %d;\n " , b .getFieldType (field ), field .GetName (), field .GetNumber ())
65- }
66- }
67-
68- func (b * contentBuilder ) getFieldType (field * descriptorpb.FieldDescriptorProto ) string {
69- typeName := field .GetTypeName ()
70- switch field .GetType () {
71- case descriptorpb .FieldDescriptorProto_TYPE_MESSAGE :
72- if b .messageEncoding == "json" && wktMapping [typeName ] != "" {
73- return wktMapping [typeName ]
74- }
75- if b .isNestedType (typeName ) {
76- return shortName (typeName )
44+ if built [message ] {
45+ continue
7746 }
78- return pascalCase (typeName )
79- case descriptorpb .FieldDescriptorProto_TYPE_ENUM :
80- return shortName (typeName )
81- default :
82- return strings .ToLower (strings .TrimPrefix (field .GetType ().String (), "TYPE_" ))
47+ fmt .Fprintln (b .output )
48+ newMessageBuilder (b , message , level ).build ()
49+ built [message ] = true
8350 }
8451}
8552
8653func (b * contentBuilder ) buildEnums (enums []* descriptorpb.EnumDescriptorProto , level int ) {
54+ built := make (map [* descriptorpb.EnumDescriptorProto ]bool )
8755 for _ , enum := range enums {
56+ if built [enum ] {
57+ continue
58+ }
8859 fmt .Fprintln (b .output )
8960 fmt .Fprintf (b .output , "%senum %s {\n " , buildIndent (level ), enum .GetName ())
9061 for _ , value := range enum .GetValue () {
9162 fmt .Fprintf (b .output , "%s%s = %d;\n " , buildIndent (level + 1 ), value .GetName (), value .GetNumber ())
9263 }
9364 fmt .Fprintf (b .output , "%s}\n " , buildIndent (level ))
65+ built [enum ] = true
9466 }
9567}
9668
97- func (b * contentBuilder ) buildOtherTypes (message * descriptorpb.DescriptorProto , level int ) {
98- built := make (map [string ]bool )
99- for _ , field := range message .GetField () {
100- typeName := field .GetTypeName ()
101- if field .GetType () != descriptorpb .FieldDescriptorProto_TYPE_MESSAGE {
102- continue
103- }
104- if b .messageEncoding == "json" && wktMapping [typeName ] != "" {
105- continue
106- }
107- if b .isNestedType (typeName ) {
108- continue
109- }
110- if built [typeName ] {
111- continue
112- }
113- b .buildOtherType (typeName , level )
114- built [typeName ] = true
115- }
116- }
117-
118- func (b * contentBuilder ) buildOtherType (typeName string , level int ) {
119- message := b .messageTypes [typeName ]
120- defer func (name * string ) { message .Name = name }(message .Name )
121- * message .Name = pascalCase (typeName )
122- fmt .Fprintln (b .output )
123- b .buildMessage (message , level )
124- }
125-
126- func (b * contentBuilder ) isNestedType (name string ) bool {
127- return b .messageTypes [name [:strings .LastIndexByte (name , '.' )]] != nil
128- }
129-
13069func buildIndent (level int ) string {
13170 return strings .Repeat (" " , level )
13271}
133-
134- func shortName (name string ) string {
135- return name [strings .LastIndexByte (name , '.' )+ 1 :]
136- }
137-
138- func pascalCase (name string ) string {
139- sb := new (strings.Builder )
140- for i , c := range name {
141- if i > 0 && name [i - 1 ] == '.' {
142- sb .WriteString (strings .ToUpper (string (c )))
143- } else if c != '.' {
144- sb .WriteRune (c )
145- }
146- }
147- return sb .String ()
148- }
0 commit comments