forked from cedar-policy/cedar-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request cedar-policy#63 from strongdm/add-ast-docs
ast: restructure ast package documentation
- Loading branch information
Showing
5 changed files
with
236 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package ast_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cedar-policy/cedar-go/ast" | ||
"github.com/cedar-policy/cedar-go/types" | ||
) | ||
|
||
// This example shows a basic programmatic AST construction via the Permit() builder: | ||
func Example() { | ||
johnny := types.NewEntityUID("FolkHeroes", "johnnyChapman") | ||
sow := types.NewEntityUID("Action", "sow") | ||
cast := types.NewEntityUID("Action", "cast") | ||
midwest := types.NewEntityUID("Locations::USA::Regions", "midwest") | ||
|
||
policy := ast.Permit(). | ||
PrincipalEq(johnny). | ||
ActionInSet(sow, cast). | ||
ResourceIs("Crops::Apple"). | ||
When(ast.Context().Access("location").In(ast.Value(midwest))). | ||
Unless(ast.Context().Access("season").Equal(ast.String("winter"))) | ||
|
||
fmt.Println(string(policy.MarshalCedar())) | ||
|
||
// Output: | ||
// permit ( | ||
// principal == FolkHeroes::"johnnyChapman", | ||
// action in [Action::"sow", Action::"cast"], | ||
// resource is Crops::Apple | ||
// ) | ||
// when { context.location in Locations::USA::Regions::"midwest" } | ||
// unless { context.season == "winter" }; | ||
} | ||
|
||
// To programmatically create policies with annotations, use the Annotation() builder: | ||
func Example_annotation() { | ||
policy := ast.Annotation("example1", "value"). | ||
Annotation("example2", ""). | ||
Forbid() | ||
|
||
fmt.Println(string(policy.MarshalCedar())) | ||
|
||
// Output: | ||
// @example1("value") | ||
// @example2("") | ||
// forbid ( principal, action, resource ); | ||
} | ||
|
||
// This example shows how precedence can be expressed using the AST builder syntax: | ||
func Example_precedence() { | ||
// The argument passed to .Add() is the entire right-hand side of the expression, so 1 + 5 is evaluated with | ||
// higher precedence than the subsequent multiplication by 10. | ||
policy := ast.Permit(). | ||
When(ast.Long(1).Add(ast.Long(5)).Multiply(ast.Long(10)).Equal(ast.Long(60))) | ||
|
||
fmt.Println(string(policy.MarshalCedar())) | ||
|
||
// Output: | ||
// permit ( principal, action, resource ) | ||
// when { (1 + 5) * 10 == 60 }; | ||
} | ||
|
||
// Extension functions can be explicitly called by using the appropriate builder with the ExtensionCall suffix. This | ||
// example demonstrates the use of DecimalExtensionCall(): | ||
func Example_explicitExtensionCall() { | ||
policy := ast.Forbid(). | ||
When( | ||
ast.Resource().Access("angleRadians").DecimalGreaterThan( | ||
ast.DecimalExtensionCall(ast.String("3.1415")), | ||
), | ||
) | ||
|
||
fmt.Println(string(policy.MarshalCedar())) | ||
|
||
// Output: | ||
// forbid ( principal, action, resource ) | ||
// when { resource.angleRadians.greaterThan(decimal("3.1415")) }; | ||
} | ||
|
||
func ExampleRecord() { | ||
// Literal records can be constructed and passed via the ast.Value() builder | ||
literalRecord := types.NewRecord(types.RecordMap{ | ||
"x": types.String("value1"), | ||
"y": types.String("value2"), | ||
}) | ||
|
||
// Records with internal expressions are constructed via the ast.Record() builder | ||
exprRecord := ast.Record(ast.Pairs{ | ||
{ | ||
Key: "x", | ||
Value: ast.Long(1).Add(ast.Context().Access("fooCount")), | ||
}, | ||
{ | ||
Key: "y", | ||
Value: ast.Long(8), | ||
}, | ||
}) | ||
|
||
policy := ast.Forbid(). | ||
When( | ||
ast.Value(literalRecord).Access("x").Equal(ast.String("value1")), | ||
). | ||
When( | ||
exprRecord.Access("x").Equal(ast.Long(3)), | ||
) | ||
|
||
fmt.Println(string(policy.MarshalCedar())) | ||
|
||
// Output: | ||
// forbid ( principal, action, resource ) | ||
// when { {"x":"value1", "y":"value2"}.x == "value1" } | ||
// when { {"x":(1 + context.fooCount), "y":8}.x == 3 }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package ast_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cedar-policy/cedar-go/ast" | ||
internalast "github.com/cedar-policy/cedar-go/internal/ast" | ||
"github.com/cedar-policy/cedar-go/internal/testutil" | ||
"github.com/cedar-policy/cedar-go/types" | ||
) | ||
|
||
func TestPolicy_MarshalJSON(t *testing.T) { | ||
t.Parallel() | ||
|
||
p := ast.Permit().PrincipalEq(types.NewEntityUID("Foo::Bar", "Baz")) | ||
expected := `{ | ||
"effect": "permit", | ||
"principal": { | ||
"op": "==", | ||
"entity": { | ||
"type": "Foo::Bar", | ||
"id": "Baz" | ||
} | ||
}, | ||
"action": { | ||
"op": "All" | ||
}, | ||
"resource": { | ||
"op": "All" | ||
} | ||
}` | ||
testutil.JSONMarshalsTo(t, p, expected) | ||
|
||
var unmarshaled ast.Policy | ||
err := unmarshaled.UnmarshalJSON([]byte(expected)) | ||
testutil.OK(t, err) | ||
testutil.Equals(t, &unmarshaled, p) | ||
} | ||
|
||
func TestPolicy_MarshalCedar(t *testing.T) { | ||
t.Parallel() | ||
|
||
p := ast.Permit().PrincipalEq(types.NewEntityUID("Foo::Bar", "Baz")) | ||
expected := `permit ( | ||
principal == Foo::Bar::"Baz", | ||
action, | ||
resource | ||
);` | ||
|
||
testutil.Equals(t, string(p.MarshalCedar()), expected) | ||
|
||
var unmarshaled ast.Policy | ||
err := unmarshaled.UnmarshalCedar([]byte(expected)) | ||
|
||
p.Position = internalast.Position{Offset: 0, Line: 1, Column: 1} | ||
testutil.OK(t, err) | ||
testutil.Equals(t, &unmarshaled, p) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters