Skip to content

Commit

Permalink
feat: start implementing multi-line formatting for permission express…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
theoriginalstove committed Sep 24, 2024
1 parent 49c27f2 commit 59802c7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/dsl/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,11 @@ func (p *Parser) parseExpression(precedence int) (ast.Expression, error) {
var exp ast.Expression
var err error

if p.currentTokenIs(token.NEWLINE) {
// advance to the next token
p.next()
}

if p.currentTokenIs(token.LP) {
p.next() // Consume the left parenthesis.
exp, err = p.parseExpression(LOWEST)
Expand Down
47 changes: 47 additions & 0 deletions pkg/dsl/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -849,5 +849,52 @@ var _ = Describe("parser", func() {
Expect(es.Expression.(*ast.InfixExpression).Right.(*ast.InfixExpression).Left.(*ast.Identifier).String()).Should(Equal("parent.admin"))
Expect(es.Expression.(*ast.InfixExpression).Right.(*ast.InfixExpression).Right.(*ast.Identifier).String()).Should(Equal("parent.member"))
})

It("Case 24 - Multi-line Permission Expression w/ Rule", func() {
pr := NewParser(`
entity account {
relation owner @user
attribute balance float
permission withdraw = check_balance(request.amount, balance) and
owner
}
rule check_balance(amount float, balance float) {
balance >= amount && amount <= 5000
}
`)

schema, err := pr.Parse()
Expect(err).ShouldNot(HaveOccurred())

st := schema.Statements[0].(*ast.EntityStatement)

Expect(st.Name.Literal).Should(Equal("account"))

r1 := st.RelationStatements[0].(*ast.RelationStatement)
Expect(r1.Name.Literal).Should(Equal("owner"))

for _, a := range r1.RelationTypes {
Expect(a.Type.Literal).Should(Equal("user"))
}

a1 := st.AttributeStatements[0].(*ast.AttributeStatement)
Expect(a1.Name.Literal).Should(Equal("balance"))
Expect(a1.AttributeType.Type.Literal).Should(Equal("float"))

p1 := st.PermissionStatements[0].(*ast.PermissionStatement)
Expect(p1.Name.Literal).Should(Equal("withdraw"))

es1 := p1.ExpressionStatement.(*ast.ExpressionStatement)

Expect(es1.Expression.(*ast.InfixExpression).Left.(*ast.Call).String()).Should(Equal("check_balance(request.amount, balance)"))
Expect(es1.Expression.(*ast.InfixExpression).Right.(*ast.Identifier).String()).Should(Equal("owner"))

rs1 := schema.Statements[1].(*ast.RuleStatement)

Expect(rs1.Name.Literal).Should(Equal("check_balance"))
Expect(rs1.Expression).Should(Equal("\nbalance >= amount && amount <= 5000\n\t\t"))
})
})
})

0 comments on commit 59802c7

Please sign in to comment.