Skip to content

Commit 378f896

Browse files
authored
Merge pull request #119 from gregory-nisbet/2025-11-01-trailing-comma
Support trailing comma after a resource.
2 parents 3baa7fb + b26eaae commit 378f896

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

internal/parser/cedar_unmarshal.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ func (p *Policy) fromCedar(parser *parser) error {
112112
if err = parser.resource(newPolicy); err != nil {
113113
return err
114114
}
115+
parser.skipAtMostOnce(",")
115116
if err = parser.exact(")"); err != nil {
116117
return err
117118
}
@@ -155,6 +156,13 @@ func (p *parser) exact(tok string) error {
155156
return nil
156157
}
157158

159+
func (p *parser) skipAtMostOnce(tok string) {
160+
t := p.peek()
161+
if t.Text == tok {
162+
p.advance()
163+
}
164+
}
165+
158166
func (p *parser) errorf(s string, args ...interface{}) error {
159167
var t Token
160168
if p.pos < len(p.tokens) {

internal/parser/cedar_unmarshal_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ func TestParsePolicySet(t *testing.T) {
590590
policyStr := []byte(`permit (
591591
principal,
592592
action,
593-
resource
593+
resource,
594594
);`)
595595

596596
var policies parser.PolicySlice

policy_test.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func TestPolicyCedar(t *testing.T) {
7171
t.Parallel()
7272

7373
// Taken from https://docs.cedarpolicy.com/policies/syntax-policy.html
74-
policyStr := `permit (
74+
const policyStr = `permit (
7575
principal,
7676
action == Action::"editPhoto",
7777
resource
@@ -84,6 +84,29 @@ when { resource.owner == principal };`
8484
testutil.Equals(t, string(policy.MarshalCedar()), policyStr)
8585
}
8686

87+
func TestPolicyCedar_TrailingComma(t *testing.T) {
88+
t.Parallel()
89+
90+
const policyStr = `permit (
91+
principal,
92+
action == Action::"editPhoto",
93+
resource
94+
)
95+
when { resource.owner == principal };`
96+
97+
const policyStrTrailingComma = `permit (
98+
principal,
99+
action == Action::"editPhoto",
100+
resource,
101+
)
102+
when { resource.owner == principal };`
103+
104+
var policy cedar.Policy
105+
testutil.OK(t, policy.UnmarshalCedar([]byte(policyStrTrailingComma)))
106+
107+
testutil.Equals(t, string(policy.MarshalCedar()), policyStr)
108+
}
109+
87110
func TestPolicyAST(t *testing.T) {
88111
t.Parallel()
89112

0 commit comments

Comments
 (0)