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
@@ -987,7 +987,7 @@ func wrapImported(file *FileDescriptor, g *Generator) (sl []*ImportedDescriptor)
987
987
func extractComments (file * FileDescriptor ) {
988
988
file .comments = make (map [string ]* descriptor.SourceCodeInfo_Location )
989
989
for _ , loc := range file .GetSourceCodeInfo ().GetLocation () {
990
- if loc .LeadingComments == nil {
990
+ if loc .LeadingComments == nil && loc . TrailingComments == nil {
991
991
continue
992
992
}
993
993
var p []string
@@ -1321,22 +1321,39 @@ 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
1333
loc , ok := g .file .comments [path ]
1334
- if ! ok {
1334
+ comments := strings .TrimSuffix (loc .GetLeadingComments (), "\n " )
1335
+ if ! ok || comments == "" {
1335
1336
return "" , false
1336
1337
}
1337
1338
w := new (bytes.Buffer )
1338
1339
nl := ""
1339
- for _ , line := range strings .Split (strings .TrimSuffix (loc .GetLeadingComments (), "\n " ), "\n " ) {
1340
+ for _ , line := range strings .Split (comments , "\n " ) {
1341
+ fmt .Fprintf (w , "%s//%s" , nl , line )
1342
+ nl = "\n "
1343
+ }
1344
+ return w .String (), true
1345
+ }
1346
+
1347
+ // makeTrailingComments generates the trailing comment string for the field, no "\n" at the end
1348
+ func (g * Generator ) makeTrailingComments (path string ) (string , bool ) {
1349
+ loc , ok := g .file .comments [path ]
1350
+ comments := strings .TrimSuffix (loc .GetTrailingComments (), "\n " )
1351
+ if ! ok || comments == "" {
1352
+ return "" , false
1353
+ }
1354
+ w := new (bytes.Buffer )
1355
+ nl := ""
1356
+ for _ , line := range strings .Split (comments , "\n " ) {
1340
1357
fmt .Fprintf (w , "%s//%s" , nl , line )
1341
1358
nl = "\n "
1342
1359
}
@@ -1605,6 +1622,7 @@ func (g *Generator) generateEnum(enum *EnumDescriptor) {
1605
1622
// The tag is a string like "varint,2,opt,name=fieldname,def=7" that
1606
1623
// identifies details of the field for the protocol buffer marshaling and unmarshaling
1607
1624
// code. The fields are:
1625
+ //
1608
1626
// wire encoding
1609
1627
// protocol tag number
1610
1628
// opt,req,rep for optional, required, or repeated
@@ -1613,6 +1631,7 @@ func (g *Generator) generateEnum(enum *EnumDescriptor) {
1613
1631
// enum= the name of the enum type if it is an enum-typed field.
1614
1632
// proto3 if this field is in a proto3 message
1615
1633
// def= string representation of the default value, if any.
1634
+ //
1616
1635
// The default value must be in a representation that can be used at run-time
1617
1636
// to generate the default value. Thus bools become 0 and 1, for instance.
1618
1637
func (g * Generator ) goTag (message * Descriptor , field * descriptor.FieldDescriptorProto , wiretype string ) string {
@@ -2179,17 +2198,18 @@ func (f *fieldCommon) getGoType() string {
2179
2198
// simpleField is not weak, not a oneof, not an extension. Can be required, optional or repeated.
2180
2199
type simpleField struct {
2181
2200
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"
2201
+ protoTypeName string // Proto type name, empty if primitive, e.g. ".google.protobuf.Duration"
2202
+ protoType descriptor.FieldDescriptorProto_Type // Actual type enum value, e.g. descriptor.FieldDescriptorProto_TYPE_FIXED64
2203
+ deprecated string // Deprecation comment, if any, e.g. "// Deprecated: Do not use."
2204
+ getterDef string // Default for getters, e.g. "nil", `""` or "Default_MessageType_FieldName"
2205
+ protoDef string // Default value as defined in the proto file, e.g "yoshi" or "5"
2206
+ comment string // The full comment for the field, e.g. "// Useful information"
2207
+ trailingComment string // The trailing comment for the field, e.g. "fieldName fieldType // Useful information"
2188
2208
}
2189
2209
2190
2210
// decl prints the declaration of the field in the struct (if any).
2191
2211
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 )
2212
+ g .P (f .comment , f . deprecated , Annotate (mc .message .file , f .fullPath , f .goName ), "\t " , f .goType , "\t `" , f .tags , "`" , f .trailingComment )
2193
2213
}
2194
2214
2195
2215
// getter prints the getter for the field.
@@ -2870,7 +2890,7 @@ func (g *Generator) generateMessage(message *Descriptor) {
2870
2890
// This is the first field of a oneof we haven't seen before.
2871
2891
// Generate the union field.
2872
2892
oneofFullPath := fmt .Sprintf ("%s,%d,%d" , message .path , messageOneofPath , * field .OneofIndex )
2873
- c , ok := g .makeComments (oneofFullPath )
2893
+ c , ok := g .makeLeadingComments (oneofFullPath )
2874
2894
if ok {
2875
2895
c += "\n //\n "
2876
2896
}
@@ -2909,7 +2929,7 @@ func (g *Generator) generateMessage(message *Descriptor) {
2909
2929
goTyp , _ := g .GoType (message , field )
2910
2930
fieldDeprecated := ""
2911
2931
if field .GetOptions ().GetDeprecated () {
2912
- fieldDeprecated = deprecationComment
2932
+ fieldDeprecated = deprecationComment + " \n "
2913
2933
}
2914
2934
dvalue := g .getterDefault (field , goTypeName , GoTypeToName (goTyp ))
2915
2935
if oneof {
@@ -2965,10 +2985,12 @@ func (g *Generator) generateMessage(message *Descriptor) {
2965
2985
}
2966
2986
2967
2987
fieldFullPath := fmt .Sprintf ("%s,%d,%d" , message .path , messageFieldPath , i )
2968
- c , ok := g .makeComments (fieldFullPath )
2988
+ c , ok := g .makeLeadingComments (fieldFullPath )
2969
2989
if ok {
2970
2990
c += "\n "
2971
2991
}
2992
+ tc , _ := g .makeTrailingComments (fieldFullPath )
2993
+
2972
2994
rf := simpleField {
2973
2995
fieldCommon : fieldCommon {
2974
2996
goName : fieldName ,
@@ -2979,12 +3001,13 @@ func (g *Generator) generateMessage(message *Descriptor) {
2979
3001
fullPath : fieldFullPath ,
2980
3002
protoField : field ,
2981
3003
},
2982
- protoTypeName : field .GetTypeName (),
2983
- protoType : * field .Type ,
2984
- deprecated : fieldDeprecated ,
2985
- getterDef : dvalue ,
2986
- protoDef : field .GetDefaultValue (),
2987
- comment : c ,
3004
+ protoTypeName : field .GetTypeName (),
3005
+ protoType : * field .Type ,
3006
+ deprecated : fieldDeprecated ,
3007
+ getterDef : dvalue ,
3008
+ protoDef : field .GetDefaultValue (),
3009
+ comment : c ,
3010
+ trailingComment : tc ,
2988
3011
}
2989
3012
var pf topLevelField = & rf
2990
3013
0 commit comments