Skip to content

Commit

Permalink
Add PackageComments and SyntaxComments to FileDescriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudomuto committed Feb 23, 2018
1 parent c2cb350 commit 256d532
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 3 deletions.
Binary file modified fixtures/fileset.pb
Binary file not shown.
1 change: 1 addition & 0 deletions fixtures/todo.proto
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Top-level comments are attached to the syntax directive.
syntax = "proto3";

import "google/protobuf/any.proto";
Expand Down
3 changes: 3 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
enumCommentPath = 5
serviceCommentPath = 6
extensionCommentPath = 7
syntaxCommentPath = 12

// tag numbers in DescriptorProto
messageFieldCommentPath = 2 // field
Expand Down Expand Up @@ -59,6 +60,8 @@ func parseFile(ctx context.Context, fd *descriptor.FileDescriptorProto) *FileDes
comments: comments,
FileDescriptorProto: fd,
Comments: comments.Get(fmt.Sprintf("%d", packageCommentPath)),
PackageComments: comments.Get(fmt.Sprintf("%d", packageCommentPath)),
SyntaxComments: comments.Get(fmt.Sprintf("%d", syntaxCommentPath)),
}

fileCtx := ContextWithFileDescriptor(ctx, file)
Expand Down
3 changes: 2 additions & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ func (assert *ParserTest) SetupSuite() {

func (assert *ParserTest) TestFileParsing() {
assert.True(proto3.IsProto3())
assert.Contains(proto3.GetComments().String(), "The official documentation for the Todo API.\n\n")
assert.Equal("Top-level comments are attached to the syntax directive.", proto3.GetSyntaxComments().String())
assert.Contains(proto3.GetPackageComments().String(), "The official documentation for the Todo API.\n\n")
assert.Len(proto3.GetExtensions(), 0) // no extensions in proto3

assert.False(proto2.IsProto3())
Expand Down
15 changes: 13 additions & 2 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ type FileDescriptor struct {
comments Comments
*descriptor.FileDescriptorProto

Comments *Comment
Comments *Comment // Deprecated: see PackageComments
PackageComments *Comment
SyntaxComments *Comment

Enums []*EnumDescriptor
Extensions []*ExtensionDescriptor
Imports []*ImportedDescriptor
Expand All @@ -64,9 +67,17 @@ type FileDescriptor struct {
// IsProto3 returns whether or not this file is a proto3 file
func (f *FileDescriptor) IsProto3() bool { return f.GetSyntax() == "proto3" }

// GetComments returns the file's package comments
// GetComments returns the file's package comments.
//
// Deprecated: please see GetPackageComments
func (f *FileDescriptor) GetComments() *Comment { return f.Comments }

// GetPackageComments returns the file's package comments
func (f *FileDescriptor) GetPackageComments() *Comment { return f.PackageComments }

// GetSyntaxComments returns the file's syntax comments
func (f *FileDescriptor) GetSyntaxComments() *Comment { return f.SyntaxComments }

// GetEnums returns the top-level enumerations defined in this file
func (f *FileDescriptor) GetEnums() []*EnumDescriptor { return f.Enums }

Expand Down

0 comments on commit 256d532

Please sign in to comment.