Skip to content

Commit

Permalink
feat: support Array<> syntax in type decl (#1152)
Browse files Browse the repository at this point in the history
You can now use `Array<T>` as an alternative to `T[]` when declaring
types for relations in the Ory Permission Language.
  • Loading branch information
hperl committed Nov 28, 2022
1 parent 5abb188 commit c4c456b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
34 changes: 21 additions & 13 deletions internal/schema/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,28 +193,36 @@ func (p *parser) parseRelated() {
p.match(":", "{")
for !p.fatal {
switch item := p.next(); item.Typ {

case itemBraceRight:
return

case itemIdentifier, itemStringLiteral:
relation := item.Val
var types []ast.RelationType
p.match(":")
switch item := p.next(); item.Typ {
case itemIdentifier:
if item.Val == "SubjectSet" {
types = append(types, p.matchSubjectSet())
} else {
types = append(types, ast.RelationType{Namespace: item.Val})
p.addCheck(checkNamespaceExists(item))
}
case itemParenLeft:
types = append(types, p.parseTypeUnion()...)

switch item := p.next(); {
case item.Val == "Array":
p.match("<")
types = append(types, p.parseTypeUnion(itemAngledRight)...)
case item.Val == "SubjectSet":
types = append(types, p.matchSubjectSet())
p.match("[", "]", optional(","))
case item.Typ == itemParenLeft:
types = append(types, p.parseTypeUnion(itemParenRight)...)
p.match("[", "]", optional(","))
default:
types = append(types, ast.RelationType{Namespace: item.Val})
p.addCheck(checkNamespaceExists(item))
p.match("[", "]", optional(","))
}
p.match("[", "]", optional(","))

p.namespace.Relations = append(p.namespace.Relations, ast.Relation{
Name: relation,
Types: types,
})

default:
p.addFatal(item, "expected identifier or '}', got %s %q", item.Typ.String(), item.Val)
return
Expand All @@ -229,7 +237,7 @@ func (p *parser) matchSubjectSet() ast.RelationType {
return ast.RelationType{Namespace: namespace.Val, Relation: relation.Val}
}

func (p *parser) parseTypeUnion() (types []ast.RelationType) {
func (p *parser) parseTypeUnion(endToken itemType) (types []ast.RelationType) {
for !p.fatal {
var identifier item
p.match(&identifier)
Expand All @@ -240,7 +248,7 @@ func (p *parser) parseTypeUnion() (types []ast.RelationType) {
p.addCheck(checkNamespaceExists(identifier))
}
switch item := p.next(); item.Typ {
case itemParenRight:
case endToken:
return
case itemTypeUnion:
default:
Expand Down
6 changes: 3 additions & 3 deletions internal/schema/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ var parserTestCases = []struct {
class Folder implements Namespace {
related: {
parents: File[]
viewers: SubjectSet<Group, "members">[]
parents: Array<File>
viewers: Array<SubjectSet<Group, "members">>
}
permits = {
Expand All @@ -88,7 +88,7 @@ var parserTestCases = []struct {
class File implements Namespace {
related: {
parents: (File | Folder)[]
parents: Array<File | Folder>
viewers: (User | SubjectSet<Group, "members">)[]
"owners": (User | SubjectSet<Group, "members">)[]
siblings: File[]
Expand Down

0 comments on commit c4c456b

Please sign in to comment.