Skip to content

Commit a38f6f9

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

File tree

3 files changed

+69
-26
lines changed

3 files changed

+69
-26
lines changed

.vscode/bookmarks.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"files": [
3+
{
4+
"path": "protoc-gen-gogo/generator/generator.go",
5+
"bookmarks": [
6+
{
7+
"line": 2636,
8+
"column": 20,
9+
"label": ""
10+
},
11+
{
12+
"line": 2649,
13+
"column": 8,
14+
"label": ""
15+
}
16+
]
17+
}
18+
]
19+
}

protoc-gen-gogo/descriptor/descriptor.pb.go

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protoc-gen-gogo/generator/generator.go

+48-25
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

@@ -987,7 +987,7 @@ func wrapImported(file *FileDescriptor, g *Generator) (sl []*ImportedDescriptor)
987987
func 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,22 +1321,39 @@ 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+
comments := strings.TrimSuffix(loc.GetLeadingComments(), "\n")
1335+
if !ok || comments == "" {
13351336
return "", false
13361337
}
13371338
w := new(bytes.Buffer)
13381339
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") {
13401357
fmt.Fprintf(w, "%s//%s", nl, line)
13411358
nl = "\n"
13421359
}
@@ -1605,6 +1622,7 @@ func (g *Generator) generateEnum(enum *EnumDescriptor) {
16051622
// The tag is a string like "varint,2,opt,name=fieldname,def=7" that
16061623
// identifies details of the field for the protocol buffer marshaling and unmarshaling
16071624
// code. The fields are:
1625+
//
16081626
// wire encoding
16091627
// protocol tag number
16101628
// opt,req,rep for optional, required, or repeated
@@ -1613,6 +1631,7 @@ func (g *Generator) generateEnum(enum *EnumDescriptor) {
16131631
// enum= the name of the enum type if it is an enum-typed field.
16141632
// proto3 if this field is in a proto3 message
16151633
// def= string representation of the default value, if any.
1634+
//
16161635
// The default value must be in a representation that can be used at run-time
16171636
// to generate the default value. Thus bools become 0 and 1, for instance.
16181637
func (g *Generator) goTag(message *Descriptor, field *descriptor.FieldDescriptorProto, wiretype string) string {
@@ -2179,17 +2198,18 @@ func (f *fieldCommon) getGoType() string {
21792198
// simpleField is not weak, not a oneof, not an extension. Can be required, optional or repeated.
21802199
type simpleField struct {
21812200
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"
21882208
}
21892209

21902210
// decl prints the declaration of the field in the struct (if any).
21912211
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)
21932213
}
21942214

21952215
// getter prints the getter for the field.
@@ -2870,7 +2890,7 @@ func (g *Generator) generateMessage(message *Descriptor) {
28702890
// This is the first field of a oneof we haven't seen before.
28712891
// Generate the union field.
28722892
oneofFullPath := fmt.Sprintf("%s,%d,%d", message.path, messageOneofPath, *field.OneofIndex)
2873-
c, ok := g.makeComments(oneofFullPath)
2893+
c, ok := g.makeLeadingComments(oneofFullPath)
28742894
if ok {
28752895
c += "\n//\n"
28762896
}
@@ -2909,7 +2929,7 @@ func (g *Generator) generateMessage(message *Descriptor) {
29092929
goTyp, _ := g.GoType(message, field)
29102930
fieldDeprecated := ""
29112931
if field.GetOptions().GetDeprecated() {
2912-
fieldDeprecated = deprecationComment
2932+
fieldDeprecated = deprecationComment + "\n"
29132933
}
29142934
dvalue := g.getterDefault(field, goTypeName, GoTypeToName(goTyp))
29152935
if oneof {
@@ -2965,10 +2985,12 @@ func (g *Generator) generateMessage(message *Descriptor) {
29652985
}
29662986

29672987
fieldFullPath := fmt.Sprintf("%s,%d,%d", message.path, messageFieldPath, i)
2968-
c, ok := g.makeComments(fieldFullPath)
2988+
c, ok := g.makeLeadingComments(fieldFullPath)
29692989
if ok {
29702990
c += "\n"
29712991
}
2992+
tc, _ := g.makeTrailingComments(fieldFullPath)
2993+
29722994
rf := simpleField{
29732995
fieldCommon: fieldCommon{
29742996
goName: fieldName,
@@ -2979,12 +3001,13 @@ func (g *Generator) generateMessage(message *Descriptor) {
29793001
fullPath: fieldFullPath,
29803002
protoField: field,
29813003
},
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,
29883011
}
29893012
var pf topLevelField = &rf
29903013

0 commit comments

Comments
 (0)