Skip to content

Commit

Permalink
fix Issue #8, aggregate option syntax case
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernest Micklei authored and Ernest Micklei committed Sep 7, 2017
1 parent eb711a8 commit c27dc4d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
cmd/proto2xsd/sample.proto
cmd/proto2xsd/proto2xsd
cmd/protofmt/protofmt
cmd/protofmt/protofmt
debug.test
21 changes: 15 additions & 6 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,22 @@ func (o *Option) parseAggregate(p *Parser) error {
o.AggregatedConstants = []*NamedLiteral{}
for {
tok, lit := p.scanIgnoreWhitespace()
if tRIGHTSQUARE == tok {
p.unscan()
// caller has checked for open square ; will consume rightsquare, rightcurly and semicolon
return nil
}
if tRIGHTCURLY == tok {
break
continue
}
if tSEMICOLON == tok {
return nil
}
if tCOMMA == tok {
if len(o.AggregatedConstants) == 0 {
return p.unexpected(lit, "non-empty option aggregate key", o)
}
continue
}
if tIDENT != tok {
return p.unexpected(lit, "option aggregate key", o)
Expand All @@ -186,9 +200,4 @@ func (o *Option) parseAggregate(p *Parser) error {
}
o.AggregatedConstants = append(o.AggregatedConstants, &NamedLiteral{Name: key, Literal: l})
}
tok, lit := p.scanIgnoreWhitespace()
if tSEMICOLON != tok {
return p.unexpected(lit, "option aggregate end ;", o)
}
return nil
}
39 changes: 39 additions & 0 deletions option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,42 @@ option Help = "me"; // inline`
t.Fatalf("got [%v] want [%v]", got, want)
}
}

func TestIssue8(t *testing.T) {
proto := `
// usage:
message Bar {
// alternative aggregate syntax (uses TextFormat):
int32 b = 2 [(foo_options) = {
opt1: 123,
opt2: "baz"
}];
}
`
p := newParserOn(proto)
pr, err := p.Parse()
if err != nil {
t.Fatal(err)
}
o := pr.Elements[0].(*Message)
f := o.Elements[0].(*NormalField)
if got, want := len(f.Options), 1; got != want {
t.Fatalf("got [%v] want [%v]", got, want)
}
ac := f.Options[0].AggregatedConstants
if got, want := len(ac), 2; got != want {
t.Fatalf("got [%v] want [%v]", got, want)
}
if got, want := ac[0].Name, "opt1"; got != want {
t.Fatalf("got [%v] want [%v]", got, want)
}
if got, want := ac[1].Name, "opt2"; got != want {
t.Fatalf("got [%v] want [%v]", got, want)
}
if got, want := ac[0].Source, "123"; got != want {
t.Fatalf("got [%v] want [%v]", got, want)
}
if got, want := ac[1].Source, "baz"; got != want {
t.Fatalf("got [%v] want [%v]", got, want)
}
}

0 comments on commit c27dc4d

Please sign in to comment.