Skip to content

Commit

Permalink
Wire parser to loader
Browse files Browse the repository at this point in the history
  • Loading branch information
ronenh committed Dec 1, 2023
1 parent 47bab34 commit 332492c
Show file tree
Hide file tree
Showing 11 changed files with 1,729 additions and 769 deletions.
2,093 changes: 1,616 additions & 477 deletions cache/expand_test.json

Large diffs are not rendered by default.

23 changes: 9 additions & 14 deletions cache/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ func TestPathMap(t *testing.T) {
c := cache.New(m)
require.NotNil(t, c)

pm, err := createPathMap(m)
require.NoError(t, err)
pm := createPathMap(m)
require.NotNil(t, pm)

// plot all paths for all roots.
Expand Down Expand Up @@ -88,7 +87,7 @@ func (pm PathMap) plotPaths(w io.Writer, or *model.ObjectRelation) {
}
}

func createPathMap(m *model.Model) (*PathMap, error) {
func createPathMap(m *model.Model) *PathMap {
pm := PathMap{}

// create roots
Expand Down Expand Up @@ -125,7 +124,7 @@ func createPathMap(m *model.Model) (*PathMap, error) {
}
}

return &pm, nil
return &pm
}

func expandPerm(m *model.Model, on model.ObjectName, pn model.PermissionName) []*model.ObjectRelation {
Expand All @@ -137,21 +136,17 @@ func expandPerm(m *model.Model, on model.ObjectName, pn model.PermissionName) []
}

for _, r := range p.Union {
result = append(result, resolve(m, on, model.RelationName(r)))
result = append(result, resolve(m, on, model.RelationName(r.RelOrPerm)))
}

for _, _ = range p.Intersection {
for range p.Intersection {
panic("not implemented")
}

if p.Exclusion != nil {
panic("not implemented")
}

if p.Arrow != nil {
panic("not implemented")
}

return result
}

Expand All @@ -166,7 +161,7 @@ func expandRel(m *model.Model, on model.ObjectName, rn model.RelationName) []*mo
for _, r := range relations {
if r.Direct != "" {
result = append(result, &model.ObjectRelation{
Object: model.ObjectName(r.Direct),
Object: r.Direct,
Relation: "",
})
}
Expand All @@ -180,7 +175,7 @@ func expandRel(m *model.Model, on model.ObjectName, rn model.RelationName) []*mo

if r.Wildcard != "" {
result = append(result, &model.ObjectRelation{
Object: model.ObjectName(r.Wildcard),
Object: r.Wildcard,
Relation: "*",
})
}
Expand All @@ -199,7 +194,7 @@ func resolve(m *model.Model, on model.ObjectName, rn model.RelationName) *model.
for _, rel := range m.Objects[on].Relations[rn] {
if rel.Direct != "" {
return &model.ObjectRelation{
Object: model.ObjectName(rel.Direct),
Object: rel.Direct,
Relation: model.RelationName(parts[1]),
}
}
Expand All @@ -213,7 +208,7 @@ func resolve(m *model.Model, on model.ObjectName, rn model.RelationName) *model.

if rel.Wildcard != "" {
return &model.ObjectRelation{
Object: model.ObjectName(rel.Wildcard),
Object: rel.Wildcard,
Relation: "*",
}
}
Expand Down
75 changes: 43 additions & 32 deletions model/model.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,48 @@
{
"version": 1,
"metadata": null,
"types": {
"user": {},
"group": {
"relations": {
"member": [
{"direct": "user"},
{"subject": {"object": "group", "relation": "member"}}
]
}
"version": 2,
"types": {
"document": {
"relations": {
"parent_folder": [{ "direct": "folder" }],
"reader": [{ "direct": "user" }, { "wildcard": "user" }],
"writer": [{ "direct": "user" }]
},
"permissions": {
"can_only_read": {
"exclusion": {
"include": { "rel_or_perm": "reader" },
"exclude": { "rel_or_perm": "writer" }
}
},
"folder": {
"relations": {
"owner": [{"direct": "user"}]
},
"permissions": {
"read": {"union": ["owner"]}
}
"edit": { "union": [{ "rel_or_perm": "writer" }] },
"read": {
"union": [{ "base": "parent_folder", "rel_or_perm": "read" }]
},
"document": {
"relations": {
"parent_folder": [{"direct": "folder"}],
"writer": [{"direct": "user"}],
"reader": [{"direct": "user"}, {"wildcard": "user"}]
},
"permissions": {
"edit": {"union": ["writer"]},
"view": {"union": ["reader", "writer"]},
"read_and_write": {"intersection": ["reader", "writer"]},
"can_only_read": {"exclusion": {"base": "reader", "subtract": "writer"}},
"read": {"arrow": {"relation": "parent_folder","permission": "read"}}
}
"read_and_write": {
"intersection": [
{ "rel_or_perm": "reader" },
{ "rel_or_perm": "writer" }
]
},
"view": {
"union": [{ "rel_or_perm": "reader" }, { "rel_or_perm": "writer" }]
}
}
}
},
"folder": {
"relations": { "owner": [{ "direct": "user" }] },
"permissions": { "read": { "union": [{ "rel_or_perm": "owner" }] } }
},
"group": {
"relations": {
"member": [
{ "direct": "user" },
{ "subject": { "object": "group", "relation": "member" } }
]
}
},
"user": {}
},
"metadata": null
}

10 changes: 5 additions & 5 deletions model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

var m1 = model.Model{
Version: 1,
Version: 2,
Objects: map[model.ObjectName]*model.Object{
model.ObjectName("user"): {},
model.ObjectName("group"): {
Expand Down Expand Up @@ -139,12 +139,12 @@ func TestModel(t *testing.T) {

func TestDiff(t *testing.T) {
m2 := model.Model{
Version: 1,
Version: 2,
Objects: nil,
}

m3 := model.Model{
Version: 1,
Version: 2,
Objects: map[model.ObjectName]*model.Object{
model.ObjectName("new_user"): {},
model.ObjectName("group"): {
Expand All @@ -169,7 +169,7 @@ func TestDiff(t *testing.T) {
},
Permissions: map[model.PermissionName]*model.Permission{
model.PermissionName("read"): {
Union: []string{"owner"},
Union: []*model.RelationRef{{RelOrPerm: "owner"}},
},
},
},
Expand Down Expand Up @@ -226,7 +226,7 @@ func TestDiff(t *testing.T) {

func TestGraph(t *testing.T) {
m := model.Model{
Version: 1,
Version: 2,
Objects: map[model.ObjectName]*model.Object{
model.ObjectName("user"): {
Relations: map[model.RelationName][]*model.Relation{
Expand Down
2 changes: 1 addition & 1 deletion parser/Azm.g4
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ COLON:
ASTERISK:
'*';

ID: [a-z][a-z0-9._]*[a-z0-9] ;
ID: [a-z][a-z0-9._-]*[a-z0-9] ;

WS: [ \t\n\r\f]+ -> skip ;
2 changes: 1 addition & 1 deletion parser/AzmLexer.interp
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ mode names:
DEFAULT_MODE

atn:
[4, 0, 9, 50, 6, -1, 2, 0, 7, 0, 2, 1, 7, 1, 2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 21, 1, 0, 0, 0, 5, 23, 1, 0, 0, 0, 7, 25, 1, 0, 0, 0, 9, 28, 1, 0, 0, 0, 11, 30, 1, 0, 0, 0, 13, 32, 1, 0, 0, 0, 15, 34, 1, 0, 0, 0, 17, 44, 1, 0, 0, 0, 19, 20, 5, 124, 0, 0, 20, 2, 1, 0, 0, 0, 21, 22, 5, 38, 0, 0, 22, 4, 1, 0, 0, 0, 23, 24, 5, 45, 0, 0, 24, 6, 1, 0, 0, 0, 25, 26, 5, 45, 0, 0, 26, 27, 5, 62, 0, 0, 27, 8, 1, 0, 0, 0, 28, 29, 5, 35, 0, 0, 29, 10, 1, 0, 0, 0, 30, 31, 5, 58, 0, 0, 31, 12, 1, 0, 0, 0, 32, 33, 5, 42, 0, 0, 33, 14, 1, 0, 0, 0, 34, 38, 7, 0, 0, 0, 35, 37, 7, 1, 0, 0, 36, 35, 1, 0, 0, 0, 37, 40, 1, 0, 0, 0, 38, 36, 1, 0, 0, 0, 38, 39, 1, 0, 0, 0, 39, 41, 1, 0, 0, 0, 40, 38, 1, 0, 0, 0, 41, 42, 7, 2, 0, 0, 42, 16, 1, 0, 0, 0, 43, 45, 7, 3, 0, 0, 44, 43, 1, 0, 0, 0, 45, 46, 1, 0, 0, 0, 46, 44, 1, 0, 0, 0, 46, 47, 1, 0, 0, 0, 47, 48, 1, 0, 0, 0, 48, 49, 6, 8, 0, 0, 49, 18, 1, 0, 0, 0, 3, 0, 38, 46, 1, 6, 0, 0]
2 changes: 1 addition & 1 deletion parser/azm_lexer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions parser/parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package parser

import (
"github.com/antlr4-go/antlr/v4"
"github.com/aserto-dev/azm/model"
)

func ParseRelation(input string) []*model.Relation {
p := newParser(input)
rTree := p.Relation()

var v RelationVisitor
return v.Visit(rTree).([]*model.Relation)
}

func ParsePermission(input string) *model.Permission {
p := newParser(input)
pTree := p.Permission()

var v PermissionVisitor
return v.Visit(pTree).(*model.Permission)
}

func newParser(input string) *AzmParser {
lexer := NewAzmLexer(antlr.NewInputStream(input))
stream := antlr.NewCommonTokenStream(lexer, 0)
p := NewAzmParser(stream)
p.AddErrorListener(antlr.NewDiagnosticErrorListener(true))
return p
}
39 changes: 12 additions & 27 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package parser_test
import (
"testing"

"github.com/antlr4-go/antlr/v4"
"github.com/aserto-dev/azm/model"
"github.com/aserto-dev/azm/parser"
"github.com/stretchr/testify/assert"
Expand All @@ -24,6 +23,16 @@ func TestRelationParser(t *testing.T) {
assert.Empty(term.Wildcard)
},
},
{
"name-with-dashes",
func(rel []*model.Relation, assert *assert.Assertions) {
assert.Len(rel, 1)
term := rel[0]
assert.Equal(model.ObjectName("name-with-dashes"), term.Direct)
assert.Nil(term.Subject)
assert.Empty(term.Wildcard)
},
},
{
"group#member",
func(rel []*model.Relation, assert *assert.Assertions) {
Expand Down Expand Up @@ -68,7 +77,7 @@ func TestRelationParser(t *testing.T) {

for _, test := range tests {
t.Run(test.input, func(tt *testing.T) {
rel := parseRelation(test.input)
rel := parser.ParseRelation(test.input)
test.validate(rel, assert.New(tt))
})
}
Expand Down Expand Up @@ -130,33 +139,9 @@ func TestPermissionParser(t *testing.T) {

for _, test := range tests {
t.Run(test.input, func(tt *testing.T) {
perm := parsePermission(test.input)
perm := parser.ParsePermission(test.input)
test.validate(perm, assert.New(tt))
})
}

}

func parseRelation(input string) []*model.Relation {
p := newParser(input)
rTree := p.Relation()

var v parser.RelationVisitor
return v.Visit(rTree).([]*model.Relation)
}

func parsePermission(input string) *model.Permission {
p := newParser(input)
pTree := p.Permission()

var v parser.PermissionVisitor
return v.Visit(pTree).(*model.Permission)
}

func newParser(input string) *parser.AzmParser {
lexer := parser.NewAzmLexer(antlr.NewInputStream(input))
stream := antlr.NewCommonTokenStream(lexer, 0)
p := parser.NewAzmParser(stream)
p.AddErrorListener(antlr.NewDiagnosticErrorListener(true))
return p
}
Loading

0 comments on commit 332492c

Please sign in to comment.