Skip to content

Commit d295500

Browse files
committed
feat: generates trailing comments of message field
close gogo#764
1 parent f67b897 commit d295500

File tree

1 file changed

+45
-21
lines changed

1 file changed

+45
-21
lines changed

protoc-gen-gogo/generator/generator.go

+45-21
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
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
*/
4242
package generator
4343

@@ -1321,15 +1321,30 @@ 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) {
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) {
13331348
loc, ok := g.file.comments[path]
13341349
if !ok {
13351350
return "", false
@@ -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.
16181635
func (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.
21802197
type 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).
21912209
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)
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
}
@@ -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)
2986+
c, ok := g.makeLeadingComments(fieldFullPath)
29692987
if ok {
29702988
c += "\n"
29712989
}
2990+
tc, ok := g.makeTrailingComments(fieldFullPath)
2991+
if ok {
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

Comments
 (0)