Skip to content

Commit

Permalink
feat: type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
hperl committed Jul 4, 2022
1 parent 2967c52 commit 5a2a501
Show file tree
Hide file tree
Showing 9 changed files with 320 additions and 60 deletions.
6 changes: 6 additions & 0 deletions internal/namespace/ast/ast_definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ package ast
type (
Relation struct {
Name string `json:"name"`
Types []RelationType `json:"types,omitempty"`
UsersetRewrite *UsersetRewrite `json:"rewrite,omitempty"`
}

RelationType struct {
Namespace string `json:"namespace"`
Relation string `json:"relation,omitempty"` // optional
}

UsersetRewrite struct {
Operation SetOperation `json:"set_operation"`
Children []Child `json:"children"`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,47 @@
{
"File": [
{
"name": "parents"
"name": "parents",
"types": [
{
"namespace": "File"
},
{
"namespace": "Folder"
}
]
},
{
"name": "viewers"
"name": "viewers",
"types": [
{
"namespace": "User"
},
{
"namespace": "Group",
"relation": "members"
}
]
},
{
"name": "owners"
"name": "owners",
"types": [
{
"namespace": "User"
},
{
"namespace": "Group",
"relation": "members"
}
]
},
{
"name": "siblings"
"name": "siblings",
"types": [
{
"namespace": "File"
}
]
},
{
"name": "view",
Expand Down Expand Up @@ -76,10 +107,21 @@
],
"Folder": [
{
"name": "parents"
"name": "parents",
"types": [
{
"namespace": "File"
}
]
},
{
"name": "viewers"
"name": "viewers",
"types": [
{
"namespace": "Group",
"relation": "members"
}
]
},
{
"name": "view",
Expand All @@ -95,12 +137,25 @@
],
"Group": [
{
"name": "members"
"name": "members",
"types": [
{
"namespace": "User"
},
{
"namespace": "Group"
}
]
}
],
"User": [
{
"name": "manager"
"name": "manager",
"types": [
{
"namespace": "User"
}
]
}
]
}
34 changes: 32 additions & 2 deletions internal/schema/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import (
"github.com/ory/x/snapshotx"
)

var lexerTestCases = []struct{ name, input string }{
var lexingErrorTestCases = []struct{ name, input string }{
{"open comment", "/*"},
{"open string literal", "'"},
{"non-token", "ü"},
}

var lexableTestCases = []struct{ name, input string }{
{"empty", ""},
{"single class", `
class name implements Namespace {
Expand Down Expand Up @@ -75,7 +81,7 @@ class X implements Namespace {

func TestLexer(t *testing.T) {
t.Run("suite=snapshots", func(t *testing.T) {
for _, tc := range lexerTestCases {
for _, tc := range lexableTestCases {
t.Run(tc.name, func(t *testing.T) {
l := Lex(tc.name, tc.input)
var items []string
Expand All @@ -96,4 +102,28 @@ func TestLexer(t *testing.T) {
})
}
})

t.Run("suite=errors", func(t *testing.T) {
for _, tc := range lexingErrorTestCases {
t.Run(tc.name, func(t *testing.T) {
l := Lex(tc.name, tc.input)
var items []string
for {
item := l.nextItem()
items = append(items, item.String())

if item.Typ == itemError {
break
}
if item.Typ == itemEOF {
t.Fatal("reached EOF, but expected error")
break
}
}
l.next()
t.Logf("Tokens:\n%s", strings.Join(items, "\n"))
})
}

})
}
11 changes: 11 additions & 0 deletions internal/schema/limits.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package schema

const (
// tupleToUsersetTypeCheckMaxDepth Controls the maximum number of recursions
// for looking up the types of SubjectSet<Namespace, "relation">.
tupleToUsersetTypeCheckMaxDepth = 10

// expressionNestingMaxDepth is the maximum number of nested '(' in a single
// 'permits'.
expressionNestingMaxDepth = 10
)
30 changes: 23 additions & 7 deletions internal/schema/parse_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,28 @@ type sourcePosition struct {
line, col int
}

func max(a, b int) int {
if a > b {
return a
}
return b
}
func min(a, b int) int {
if a < b {
return a
}
return b
}

func (e *ParseError) Error() string {
var s strings.Builder
start := e.toSrcPos(e.item.Start)
end := e.toSrcPos(e.item.End)
rows := e.rows()
startLineIdx := max(start.line-2, 0)
errorLineIdx := max(start.line-1, 0)

s.WriteString(fmt.Sprintf("parse error from %d:%d to %d:%d: %s\n\n",
s.WriteString(fmt.Sprintf("error from %d:%d to %d:%d: %s\n\n",
start.line, start.col,
end.line, end.col,
e.msg))
Expand All @@ -31,11 +46,11 @@ func (e *ParseError) Error() string {
return s.String()
}

for line := start.line - 1; line < start.line+1; line++ {
s.WriteString(fmt.Sprintf("%4d | %s\n", line, rows[line-1]))
for line := startLineIdx; line <= errorLineIdx; line++ {
s.WriteString(fmt.Sprintf("%4d | %s\n", line, rows[line]))
}
s.WriteString(" ")
for i, r := range rows[start.line-1] {
for i, r := range rows[errorLineIdx] {
switch {
case start.col == i:
s.WriteRune('^')
Expand All @@ -49,9 +64,10 @@ func (e *ParseError) Error() string {
}
s.WriteRune('\n')

s.WriteString(fmt.Sprintf("%4d | %s\n", start.line+1, rows[start.line]))

s.WriteRune('\n')
if errorLineIdx+1 < len(rows) {
s.WriteString(fmt.Sprintf("%4d | %s\n", errorLineIdx, rows[errorLineIdx+1]))
s.WriteRune('\n')
}

return s.String()
}
Expand Down
Loading

0 comments on commit 5a2a501

Please sign in to comment.