From 59802c78313a013a1ce760d70dbea7450a43d650 Mon Sep 17 00:00:00 2001 From: Steven Turturo Date: Tue, 24 Sep 2024 15:10:25 -0700 Subject: [PATCH] feat: start implementing multi-line formatting for permission expressions --- pkg/dsl/parser/parser.go | 5 ++++ pkg/dsl/parser/parser_test.go | 47 +++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/pkg/dsl/parser/parser.go b/pkg/dsl/parser/parser.go index 64af42001..4e94a0e40 100644 --- a/pkg/dsl/parser/parser.go +++ b/pkg/dsl/parser/parser.go @@ -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) diff --git a/pkg/dsl/parser/parser_test.go b/pkg/dsl/parser/parser_test.go index f066c15af..b47b7be28 100644 --- a/pkg/dsl/parser/parser_test.go +++ b/pkg/dsl/parser/parser_test.go @@ -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")) + }) }) })