Skip to content

Commit

Permalink
feat: generates trailing comments of message field
Browse files Browse the repository at this point in the history
close gogo#764
  • Loading branch information
hitzhangjie committed Feb 23, 2023
1 parent f67b897 commit a38f6f9
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 26 deletions.
19 changes: 19 additions & 0 deletions .vscode/bookmarks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"files": [
{
"path": "protoc-gen-gogo/generator/generator.go",
"bookmarks": [
{
"line": 2636,
"column": 20,
"label": ""
},
{
"line": 2649,
"column": 8,
"label": ""
}
]
}
]
}
3 changes: 2 additions & 1 deletion protoc-gen-gogo/descriptor/descriptor.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 48 additions & 25 deletions protoc-gen-gogo/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

/*
The code generator for the plugin for the Google protocol buffer compiler.
It generates Go code from the protocol buffer description files read by the
main routine.
The code generator for the plugin for the Google protocol buffer compiler.
It generates Go code from the protocol buffer description files read by the
main routine.
*/
package generator

Expand Down Expand Up @@ -987,7 +987,7 @@ func wrapImported(file *FileDescriptor, g *Generator) (sl []*ImportedDescriptor)
func extractComments(file *FileDescriptor) {
file.comments = make(map[string]*descriptor.SourceCodeInfo_Location)
for _, loc := range file.GetSourceCodeInfo().GetLocation() {
if loc.LeadingComments == nil {
if loc.LeadingComments == nil && loc.TrailingComments == nil {
continue
}
var p []string
Expand Down Expand Up @@ -1321,22 +1321,39 @@ func (g *Generator) PrintComments(path string) bool {
if !g.writeOutput {
return false
}
if c, ok := g.makeComments(path); ok {
if c, ok := g.makeLeadingComments(path); ok {
g.P(c)
return true
}
return false
}

// makeComments generates the comment string for the field, no "\n" at the end
func (g *Generator) makeComments(path string) (string, bool) {
// makeLeadingComments generates the comment string for the field, no "\n" at the end
func (g *Generator) makeLeadingComments(path string) (string, bool) {
loc, ok := g.file.comments[path]
if !ok {
comments := strings.TrimSuffix(loc.GetLeadingComments(), "\n")
if !ok || comments == "" {
return "", false
}
w := new(bytes.Buffer)
nl := ""
for _, line := range strings.Split(strings.TrimSuffix(loc.GetLeadingComments(), "\n"), "\n") {
for _, line := range strings.Split(comments, "\n") {
fmt.Fprintf(w, "%s//%s", nl, line)
nl = "\n"
}
return w.String(), true
}

// makeTrailingComments generates the trailing comment string for the field, no "\n" at the end
func (g *Generator) makeTrailingComments(path string) (string, bool) {
loc, ok := g.file.comments[path]
comments := strings.TrimSuffix(loc.GetTrailingComments(), "\n")
if !ok || comments == "" {
return "", false
}
w := new(bytes.Buffer)
nl := ""
for _, line := range strings.Split(comments, "\n") {
fmt.Fprintf(w, "%s//%s", nl, line)
nl = "\n"
}
Expand Down Expand Up @@ -1605,6 +1622,7 @@ func (g *Generator) generateEnum(enum *EnumDescriptor) {
// The tag is a string like "varint,2,opt,name=fieldname,def=7" that
// identifies details of the field for the protocol buffer marshaling and unmarshaling
// code. The fields are:
//
// wire encoding
// protocol tag number
// opt,req,rep for optional, required, or repeated
Expand All @@ -1613,6 +1631,7 @@ func (g *Generator) generateEnum(enum *EnumDescriptor) {
// enum= the name of the enum type if it is an enum-typed field.
// proto3 if this field is in a proto3 message
// def= string representation of the default value, if any.
//
// The default value must be in a representation that can be used at run-time
// to generate the default value. Thus bools become 0 and 1, for instance.
func (g *Generator) goTag(message *Descriptor, field *descriptor.FieldDescriptorProto, wiretype string) string {
Expand Down Expand Up @@ -2179,17 +2198,18 @@ func (f *fieldCommon) getGoType() string {
// simpleField is not weak, not a oneof, not an extension. Can be required, optional or repeated.
type simpleField struct {
fieldCommon
protoTypeName string // Proto type name, empty if primitive, e.g. ".google.protobuf.Duration"
protoType descriptor.FieldDescriptorProto_Type // Actual type enum value, e.g. descriptor.FieldDescriptorProto_TYPE_FIXED64
deprecated string // Deprecation comment, if any, e.g. "// Deprecated: Do not use."
getterDef string // Default for getters, e.g. "nil", `""` or "Default_MessageType_FieldName"
protoDef string // Default value as defined in the proto file, e.g "yoshi" or "5"
comment string // The full comment for the field, e.g. "// Useful information"
protoTypeName string // Proto type name, empty if primitive, e.g. ".google.protobuf.Duration"
protoType descriptor.FieldDescriptorProto_Type // Actual type enum value, e.g. descriptor.FieldDescriptorProto_TYPE_FIXED64
deprecated string // Deprecation comment, if any, e.g. "// Deprecated: Do not use."
getterDef string // Default for getters, e.g. "nil", `""` or "Default_MessageType_FieldName"
protoDef string // Default value as defined in the proto file, e.g "yoshi" or "5"
comment string // The full comment for the field, e.g. "// Useful information"
trailingComment string // The trailing comment for the field, e.g. "fieldName fieldType // Useful information"
}

// decl prints the declaration of the field in the struct (if any).
func (f *simpleField) decl(g *Generator, mc *msgCtx) {
g.P(f.comment, Annotate(mc.message.file, f.fullPath, f.goName), "\t", f.goType, "\t`", f.tags, "`", f.deprecated)
g.P(f.comment, f.deprecated, Annotate(mc.message.file, f.fullPath, f.goName), "\t", f.goType, "\t`", f.tags, "`", f.trailingComment)
}

// getter prints the getter for the field.
Expand Down Expand Up @@ -2870,7 +2890,7 @@ func (g *Generator) generateMessage(message *Descriptor) {
// This is the first field of a oneof we haven't seen before.
// Generate the union field.
oneofFullPath := fmt.Sprintf("%s,%d,%d", message.path, messageOneofPath, *field.OneofIndex)
c, ok := g.makeComments(oneofFullPath)
c, ok := g.makeLeadingComments(oneofFullPath)
if ok {
c += "\n//\n"
}
Expand Down Expand Up @@ -2909,7 +2929,7 @@ func (g *Generator) generateMessage(message *Descriptor) {
goTyp, _ := g.GoType(message, field)
fieldDeprecated := ""
if field.GetOptions().GetDeprecated() {
fieldDeprecated = deprecationComment
fieldDeprecated = deprecationComment + "\n"
}
dvalue := g.getterDefault(field, goTypeName, GoTypeToName(goTyp))
if oneof {
Expand Down Expand Up @@ -2965,10 +2985,12 @@ func (g *Generator) generateMessage(message *Descriptor) {
}

fieldFullPath := fmt.Sprintf("%s,%d,%d", message.path, messageFieldPath, i)
c, ok := g.makeComments(fieldFullPath)
c, ok := g.makeLeadingComments(fieldFullPath)
if ok {
c += "\n"
}
tc, _ := g.makeTrailingComments(fieldFullPath)

rf := simpleField{
fieldCommon: fieldCommon{
goName: fieldName,
Expand All @@ -2979,12 +3001,13 @@ func (g *Generator) generateMessage(message *Descriptor) {
fullPath: fieldFullPath,
protoField: field,
},
protoTypeName: field.GetTypeName(),
protoType: *field.Type,
deprecated: fieldDeprecated,
getterDef: dvalue,
protoDef: field.GetDefaultValue(),
comment: c,
protoTypeName: field.GetTypeName(),
protoType: *field.Type,
deprecated: fieldDeprecated,
getterDef: dvalue,
protoDef: field.GetDefaultValue(),
comment: c,
trailingComment: tc,
}
var pf topLevelField = &rf

Expand Down

0 comments on commit a38f6f9

Please sign in to comment.