Skip to content

Commit

Permalink
issue #48 : allow multiple enum value options + field.ValueOption -> …
Browse files Browse the repository at this point in the history
…field.Elements
  • Loading branch information
Ernest Micklei committed Jan 3, 2018
1 parent 1ed9ecb commit a044f3b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
29 changes: 17 additions & 12 deletions enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ type EnumField struct {
Comment *Comment
Name string
Integer int
ValueOption *Option
Elements []Visitee // such as Option and Comment
InlineComment *Comment
}

Expand Down Expand Up @@ -157,17 +157,22 @@ func (f *EnumField) parse(p *Parser) error {
f.Integer = i
pos, tok, lit = p.next()
if tok == tLEFTSQUARE {
o := new(Option)
o.Position = pos
o.IsEmbedded = true
err := o.parse(p)
if err != nil {
return err
}
f.ValueOption = o
pos, tok, lit = p.next()
if tok != tRIGHTSQUARE {
return p.unexpected(lit, "option closing ]", f)
for {
o := new(Option)
o.Position = pos
o.IsEmbedded = true
err := o.parse(p)
if err != nil {
return err
}
f.Elements = append(f.Elements, o)
pos, tok, lit = p.next()
if tok == tCOMMA {
continue
}
if tok == tRIGHTSQUARE {
break
}
}
}
if tSEMICOLON == tok {
Expand Down
11 changes: 8 additions & 3 deletions enum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ enum EnumAllowingAlias {
STARTED = 1;
RUNNING = 2 [(custom_option) = "hello world"];
NEG = -42;
SOMETHING_FOO = 0 [
(bar.enum_value_option) = true,
(bar.enum_value_dep_option) = { hello: 1 }
];
}`
p := newParserOn(proto)
pr, err := p.Parse()
Expand All @@ -44,7 +48,7 @@ enum EnumAllowingAlias {
if got, want := len(enums), 1; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
if got, want := len(enums[0].Elements), 5; got != want {
if got, want := len(enums[0].Elements), 6; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
if got, want := enums[0].Comment != nil, true; got != want {
Expand All @@ -67,10 +71,11 @@ enum EnumAllowingAlias {
if got, want := ef3.Integer, 2; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
if got, want := ef3.ValueOption.Name, "(custom_option)"; got != want {
ef3opt := ef3.Elements[0].(*Option)
if got, want := ef3opt.Name, "(custom_option)"; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
if got, want := ef3.ValueOption.Constant.Source, "hello world"; got != want {
if got, want := ef3opt.Constant.Source, "hello world"; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
if got, want := ef3.Position.Line, 7; got != want {
Expand Down

0 comments on commit a044f3b

Please sign in to comment.