3535// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3636
3737/*
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.
4141*/
4242package generator
4343
@@ -987,7 +987,7 @@ func wrapImported(file *FileDescriptor, g *Generator) (sl []*ImportedDescriptor)
987987func extractComments (file * FileDescriptor ) {
988988 file .comments = make (map [string ]* descriptor.SourceCodeInfo_Location )
989989 for _ , loc := range file .GetSourceCodeInfo ().GetLocation () {
990- if loc .LeadingComments == nil {
990+ if loc .LeadingComments == nil && loc . TrailingComments == nil {
991991 continue
992992 }
993993 var p []string
@@ -1321,17 +1321,17 @@ func (g *Generator) PrintComments(path string) bool {
13211321 if ! g .writeOutput {
13221322 return false
13231323 }
1324- if c , ok := g .makeComments (path ); ok {
1324+ if c , ok := g .makeLeadingComments (path ); ok {
13251325 g .P (c )
13261326 return true
13271327 }
13281328 return false
13291329}
13301330
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 ) {
13331333 loc , ok := g .file .comments [path ]
1334- if ! ok {
1334+ if ! ok || loc . GetLeadingComments () == "" {
13351335 return "" , false
13361336 }
13371337 w := new (bytes.Buffer )
@@ -1343,6 +1343,21 @@ func (g *Generator) makeComments(path string) (string, bool) {
13431343 return w .String (), true
13441344}
13451345
1346+ // makeTrailingComments generates the trailing comment string for the field, no "\n" at the end
1347+ func (g * Generator ) makeTrailingComments (path string ) (string , bool ) {
1348+ loc , ok := g .file .comments [path ]
1349+ if ! ok || loc .GetTrailingComments () == "" {
1350+ return "" , false
1351+ }
1352+ w := new (bytes.Buffer )
1353+ nl := ""
1354+ for _ , line := range strings .Split (strings .TrimSuffix (loc .GetTrailingComments (), "\n " ), "\n " ) {
1355+ fmt .Fprintf (w , "%s//%s" , nl , line )
1356+ nl = "\n "
1357+ }
1358+ return w .String (), true
1359+ }
1360+
13461361// Comments returns any comments from the source .proto file and empty string if comments not found.
13471362// The path is a comma-separated list of intergers.
13481363// See descriptor.proto for its format.
@@ -1605,6 +1620,7 @@ func (g *Generator) generateEnum(enum *EnumDescriptor) {
16051620// The tag is a string like "varint,2,opt,name=fieldname,def=7" that
16061621// identifies details of the field for the protocol buffer marshaling and unmarshaling
16071622// code. The fields are:
1623+ //
16081624// wire encoding
16091625// protocol tag number
16101626// opt,req,rep for optional, required, or repeated
@@ -1613,6 +1629,7 @@ func (g *Generator) generateEnum(enum *EnumDescriptor) {
16131629// enum= the name of the enum type if it is an enum-typed field.
16141630// proto3 if this field is in a proto3 message
16151631// def= string representation of the default value, if any.
1632+ //
16161633// The default value must be in a representation that can be used at run-time
16171634// to generate the default value. Thus bools become 0 and 1, for instance.
16181635func (g * Generator ) goTag (message * Descriptor , field * descriptor.FieldDescriptorProto , wiretype string ) string {
@@ -2179,17 +2196,18 @@ func (f *fieldCommon) getGoType() string {
21792196// simpleField is not weak, not a oneof, not an extension. Can be required, optional or repeated.
21802197type simpleField struct {
21812198 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"
21882206}
21892207
21902208// decl prints the declaration of the field in the struct (if any).
21912209func (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 , f . deprecated , Annotate (mc .message .file , f .fullPath , f .goName ), "\t " , f .goType , "\t `" , f .tags , "`" , f .trailingComment )
21932211}
21942212
21952213// getter prints the getter for the field.
@@ -2870,7 +2888,7 @@ func (g *Generator) generateMessage(message *Descriptor) {
28702888 // This is the first field of a oneof we haven't seen before.
28712889 // Generate the union field.
28722890 oneofFullPath := fmt .Sprintf ("%s,%d,%d" , message .path , messageOneofPath , * field .OneofIndex )
2873- c , ok := g .makeComments (oneofFullPath )
2891+ c , ok := g .makeLeadingComments (oneofFullPath )
28742892 if ok {
28752893 c += "\n //\n "
28762894 }
@@ -2909,7 +2927,7 @@ func (g *Generator) generateMessage(message *Descriptor) {
29092927 goTyp , _ := g .GoType (message , field )
29102928 fieldDeprecated := ""
29112929 if field .GetOptions ().GetDeprecated () {
2912- fieldDeprecated = deprecationComment
2930+ fieldDeprecated = deprecationComment + " \n "
29132931 }
29142932 dvalue := g .getterDefault (field , goTypeName , GoTypeToName (goTyp ))
29152933 if oneof {
@@ -2965,10 +2983,15 @@ func (g *Generator) generateMessage(message *Descriptor) {
29652983 }
29662984
29672985 fieldFullPath := fmt .Sprintf ("%s,%d,%d" , message .path , messageFieldPath , i )
2968- c , ok := g .makeComments (fieldFullPath )
2969- if ok {
2986+ c , _ := g .makeLeadingComments (fieldFullPath )
2987+ if strings . Contains ( c , " \n " ) {
29702988 c += "\n "
29712989 }
2990+ tc , _ := g .makeTrailingComments (fieldFullPath )
2991+ if strings .Contains (tc , "\n " ) {
2992+ tc += "\n "
2993+ }
2994+
29722995 rf := simpleField {
29732996 fieldCommon : fieldCommon {
29742997 goName : fieldName ,
@@ -2979,12 +3002,13 @@ func (g *Generator) generateMessage(message *Descriptor) {
29793002 fullPath : fieldFullPath ,
29803003 protoField : field ,
29813004 },
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 ,
29883012 }
29893013 var pf topLevelField = & rf
29903014
0 commit comments