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

feat: support Array<> syntax in type decl #1152

Merged
merged 2 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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