Skip to content
This repository has been archived by the owner on Jan 5, 2019. It is now read-only.

Commit

Permalink
allow oneof and service to have option, issue emicklei#43 (emicklei#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
emicklei authored Mar 2, 2018
1 parent 04b3b05 commit efd3625
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
8 changes: 8 additions & 0 deletions oneof.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ func (o *Oneof) parse(p *Parser) error {
return err
}
o.Elements = append(o.Elements, g)
case tOPTION:
opt := new(Option)
opt.Position = pos
opt.Comment, o.Elements = takeLastCommentIfEndsOnLine(o.elements(), pos.Line-1)
if err := opt.parse(p); err != nil {
return err
}
o.Elements = append(o.Elements, opt)
case tSEMICOLON:
maybeScanInlineComment(p, o)
// continue
Expand Down
17 changes: 17 additions & 0 deletions oneof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,20 @@ func TestFieldOneofImported(t *testing.T) {
t.Errorf("got [%v] want [%v]", got, want)
}
}

func TestOneOfWithOption(t *testing.T) {
src := `oneof AnOneof {
option (oneof_opt1) = -99;
int32 oneof_field = 2;
}`
p := newParserOn(src)
p.next()
oneof := new(Oneof)
err := oneof.parse(p)
if err != nil {
t.Fatal(err)
}
if got, want := oneof.Elements[0].(*Option).Name, "(oneof_opt1)"; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
}
8 changes: 8 additions & 0 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ func (s *Service) parse(p *Parser) error {
if com := mergeOrReturnComment(s.Elements, lit, pos); com != nil { // not merged?
s.Elements = append(s.Elements, com)
}
case tOPTION:
opt := new(Option)
opt.Position = pos
opt.Comment, s.Elements = takeLastCommentIfEndsOnLine(s.elements(), pos.Line-1)
if err := opt.parse(p); err != nil {
return err
}
s.Elements = append(s.Elements, opt)
case tRPC:
rpc := new(RPC)
rpc.Position = pos
Expand Down
16 changes: 16 additions & 0 deletions service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,19 @@ func TestRPCWithOptionAggregateSyntax(t *testing.T) {
t.Errorf("got len Options %v want %v", got, want)
}
}

func TestServiceWithOption(t *testing.T) {
src := `service AnyService {
option secure = true;
}`
p := newParserOn(src)
p.next()
svc := new(Service)
err := svc.parse(p)
if err != nil {
t.Fatal(err)
}
if got, want := svc.Elements[0].(*Option).Name, "secure"; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
}

0 comments on commit efd3625

Please sign in to comment.