Skip to content

Commit

Permalink
ast: made equal not equal more consistently named
Browse files Browse the repository at this point in the history
Addresses IDX-142

Signed-off-by: philhassey <[email protected]>
  • Loading branch information
philhassey committed Aug 23, 2024
1 parent bf3d07d commit 1bc846d
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 43 deletions.
12 changes: 6 additions & 6 deletions ast/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ func TestAstExamples(t *testing.T) {
}
_ = ast.Forbid().
When(
ast.Value(simpleRecord).Access("x").Equals(ast.String("value")),
ast.Value(simpleRecord).Access("x").Equal(ast.String("value")),
).
When(
ast.Record(ast.Pairs{{Key: "x", Value: ast.Long(1).Add(ast.Context().Access("fooCount"))}}).Access("x").Equals(ast.Long(3)),
ast.Record(ast.Pairs{{Key: "x", Value: ast.Long(1).Add(ast.Context().Access("fooCount"))}}).Access("x").Equal(ast.Long(3)),
).
When(
ast.Set(
Expand Down Expand Up @@ -245,13 +245,13 @@ func TestASTByTable(t *testing.T) {
},
{
"opEquals",
ast.Permit().When(ast.Long(42).Equals(ast.Long(43))),
internalast.Permit().When(internalast.Long(42).Equals(internalast.Long(43))),
ast.Permit().When(ast.Long(42).Equal(ast.Long(43))),
internalast.Permit().When(internalast.Long(42).Equal(internalast.Long(43))),
},
{
"opNotEquals",
ast.Permit().When(ast.Long(42).NotEquals(ast.Long(43))),
internalast.Permit().When(internalast.Long(42).NotEquals(internalast.Long(43))),
ast.Permit().When(ast.Long(42).NotEqual(ast.Long(43))),
internalast.Permit().When(internalast.Long(42).NotEqual(internalast.Long(43))),
},
{
"opLessThan",
Expand Down
8 changes: 4 additions & 4 deletions ast/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
// \____\___/|_| |_| |_| .__/ \__,_|_| |_|___/\___/|_| |_|
// |_|

func (lhs Node) Equals(rhs Node) Node {
return wrapNode(lhs.Node.Equals(rhs.Node))
func (lhs Node) Equal(rhs Node) Node {
return wrapNode(lhs.Node.Equal(rhs.Node))
}

func (lhs Node) NotEquals(rhs Node) Node {
return wrapNode(lhs.Node.NotEquals(rhs.Node))
func (lhs Node) NotEqual(rhs Node) Node {
return wrapNode(lhs.Node.NotEqual(rhs.Node))
}

func (lhs Node) LessThan(rhs Node) Node {
Expand Down
8 changes: 4 additions & 4 deletions internal/ast/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ func TestAstExamples(t *testing.T) {
}
_ = ast.Forbid().
When(
ast.Value(simpleRecord).Access("x").Equals(ast.String("value")),
ast.Value(simpleRecord).Access("x").Equal(ast.String("value")),
).
When(
ast.Record(ast.Pairs{{Key: "x", Value: ast.Long(1).Add(ast.Context().Access("fooCount"))}}).
Access("x").Equals(ast.Long(3)),
Access("x").Equal(ast.Long(3)),
).
When(
ast.Set(
Expand Down Expand Up @@ -290,13 +290,13 @@ func TestASTByTable(t *testing.T) {
}},
{
"opEquals",
ast.Permit().When(ast.Long(42).Equals(ast.Long(43))),
ast.Permit().When(ast.Long(42).Equal(ast.Long(43))),
ast.Policy{Effect: ast.EffectPermit, Principal: ast.ScopeTypeAll{}, Action: ast.ScopeTypeAll{}, Resource: ast.ScopeTypeAll{},
Conditions: []ast.ConditionType{{Condition: ast.ConditionWhen, Body: ast.NodeTypeEquals{BinaryNode: ast.BinaryNode{Left: ast.NodeValue{Value: types.Long(42)}, Right: ast.NodeValue{Value: types.Long(43)}}}}}},
},
{
"opNotEquals",
ast.Permit().When(ast.Long(42).NotEquals(ast.Long(43))),
ast.Permit().When(ast.Long(42).NotEqual(ast.Long(43))),
ast.Policy{Effect: ast.EffectPermit, Principal: ast.ScopeTypeAll{}, Action: ast.ScopeTypeAll{}, Resource: ast.ScopeTypeAll{},
Conditions: []ast.ConditionType{{Condition: ast.ConditionWhen, Body: ast.NodeTypeNotEquals{BinaryNode: ast.BinaryNode{Left: ast.NodeValue{Value: types.Long(42)}, Right: ast.NodeValue{Value: types.Long(43)}}}}}},
},
Expand Down
4 changes: 2 additions & 2 deletions internal/ast/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import "github.com/cedar-policy/cedar-go/types"
// \____\___/|_| |_| |_| .__/ \__,_|_| |_|___/\___/|_| |_|
// |_|

func (lhs Node) Equals(rhs Node) Node {
func (lhs Node) Equal(rhs Node) Node {
return NewNode(NodeTypeEquals{BinaryNode: BinaryNode{Left: lhs.v, Right: rhs.v}})
}

func (lhs Node) NotEquals(rhs Node) Node {
func (lhs Node) NotEqual(rhs Node) Node {
return NewNode(NodeTypeNotEquals{BinaryNode: BinaryNode{Left: lhs.v, Right: rhs.v}})
}

Expand Down
2 changes: 1 addition & 1 deletion internal/eval/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func scopeToNode(varNode ast.NodeTypeVariable, in ast.IsScopeNode) ast.Node {
case ast.ScopeTypeAll:
return ast.True()
case ast.ScopeTypeEq:
return ast.NewNode(varNode).Equals(ast.Value(t.Entity))
return ast.NewNode(varNode).Equal(ast.Value(t.Entity))
case ast.ScopeTypeIn:
return ast.NewNode(varNode).In(ast.Value(t.Entity))
case ast.ScopeTypeInSet:
Expand Down
8 changes: 4 additions & 4 deletions internal/eval/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ func TestPolicyToNode(t *testing.T) {
ActionEq(types.NewEntityUID("Action", "test")).
ResourceEq(types.NewEntityUID("Resource", "database")),

ast.Principal().Equals(ast.EntityUID("Account", "principal")).And(
ast.Action().Equals(ast.EntityUID("Action", "test")).And(
ast.Resource().Equals(ast.EntityUID("Resource", "database")),
ast.Principal().Equal(ast.EntityUID("Account", "principal")).And(
ast.Action().Equal(ast.EntityUID("Action", "test")).And(
ast.Resource().Equal(ast.EntityUID("Resource", "database")),
),
),
},
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestScopeToNode(t *testing.T) {
"eq",
ast.NewPrincipalNode(),
ast.ScopeTypeEq{Entity: types.NewEntityUID("T", "42")},
ast.Principal().Equals(ast.EntityUID("T", "42")),
ast.Principal().Equal(ast.EntityUID("T", "42")),
},
{
"in",
Expand Down
4 changes: 2 additions & 2 deletions internal/eval/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ func TestToEval(t *testing.T) {
},
{
"equals",
ast.Long(42).Equals(ast.Long(43)),
ast.Long(42).Equal(ast.Long(43)),
types.False,
testutil.OK,
},
{
"notEquals",
ast.Long(42).NotEquals(ast.Long(43)),
ast.Long(42).NotEqual(ast.Long(43)),
types.True,
testutil.OK,
},
Expand Down
6 changes: 3 additions & 3 deletions internal/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestUnmarshalJSON(t *testing.T) {
ActionEq(types.NewEntityUID("Action", "view")).
ResourceIn(types.NewEntityUID("Folder", "abc")).
When(
ast.Context().Access("tls_version").Equals(ast.String("1.3")),
ast.Context().Access("tls_version").Equal(ast.String("1.3")),
),
testutil.OK,
},
Expand Down Expand Up @@ -265,14 +265,14 @@ func TestUnmarshalJSON(t *testing.T) {
"equals",
`{"effect":"permit","principal":{"op":"All"},"action":{"op":"All"},"resource":{"op":"All"},
"conditions":[{"kind":"when","body":{"==":{"left":{"Value":42},"right":{"Value":24}}}}]}`,
ast.Permit().When(ast.Long(42).Equals(ast.Long(24))),
ast.Permit().When(ast.Long(42).Equal(ast.Long(24))),
testutil.OK,
},
{
"notEquals",
`{"effect":"permit","principal":{"op":"All"},"action":{"op":"All"},"resource":{"op":"All"},
"conditions":[{"kind":"when","body":{"!=":{"left":{"Value":42},"right":{"Value":24}}}}]}`,
ast.Permit().When(ast.Long(42).NotEquals(ast.Long(24))),
ast.Permit().When(ast.Long(42).NotEqual(ast.Long(24))),
testutil.OK,
},
{
Expand Down
4 changes: 2 additions & 2 deletions internal/json/json_unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ func (j nodeJSON) ToNode() (ast.Node, error) {

// Binary operators: ==, !=, in, <, <=, >, >=, &&, ||, +, -, *, contains, containsAll, containsAny
case j.Equals != nil:
return j.Equals.ToNode(ast.Node.Equals)
return j.Equals.ToNode(ast.Node.Equal)
case j.NotEquals != nil:
return j.NotEquals.ToNode(ast.Node.NotEquals)
return j.NotEquals.ToNode(ast.Node.NotEqual)
case j.In != nil:
return j.In.ToNode(ast.Node.In)
case j.LessThan != nil:
Expand Down
2 changes: 1 addition & 1 deletion internal/parser/cedar_marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func scopeToNode(varNode ast.NodeTypeVariable, in ast.IsScopeNode) ast.Node {
case ast.ScopeTypeAll:
return ast.True()
case ast.ScopeTypeEq:
return ast.NewNode(varNode).Equals(ast.Value(t.Entity))
return ast.NewNode(varNode).Equal(ast.Value(t.Entity))
case ast.ScopeTypeIn:
return ast.NewNode(varNode).In(ast.Value(t.Entity))
case ast.ScopeTypeInSet:
Expand Down
4 changes: 2 additions & 2 deletions internal/parser/cedar_unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,9 +517,9 @@ func (p *parser) relation() (ast.Node, error) {
case ">=":
operator = ast.Node.GreaterThanOrEqual
case "!=":
operator = ast.Node.NotEquals
operator = ast.Node.NotEqual
case "==":
operator = ast.Node.Equals
operator = ast.Node.Equal
case "in":
operator = ast.Node.In
default:
Expand Down
22 changes: 11 additions & 11 deletions internal/parser/cedar_unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,13 @@ when { 2 >= 42 };`,
"equal",
`permit ( principal, action, resource )
when { 2 == 42 };`,
ast.Permit().When(ast.Long(2).Equals(ast.Long(42))),
ast.Permit().When(ast.Long(2).Equal(ast.Long(42))),
},
{
"not equal",
`permit ( principal, action, resource )
when { 2 != 42 };`,
ast.Permit().When(ast.Long(2).NotEquals(ast.Long(42))),
ast.Permit().When(ast.Long(2).NotEqual(ast.Long(42))),
},
{
"in",
Expand Down Expand Up @@ -349,7 +349,7 @@ when { if true then true else false };`,
`permit ( principal, action, resource )
when { ip("1.2.3.4") == ip("2.3.4.5") };`,
ast.Permit().When(
ast.ExtensionCall("ip", ast.String("1.2.3.4")).Equals(
ast.ExtensionCall("ip", ast.String("1.2.3.4")).Equal(
ast.ExtensionCall("ip", ast.String("2.3.4.5")),
),
),
Expand All @@ -359,7 +359,7 @@ when { ip("1.2.3.4") == ip("2.3.4.5") };`,
`permit ( principal, action, resource )
when { decimal("12.34") == decimal("23.45") };`,
ast.Permit().When(
ast.ExtensionCall("decimal", ast.String("12.34")).Equals(ast.ExtensionCall("decimal", ast.String("23.45"))),
ast.ExtensionCall("decimal", ast.String("12.34")).Equal(ast.ExtensionCall("decimal", ast.String("23.45"))),
),
},
{
Expand All @@ -384,19 +384,19 @@ when { 1 + 1 < 3 };`,
"mult over add precedence (rhs add)",
`permit ( principal, action, resource )
when { 2 * 3 + 4 == 10 };`,
ast.Permit().When(ast.Long(2).Multiply(ast.Long(3)).Add(ast.Long(4)).Equals(ast.Long(10))),
ast.Permit().When(ast.Long(2).Multiply(ast.Long(3)).Add(ast.Long(4)).Equal(ast.Long(10))),
},
{
"mult over add precedence (lhs add)",
`permit ( principal, action, resource )
when { 2 + 3 * 4 == 14 };`,
ast.Permit().When(ast.Long(2).Add(ast.Long(3).Multiply(ast.Long(4))).Equals(ast.Long(14))),
ast.Permit().When(ast.Long(2).Add(ast.Long(3).Multiply(ast.Long(4))).Equal(ast.Long(14))),
},
{
"unary over mult precedence",
`permit ( principal, action, resource )
when { -2 * 3 == -6 };`,
ast.Permit().When(ast.Long(-2).Multiply(ast.Long(3)).Equals(ast.Long(-6))),
ast.Permit().When(ast.Long(-2).Multiply(ast.Long(3)).Equal(ast.Long(-6))),
},
{
"member over unary precedence",
Expand All @@ -408,25 +408,25 @@ when { -context.num };`,
"parens over unary precedence",
`permit ( principal, action, resource )
when { -(2 + 3) == -5 };`,
ast.Permit().When(ast.Negate(ast.Long(2).Add(ast.Long(3))).Equals(ast.Long(-5))),
ast.Permit().When(ast.Negate(ast.Long(2).Add(ast.Long(3))).Equal(ast.Long(-5))),
},
{
"multiple parenthesized operations",
`permit ( principal, action, resource )
when { (2 + 3 + 4) * 5 == 18 };`,
ast.Permit().When(ast.Long(2).Add(ast.Long(3)).Add(ast.Long(4)).Multiply(ast.Long(5)).Equals(ast.Long(18))),
ast.Permit().When(ast.Long(2).Add(ast.Long(3)).Add(ast.Long(4)).Multiply(ast.Long(5)).Equal(ast.Long(18))),
},
{
"parenthesized if",
`permit ( principal, action, resource )
when { (if true then 2 else 3 * 4) == 2 };`,
ast.Permit().When(ast.IfThenElse(ast.True(), ast.Long(2), ast.Long(3).Multiply(ast.Long(4))).Equals(ast.Long(2))),
ast.Permit().When(ast.IfThenElse(ast.True(), ast.Long(2), ast.Long(3).Multiply(ast.Long(4))).Equal(ast.Long(2))),
},
{
"parenthesized if with trailing mult",
`permit ( principal, action, resource )
when { (if true then 2 else 3) * 4 == 8 };`,
ast.Permit().When(ast.IfThenElse(ast.True(), ast.Long(2), ast.Long(3)).Multiply(ast.Long(4)).Equals(ast.Long(8))),
ast.Permit().When(ast.IfThenElse(ast.True(), ast.Long(2), ast.Long(3)).Multiply(ast.Long(4)).Equal(ast.Long(8))),
},
}

Expand Down
2 changes: 1 addition & 1 deletion policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func TestPolicyAST(t *testing.T) {

astExample := ast.Permit().
ActionEq(types.NewEntityUID("Action", "editPhoto")).
When(ast.Resource().Access("owner").Equals(ast.Principal()))
When(ast.Resource().Access("owner").Equal(ast.Principal()))

_ = cedar.NewPolicyFromAST(astExample)
}

0 comments on commit 1bc846d

Please sign in to comment.