35
35
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
36
37
37
/*
38
- The code generator for the plugin for the Google protocol buffer compiler.
39
- It generates Go code from the protocol buffer description files read by the
40
- main routine.
38
+ The code generator for the plugin for the Google protocol buffer compiler.
39
+ It generates Go code from the protocol buffer description files read by the
40
+ main routine.
41
41
*/
42
42
package generator
43
43
@@ -1321,15 +1321,30 @@ func (g *Generator) PrintComments(path string) bool {
1321
1321
if ! g .writeOutput {
1322
1322
return false
1323
1323
}
1324
- if c , ok := g .makeComments (path ); ok {
1324
+ if c , ok := g .makeLeadingComments (path ); ok {
1325
1325
g .P (c )
1326
1326
return true
1327
1327
}
1328
1328
return false
1329
1329
}
1330
1330
1331
- // makeComments generates the comment string for the field, no "\n" at the end
1332
- func (g * Generator ) makeComments (path string ) (string , bool ) {
1331
+ // makeLeadingComments generates the comment string for the field, no "\n" at the end
1332
+ func (g * Generator ) makeLeadingComments (path string ) (string , bool ) {
1333
+ loc , ok := g .file .comments [path ]
1334
+ if ! ok {
1335
+ return "" , false
1336
+ }
1337
+ w := new (bytes.Buffer )
1338
+ nl := ""
1339
+ for _ , line := range strings .Split (strings .TrimSuffix (loc .GetLeadingComments (), "\n " ), "\n " ) {
1340
+ fmt .Fprintf (w , "%s//%s" , nl , line )
1341
+ nl = "\n "
1342
+ }
1343
+ return w .String (), true
1344
+ }
1345
+
1346
+ // makeTrailingComments generates the trailing comment string for the field, no "\n" at the end
1347
+ func (g * Generator ) makeTrailingComments (path string ) (string , bool ) {
1333
1348
loc , ok := g .file .comments [path ]
1334
1349
if ! ok {
1335
1350
return "" , false
@@ -1605,6 +1620,7 @@ func (g *Generator) generateEnum(enum *EnumDescriptor) {
1605
1620
// The tag is a string like "varint,2,opt,name=fieldname,def=7" that
1606
1621
// identifies details of the field for the protocol buffer marshaling and unmarshaling
1607
1622
// code. The fields are:
1623
+ //
1608
1624
// wire encoding
1609
1625
// protocol tag number
1610
1626
// opt,req,rep for optional, required, or repeated
@@ -1613,6 +1629,7 @@ func (g *Generator) generateEnum(enum *EnumDescriptor) {
1613
1629
// enum= the name of the enum type if it is an enum-typed field.
1614
1630
// proto3 if this field is in a proto3 message
1615
1631
// def= string representation of the default value, if any.
1632
+ //
1616
1633
// The default value must be in a representation that can be used at run-time
1617
1634
// to generate the default value. Thus bools become 0 and 1, for instance.
1618
1635
func (g * Generator ) goTag (message * Descriptor , field * descriptor.FieldDescriptorProto , wiretype string ) string {
@@ -2179,17 +2196,18 @@ func (f *fieldCommon) getGoType() string {
2179
2196
// simpleField is not weak, not a oneof, not an extension. Can be required, optional or repeated.
2180
2197
type simpleField struct {
2181
2198
fieldCommon
2182
- protoTypeName string // Proto type name, empty if primitive, e.g. ".google.protobuf.Duration"
2183
- protoType descriptor.FieldDescriptorProto_Type // Actual type enum value, e.g. descriptor.FieldDescriptorProto_TYPE_FIXED64
2184
- deprecated string // Deprecation comment, if any, e.g. "// Deprecated: Do not use."
2185
- getterDef string // Default for getters, e.g. "nil", `""` or "Default_MessageType_FieldName"
2186
- protoDef string // Default value as defined in the proto file, e.g "yoshi" or "5"
2187
- comment string // The full comment for the field, e.g. "// Useful information"
2199
+ protoTypeName string // Proto type name, empty if primitive, e.g. ".google.protobuf.Duration"
2200
+ protoType descriptor.FieldDescriptorProto_Type // Actual type enum value, e.g. descriptor.FieldDescriptorProto_TYPE_FIXED64
2201
+ deprecated string // Deprecation comment, if any, e.g. "// Deprecated: Do not use."
2202
+ getterDef string // Default for getters, e.g. "nil", `""` or "Default_MessageType_FieldName"
2203
+ protoDef string // Default value as defined in the proto file, e.g "yoshi" or "5"
2204
+ comment string // The full comment for the field, e.g. "// Useful information"
2205
+ trailingComment string // The trailing comment for the field, e.g. "fieldName fieldType // Useful information"
2188
2206
}
2189
2207
2190
2208
// decl prints the declaration of the field in the struct (if any).
2191
2209
func (f * simpleField ) decl (g * Generator , mc * msgCtx ) {
2192
- g .P (f .comment , Annotate (mc .message .file , f .fullPath , f .goName ), "\t " , f .goType , "\t `" , f .tags , "`" , f .deprecated )
2210
+ g .P (f .comment , Annotate (mc .message .file , f .fullPath , f .goName ), "\t " , f .goType , "\t `" , f .tags , "`" , f .deprecated , f . trailingComment )
2193
2211
}
2194
2212
2195
2213
// getter prints the getter for the field.
@@ -2870,7 +2888,7 @@ func (g *Generator) generateMessage(message *Descriptor) {
2870
2888
// This is the first field of a oneof we haven't seen before.
2871
2889
// Generate the union field.
2872
2890
oneofFullPath := fmt .Sprintf ("%s,%d,%d" , message .path , messageOneofPath , * field .OneofIndex )
2873
- c , ok := g .makeComments (oneofFullPath )
2891
+ c , ok := g .makeLeadingComments (oneofFullPath )
2874
2892
if ok {
2875
2893
c += "\n //\n "
2876
2894
}
@@ -2965,10 +2983,15 @@ func (g *Generator) generateMessage(message *Descriptor) {
2965
2983
}
2966
2984
2967
2985
fieldFullPath := fmt .Sprintf ("%s,%d,%d" , message .path , messageFieldPath , i )
2968
- c , ok := g .makeComments (fieldFullPath )
2986
+ c , ok := g .makeLeadingComments (fieldFullPath )
2969
2987
if ok {
2970
2988
c += "\n "
2971
2989
}
2990
+ tc , ok := g .makeTrailingComments (fieldFullPath )
2991
+ if ok {
2992
+ tc += "\n "
2993
+ }
2994
+
2972
2995
rf := simpleField {
2973
2996
fieldCommon : fieldCommon {
2974
2997
goName : fieldName ,
@@ -2979,12 +3002,13 @@ func (g *Generator) generateMessage(message *Descriptor) {
2979
3002
fullPath : fieldFullPath ,
2980
3003
protoField : field ,
2981
3004
},
2982
- protoTypeName : field .GetTypeName (),
2983
- protoType : * field .Type ,
2984
- deprecated : fieldDeprecated ,
2985
- getterDef : dvalue ,
2986
- protoDef : field .GetDefaultValue (),
2987
- comment : c ,
3005
+ protoTypeName : field .GetTypeName (),
3006
+ protoType : * field .Type ,
3007
+ deprecated : fieldDeprecated ,
3008
+ getterDef : dvalue ,
3009
+ protoDef : field .GetDefaultValue (),
3010
+ comment : c ,
3011
+ trailingComment : tc ,
2988
3012
}
2989
3013
var pf topLevelField = & rf
2990
3014
0 commit comments