Skip to content

Commit

Permalink
fix issue #42 : inline comment on options + RPC.Options -> RPC.Elements
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernest Micklei committed Jan 4, 2018
1 parent 7b98812 commit 2fa54d3
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 7 deletions.
1 change: 1 addition & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func (o *Option) parseAggregate(p *Parser) error {
continue
}
if tSEMICOLON == tok {
p.nextPut(pos, tok, lit) // allow for inline comment parsing
return nil
}
if tCOMMA == tok {
Expand Down
24 changes: 24 additions & 0 deletions option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,27 @@ message Bar {
t.Fatalf("got [%v] want [%v]", got, want)
}
}

func TestNonPrimitiveOptionComment(t *testing.T) {
proto := `
// comment
option Help = { string_field: "value" }; // inline`
p := newParserOn(proto)
pr, err := p.Parse()
if err != nil {
t.Fatal(err)
}
o := pr.Elements[0].(*Option)
if got, want := o.Comment != nil, true; got != want {
t.Fatalf("got [%v] want [%v]", got, want)
}
if got, want := o.Comment.Lines[0], " comment"; got != want {
t.Fatalf("got [%v] want [%v]", got, want)
}
if got, want := o.InlineComment != nil, true; got != want {
t.Fatalf("got [%v] want [%v]", got, want)
}
if got, want := o.InlineComment.Lines[0], " inline"; got != want {
t.Fatalf("got [%v] want [%v]", got, want)
}
}
29 changes: 25 additions & 4 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ type RPC struct {
StreamsRequest bool
ReturnsType string
StreamsReturns bool
Options []*Option
Elements []Visitee
InlineComment *Comment
}

Expand Down Expand Up @@ -188,8 +188,14 @@ func (r *RPC) parse(p *Parser) error {
if tRIGHTCURLY == tok {
break
}
if tCOMMENT == tok {
// TODO put comment in the next option
if isComment(lit) {
if com := mergeOrReturnComment(r.elements(), lit, pos); com != nil { // not merged?
r.addElement(com)
continue
}
}
if tSEMICOLON == tok {
maybeScanInlineComment(p, r)
continue
}
if tOPTION != tok {
Expand All @@ -200,8 +206,23 @@ func (r *RPC) parse(p *Parser) error {
if err := o.parse(p); err != nil {
return err
}
r.Options = append(r.Options, o)
r.Elements = append(r.Elements, o)
}
}
return nil
}

// addElement is part of elementContainer
func (r *RPC) addElement(v Visitee) {
r.Elements = append(r.Elements, v)
}

// elements is part of elementContainer
func (r *RPC) elements() []Visitee {
return r.Elements
}

func (r *RPC) takeLastComment() (last *Comment) {
last, r.Elements = takeLastComment(r.Elements)
return
}
17 changes: 14 additions & 3 deletions service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ func TestRPCWithOptionAggregateSyntax(t *testing.T) {
proto := `service AccountService {
// CreateAccount
rpc CreateAccount (CreateAccount) returns (ServiceFault){
// test_ident
option (test_ident) = {
test: "test"
test2:"test2"
};
}; // inline test_ident
}
}`
pr, err := newParserOn(proto).Parse()
Expand All @@ -82,13 +83,23 @@ func TestRPCWithOptionAggregateSyntax(t *testing.T) {
t.Fatalf("got [%v] want [%v]", got, want)
}
rpc1 := srv.Elements[0].(*RPC)
if got, want := len(rpc1.Options), 1; got != want {
if got, want := len(rpc1.Elements), 2; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
opt := rpc1.Options[0]
com := rpc1.Elements[0].(*Comment)
if got, want := com.Message(), " test_ident"; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
opt := rpc1.Elements[1].(*Option)
if got, want := opt.Name, "(test_ident)"; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
if got, want := opt.InlineComment != nil, true; got != want {
t.Fatalf("got [%v] want [%v]", got, want)
}
if got, want := opt.InlineComment.Message(), " inline test_ident"; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
if got, want := len(opt.AggregatedConstants), 2; got != want {
t.Fatalf("got [%v] want [%v]", got, want)
}
Expand Down

2 comments on commit 2fa54d3

@bufdev
Copy link
Contributor

@bufdev bufdev commented on 2fa54d3 Jan 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change, I just updated to the latest master and my tests are broken. Can you roll this back and just mark the fields as deprecated/make sure they have the same behavior?

@bufdev
Copy link
Contributor

@bufdev bufdev commented on 2fa54d3 Jan 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

internal/format/log_visitor.go:140:31: element.Options undefined (type *proto.RPC has no field or method Options)
internal/format/middle_visitor.go:256:16: element.Options undefined (type *proto.RPC has no field or method Options)
internal/format/middle_visitor.go:266:27: element.Options undefined (type *proto.RPC has no field or method Options)
internal/lint/check_comments_no_c_style.go:119:31: element.Options undefined (type *proto.RPC has no field or method Options)

Please sign in to comment.