Skip to content

Commit

Permalink
ast: implement tag nodes in AST
Browse files Browse the repository at this point in the history
Signed-off-by: Ali Dowair <[email protected]>
  • Loading branch information
adowair committed Dec 5, 2024
1 parent 5770c5e commit 387286b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ast/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,16 @@ func TestASTByTable(t *testing.T) {
ast.Permit().When(ast.Long(42).Has("key")),
internalast.Permit().When(internalast.Long(42).Has("key")),
},
{
"opGetTag",
ast.Permit().When(ast.Long(42).GetTag(ast.String("key"))),
internalast.Permit().When(internalast.Long(42).GetTag(internalast.String("key"))),
},
{
"opsHasTag",
ast.Permit().When(ast.Long(42).HasTag(ast.String("key"))),
internalast.Permit().When(internalast.Long(42).HasTag(internalast.String("key"))),
},
{
"opIsIpv4",
ast.Permit().When(ast.Long(42).IsIpv4()),
Expand Down
8 changes: 8 additions & 0 deletions ast/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ func (lhs Node) Has(attr types.String) Node {
return wrapNode(lhs.Node.Has(attr))
}

func (lhs Node) GetTag(rhs Node) Node {
return wrapNode(lhs.Node.GetTag(rhs.Node))
}

func (lhs Node) HasTag(rhs Node) Node {
return wrapNode(lhs.Node.HasTag(rhs.Node))
}

// ___ ____ _ _ _
// |_ _| _ \ / \ __| | __| |_ __ ___ ___ ___
// | || |_) / _ \ / _` |/ _` | '__/ _ \/ __/ __|
Expand Down
12 changes: 12 additions & 0 deletions x/exp/ast/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,18 @@ func TestASTByTable(t *testing.T) {
ast.Policy{Effect: ast.EffectPermit, Principal: ast.ScopeTypeAll{}, Action: ast.ScopeTypeAll{}, Resource: ast.ScopeTypeAll{},
Conditions: []ast.ConditionType{{Condition: ast.ConditionWhen, Body: ast.NodeTypeContainsAny{BinaryNode: ast.BinaryNode{Left: ast.NodeValue{Value: types.Long(42)}, Right: ast.NodeValue{Value: types.Long(43)}}}}}},
},
{
"opGetTag",
ast.Permit().When(ast.Long(42).GetTag(ast.String("key"))),
ast.Policy{Effect: ast.EffectPermit, Principal: ast.ScopeTypeAll{}, Action: ast.ScopeTypeAll{}, Resource: ast.ScopeTypeAll{},
Conditions: []ast.ConditionType{{Condition: ast.ConditionWhen, Body: ast.NodeTypeExtensionCall{Name: "getTag", Args: []ast.IsNode{ast.NodeValue{Value: types.Long(42)}, ast.NodeValue{Value: types.String("key")}}}}}},
},
{
"opHasTag",
ast.Permit().When(ast.Long(42).HasTag(ast.String("key"))),
ast.Policy{Effect: ast.EffectPermit, Principal: ast.ScopeTypeAll{}, Action: ast.ScopeTypeAll{}, Resource: ast.ScopeTypeAll{},
Conditions: []ast.ConditionType{{Condition: ast.ConditionWhen, Body: ast.NodeTypeExtensionCall{Name: "hasTag", Args: []ast.IsNode{ast.NodeValue{Value: types.Long(42)}, ast.NodeValue{Value: types.String("key")}}}}}},
},
{
"opAccess",
ast.Permit().When(ast.Long(42).Access("key")),
Expand Down
8 changes: 8 additions & 0 deletions x/exp/ast/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ func (lhs Node) Has(attr types.String) Node {
return NewNode(NodeTypeHas{StrOpNode: StrOpNode{Arg: lhs.v, Value: attr}})
}

func (lhs Node) GetTag(rhs Node) Node {
return NewMethodCall(lhs, "getTag", rhs)
}

func (lhs Node) HasTag(rhs Node) Node {
return NewMethodCall(lhs, "hasTag", rhs)
}

// ___ ____ _ _ _
// |_ _| _ \ / \ __| | __| |_ __ ___ ___ ___
// | || |_) / _ \ / _` |/ _` | '__/ _ \/ __/ __|
Expand Down

0 comments on commit 387286b

Please sign in to comment.