Skip to content

Commit 0a2ca33

Browse files
authored
Merge pull request #123 from jaredzhou/trailing-commas
add feather: trailing commas
2 parents 3fcc669 + a33a9d5 commit 0a2ca33

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

internal/parser/cedar_unmarshal.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -373,16 +373,19 @@ func (p *parser) action(policy *ast.Policy) error {
373373
func (p *parser) entlist() ([]types.EntityUID, error) {
374374
var res []types.EntityUID
375375
for p.peek().Text != "]" {
376-
if len(res) > 0 {
377-
if err := p.exact(","); err != nil {
378-
return nil, err
379-
}
380-
}
381376
e, err := p.entity()
382377
if err != nil {
383378
return nil, err
384379
}
385380
res = append(res, e)
381+
switch p.peek().Text {
382+
case ",":
383+
p.advance()
384+
case "]":
385+
// do nothing, exit switch
386+
default:
387+
return nil, p.errorf("got %v want ,", p.peek().Text)
388+
}
386389
}
387390
return res, nil
388391
}
@@ -851,16 +854,19 @@ func (p *parser) entityOrExtFun(prefix string) (ast.Node, error) {
851854
func (p *parser) expressions(endOfListMarker string) ([]ast.Node, error) {
852855
var res []ast.Node
853856
for p.peek().Text != endOfListMarker {
854-
if len(res) > 0 {
855-
if err := p.exact(","); err != nil {
856-
return res, err
857-
}
858-
}
859857
e, err := p.expression()
860858
if err != nil {
861859
return res, err
862860
}
863861
res = append(res, e)
862+
switch p.peek().Text {
863+
case ",":
864+
p.advance()
865+
case endOfListMarker:
866+
// do nothing, exit switch
867+
default:
868+
return nil, p.errorf("got %v want ,", p.peek().Text)
869+
}
864870
}
865871
return res, nil
866872
}
@@ -875,11 +881,6 @@ func (p *parser) record() (ast.Node, error) {
875881
p.advance()
876882
return ast.Record(elements), nil
877883
}
878-
if len(elements) > 0 {
879-
if err := p.exact(","); err != nil {
880-
return res, err
881-
}
882-
}
883884
k, v, err := p.recordEntry()
884885
if err != nil {
885886
return res, err
@@ -890,6 +891,14 @@ func (p *parser) record() (ast.Node, error) {
890891
}
891892
known.Add(k)
892893
elements = append(elements, ast.Pair{Key: types.String(k), Value: v})
894+
switch p.peek().Text {
895+
case ",":
896+
p.advance()
897+
case "}":
898+
// do nothing, exit switch
899+
default:
900+
return res, p.errorf("got %v want ,", p.peek().Text)
901+
}
893902
}
894903
}
895904

internal/parser/cedar_unmarshal_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,26 @@ func TestParseTrailingCommas(t *testing.T) {
503503
"permit (principal, action, resource,);",
504504
ast.Permit(),
505505
},
506+
{
507+
"expr list with trailing comma",
508+
`permit (principal, action, resource) when {[1,2,].isEmpty() };`,
509+
ast.Permit().When(ast.Set(ast.Long(1), ast.Long(2)).IsEmpty()),
510+
},
511+
{
512+
"record with trailing comma",
513+
`permit (principal, action, resource) when {{"key":1,} has key };`,
514+
ast.Permit().When(ast.Record(ast.Pairs{ast.Pair{Key: types.String("key"), Value: ast.Long(1)}}).Has("key")),
515+
},
516+
{
517+
"entity list with trailing comma",
518+
`permit (principal, action, resource) when {User::"alice" in [User::"bob",] };`,
519+
ast.Permit().When(ast.EntityUID(types.Ident("User"), types.String("alice")).In(ast.Set(ast.EntityUID(types.Ident("User"), types.String("bob"))))),
520+
},
521+
{
522+
"extension call with trailing comma",
523+
`permit (principal, action, resource) when { ip("1.2.3.4",) };`,
524+
ast.Permit().When(ast.ExtensionCall("ip", ast.String("1.2.3.4"))),
525+
},
506526
}
507527

508528
for _, tt := range parseTests {

0 commit comments

Comments
 (0)