Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add composable schema test syntax parsing #2133

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/composableschemadsl/compiler/development_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestPositionToAstNode(t *testing.T) {
line: 2,
column: 6,
expected: []dslshape.NodeType{
dslshape.NodeTypeCaveatExpression,
dslshape.NodeTypeOpaqueBraceExpression,
dslshape.NodeTypeCaveatDefinition,
dslshape.NodeTypeFile,
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/composableschemadsl/compiler/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ func translateCaveatDefinition(tctx translationContext, defNode *dslNode) (*core
}

// caveat expression.
expressionStringNode, err := defNode.Lookup(dslshape.NodeCaveatDefinitionPredicateExpession)
expressionStringNode, err := defNode.Lookup(dslshape.NodeOpaqueBraceExpressionPredicateExpression)
if err != nil {
return nil, defNode.WithSourceErrorf(definitionName, "invalid expression: %w", err)
}

expressionString, err := expressionStringNode.GetString(dslshape.NodeCaveatExpressionPredicateExpression)
expressionString, err := expressionStringNode.GetString(dslshape.NodeOpaqueBraceExpressionPredicateExpression)
if err != nil {
return nil, defNode.WithSourceErrorf(expressionString, "invalid expression: %w", err)
}
Expand Down
35 changes: 35 additions & 0 deletions pkg/composableschemadsl/dslshape/dslshape.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ const (
NodeTypeCaveatTypeReference // A type reference for a caveat parameter.

NodeTypeImport

// A balanced brace-enclosed expression. Represents both caveat CELs and caveat JSON context.
NodeTypeOpaqueBraceExpression

NodeTypeTest
NodeTypeTestRelations
NodeTypeTestRelation
NodeTypeTestObject
NodeTypeTestPermission
NodeTypeTestNegativePermission
NodeTypeTestAssertions
NodeTypeTestAssertion
NodeTypeTestExpectedRelations
NodeTypeTestExpectedRelation
NodeTypeTestExpectedRelationSource
)

const (
Expand Down Expand Up @@ -198,4 +213,24 @@ const (
NodeImportPredicateSource = "import-source"
NodeImportPredicatePathSegment = "path-segment"
NodeImportPredicateDefinitionName = "imported-definition"

NodeTestPredicateName = "test-name"

NodeTestPredicateSubject = "subject"
NodeTestPredicateObject = "object"
NodeTestPredicateRelation = "relation"
NodeTestPredicatePermission = "permission"
NodeTestPredicateAssertionType = "assertion-type"
NodeTestPredicateCaveatName = "caveat-name"
NodeTestPredicateCaveatContext = "caveat-context"
NodeTestPredicateExpirationTime = "expiration-time"

NodeTestObjectPredicateObjectType = "object-type"
NodeTestObjectPredicateObjectID = "object-id"
// Used for both positive and negative permissions
NodeTestRelationPermissionPredicateName = "permission-name"

NodeTestExpectedRelationSourcePredicateCaveatAnnotation = "caveat-annotation"

NodeOpaqueBraceExpressionPredicateExpression = "expression"
)
14 changes: 14 additions & 0 deletions pkg/composableschemadsl/lexer/lex_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ var keywords = map[string]struct{}{
"import": {},
"all": {},
"any": {},
// caveat syntax
"and": {},
"expiration": {},
// Test keywords
"test": {},
"relationships": {},
"assertions": {},
"expected": {},
"is": {},
"of": {},
"not": {},
"conditional": {},
"when": {},
"for": {},
}

// IsKeyword returns whether the specified input string is a reserved keyword.
Expand Down
13 changes: 13 additions & 0 deletions pkg/composableschemadsl/lexer/lex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ var lexerTests = []lexerTest{
{"keyword", "import", []Lexeme{{TokenTypeKeyword, 0, "import", ""}, tEOF}},
{"keyword", "all", []Lexeme{{TokenTypeKeyword, 0, "all", ""}, tEOF}},
{"keyword", "nil", []Lexeme{{TokenTypeKeyword, 0, "nil", ""}, tEOF}},
{"keyword", "and", []Lexeme{{TokenTypeKeyword, 0, "and", ""}, tEOF}},
// Test keywords
{"keyword", "test", []Lexeme{{TokenTypeKeyword, 0, "test", ""}, tEOF}},
{"keyword", "relationships", []Lexeme{{TokenTypeKeyword, 0, "relationships", ""}, tEOF}},
{"keyword", "assertions", []Lexeme{{TokenTypeKeyword, 0, "assertions", ""}, tEOF}},
{"keyword", "expected", []Lexeme{{TokenTypeKeyword, 0, "expected", ""}, tEOF}},
{"keyword", "is", []Lexeme{{TokenTypeKeyword, 0, "is", ""}, tEOF}},
{"keyword", "of", []Lexeme{{TokenTypeKeyword, 0, "of", ""}, tEOF}},
{"keyword", "not", []Lexeme{{TokenTypeKeyword, 0, "not", ""}, tEOF}},
{"keyword", "conditional", []Lexeme{{TokenTypeKeyword, 0, "conditional", ""}, tEOF}},
{"keyword", "when", []Lexeme{{TokenTypeKeyword, 0, "when", ""}, tEOF}},
{"keyword", "for", []Lexeme{{TokenTypeKeyword, 0, "for", ""}, tEOF}},

{"identifier", "define", []Lexeme{{TokenTypeIdentifier, 0, "define", ""}, tEOF}},
{"typepath", "foo/bar", []Lexeme{
{TokenTypeIdentifier, 0, "foo", ""},
Expand Down
Loading
Loading