From a73ca755647990a77e760a78b695ca3487f87589 Mon Sep 17 00:00:00 2001 From: philhassey Date: Tue, 26 Nov 2024 14:11:38 -0700 Subject: [PATCH] x/exp/ast: move ast into the x/exp package Addresses IDX-225 Signed-off-by: philhassey --- ast/annotation.go | 2 +- ast/ast_test.go | 2 +- ast/node.go | 2 +- ast/operator.go | 2 +- ast/policy.go | 2 +- ast/policy_test.go | 2 +- ast/value.go | 2 +- ast/variable.go | 2 +- internal/eval/compile.go | 2 +- internal/eval/compile_test.go | 2 +- internal/eval/convert.go | 2 +- internal/eval/convert_test.go | 2 +- internal/eval/fold.go | 2 +- internal/eval/fold_test.go | 2 +- internal/eval/partial.go | 2 +- internal/eval/partial_test.go | 2 +- internal/json/json_marshal.go | 2 +- internal/json/json_test.go | 2 +- internal/json/json_unmarshal.go | 2 +- internal/parser/cedar_marshal.go | 2 +- internal/parser/cedar_parse_test.go | 2 +- internal/parser/cedar_tokenize.go | 2 +- internal/parser/cedar_unmarshal.go | 2 +- internal/parser/cedar_unmarshal_test.go | 2 +- internal/parser/internal_test.go | 2 +- internal/parser/node.go | 2 +- internal/parser/policy.go | 2 +- policy.go | 2 +- policy_list.go | 2 +- policy_set.go | 2 +- {internal => x/exp}/ast/annotation.go | 0 {internal => x/exp}/ast/ast_test.go | 2 +- x/exp/ast/doc.go | 21 +++++++++++++++++++++ {internal => x/exp}/ast/internal_test.go | 0 {internal => x/exp}/ast/node.go | 0 {internal => x/exp}/ast/operator.go | 0 {internal => x/exp}/ast/policy.go | 0 {internal => x/exp}/ast/scope.go | 0 {internal => x/exp}/ast/value.go | 0 {internal => x/exp}/ast/variable.go | 0 x/exp/batch/batch.go | 2 +- x/exp/batch/batch_test.go | 2 +- 42 files changed, 54 insertions(+), 33 deletions(-) rename {internal => x/exp}/ast/annotation.go (100%) rename {internal => x/exp}/ast/ast_test.go (99%) create mode 100644 x/exp/ast/doc.go rename {internal => x/exp}/ast/internal_test.go (100%) rename {internal => x/exp}/ast/node.go (100%) rename {internal => x/exp}/ast/operator.go (100%) rename {internal => x/exp}/ast/policy.go (100%) rename {internal => x/exp}/ast/scope.go (100%) rename {internal => x/exp}/ast/value.go (100%) rename {internal => x/exp}/ast/variable.go (100%) diff --git a/ast/annotation.go b/ast/annotation.go index eaa67138..218d2d3c 100644 --- a/ast/annotation.go +++ b/ast/annotation.go @@ -1,8 +1,8 @@ package ast import ( - "github.com/cedar-policy/cedar-go/internal/ast" "github.com/cedar-policy/cedar-go/types" + "github.com/cedar-policy/cedar-go/x/exp/ast" ) type Annotations ast.Annotations diff --git a/ast/ast_test.go b/ast/ast_test.go index 53a03795..6398dd9f 100644 --- a/ast/ast_test.go +++ b/ast/ast_test.go @@ -6,9 +6,9 @@ import ( "time" "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" + internalast "github.com/cedar-policy/cedar-go/x/exp/ast" ) func TestASTByTable(t *testing.T) { diff --git a/ast/node.go b/ast/node.go index 8a9e804e..6ffb9a44 100644 --- a/ast/node.go +++ b/ast/node.go @@ -1,6 +1,6 @@ package ast -import "github.com/cedar-policy/cedar-go/internal/ast" +import "github.com/cedar-policy/cedar-go/x/exp/ast" // Node is a wrapper type for all the Cedar language operators. See the [Cedar operators documentation] for details. // diff --git a/ast/operator.go b/ast/operator.go index 833f92a2..5c807451 100644 --- a/ast/operator.go +++ b/ast/operator.go @@ -1,8 +1,8 @@ package ast import ( - "github.com/cedar-policy/cedar-go/internal/ast" "github.com/cedar-policy/cedar-go/types" + "github.com/cedar-policy/cedar-go/x/exp/ast" ) // ____ _ diff --git a/ast/policy.go b/ast/policy.go index be2ddc9e..5ae81545 100644 --- a/ast/policy.go +++ b/ast/policy.go @@ -9,9 +9,9 @@ package ast import ( "bytes" - "github.com/cedar-policy/cedar-go/internal/ast" "github.com/cedar-policy/cedar-go/internal/json" "github.com/cedar-policy/cedar-go/internal/parser" + "github.com/cedar-policy/cedar-go/x/exp/ast" ) type Policy ast.Policy diff --git a/ast/policy_test.go b/ast/policy_test.go index 7e817ec9..fbdc0ff1 100644 --- a/ast/policy_test.go +++ b/ast/policy_test.go @@ -4,9 +4,9 @@ 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" + internalast "github.com/cedar-policy/cedar-go/x/exp/ast" ) func TestPolicy_MarshalJSON(t *testing.T) { diff --git a/ast/value.go b/ast/value.go index 3be17316..b2fbf053 100644 --- a/ast/value.go +++ b/ast/value.go @@ -4,8 +4,8 @@ import ( "net/netip" "time" - "github.com/cedar-policy/cedar-go/internal/ast" "github.com/cedar-policy/cedar-go/types" + "github.com/cedar-policy/cedar-go/x/exp/ast" ) // Boolean creates a value node containing a Boolean. diff --git a/ast/variable.go b/ast/variable.go index f12ce38b..a378875f 100644 --- a/ast/variable.go +++ b/ast/variable.go @@ -1,6 +1,6 @@ package ast -import "github.com/cedar-policy/cedar-go/internal/ast" +import "github.com/cedar-policy/cedar-go/x/exp/ast" func Principal() Node { return wrapNode(ast.Principal()) diff --git a/internal/eval/compile.go b/internal/eval/compile.go index 74d403fd..599c349c 100644 --- a/internal/eval/compile.go +++ b/internal/eval/compile.go @@ -3,8 +3,8 @@ package eval import ( "fmt" - "github.com/cedar-policy/cedar-go/internal/ast" "github.com/cedar-policy/cedar-go/types" + "github.com/cedar-policy/cedar-go/x/exp/ast" ) type BoolEvaler struct { diff --git a/internal/eval/compile_test.go b/internal/eval/compile_test.go index c73e8c2f..6ebb89fe 100644 --- a/internal/eval/compile_test.go +++ b/internal/eval/compile_test.go @@ -4,9 +4,9 @@ import ( "fmt" "testing" - "github.com/cedar-policy/cedar-go/internal/ast" "github.com/cedar-policy/cedar-go/internal/testutil" "github.com/cedar-policy/cedar-go/types" + "github.com/cedar-policy/cedar-go/x/exp/ast" ) func TestCompile(t *testing.T) { diff --git a/internal/eval/convert.go b/internal/eval/convert.go index 7472c890..44f5bb15 100644 --- a/internal/eval/convert.go +++ b/internal/eval/convert.go @@ -3,9 +3,9 @@ package eval import ( "fmt" - "github.com/cedar-policy/cedar-go/internal/ast" "github.com/cedar-policy/cedar-go/internal/consts" "github.com/cedar-policy/cedar-go/types" + "github.com/cedar-policy/cedar-go/x/exp/ast" ) func toEval(n ast.IsNode) Evaler { diff --git a/internal/eval/convert_test.go b/internal/eval/convert_test.go index 1868107a..e73dd214 100644 --- a/internal/eval/convert_test.go +++ b/internal/eval/convert_test.go @@ -5,9 +5,9 @@ import ( "testing" "time" - "github.com/cedar-policy/cedar-go/internal/ast" "github.com/cedar-policy/cedar-go/internal/testutil" "github.com/cedar-policy/cedar-go/types" + "github.com/cedar-policy/cedar-go/x/exp/ast" ) func TestToEval(t *testing.T) { diff --git a/internal/eval/fold.go b/internal/eval/fold.go index 9dc1d36f..880834bf 100644 --- a/internal/eval/fold.go +++ b/internal/eval/fold.go @@ -4,8 +4,8 @@ import ( "fmt" "slices" - "github.com/cedar-policy/cedar-go/internal/ast" "github.com/cedar-policy/cedar-go/types" + "github.com/cedar-policy/cedar-go/x/exp/ast" ) // foldPolicy takes in a given policy and attempts as much constant folding as possible. diff --git a/internal/eval/fold_test.go b/internal/eval/fold_test.go index 9230daba..09667174 100644 --- a/internal/eval/fold_test.go +++ b/internal/eval/fold_test.go @@ -5,9 +5,9 @@ import ( "testing" "time" - "github.com/cedar-policy/cedar-go/internal/ast" "github.com/cedar-policy/cedar-go/internal/testutil" "github.com/cedar-policy/cedar-go/types" + "github.com/cedar-policy/cedar-go/x/exp/ast" ) func TestFoldNode(t *testing.T) { diff --git a/internal/eval/partial.go b/internal/eval/partial.go index 922eb1ed..07106cd8 100644 --- a/internal/eval/partial.go +++ b/internal/eval/partial.go @@ -5,9 +5,9 @@ import ( "fmt" "slices" - "github.com/cedar-policy/cedar-go/internal/ast" "github.com/cedar-policy/cedar-go/internal/mapset" "github.com/cedar-policy/cedar-go/types" + "github.com/cedar-policy/cedar-go/x/exp/ast" ) const variableEntityType = "__cedar::variable" diff --git a/internal/eval/partial_test.go b/internal/eval/partial_test.go index b2ab92c6..0509c41e 100644 --- a/internal/eval/partial_test.go +++ b/internal/eval/partial_test.go @@ -7,9 +7,9 @@ import ( "testing" "time" - "github.com/cedar-policy/cedar-go/internal/ast" "github.com/cedar-policy/cedar-go/internal/testutil" "github.com/cedar-policy/cedar-go/types" + "github.com/cedar-policy/cedar-go/x/exp/ast" ) func TestPartialScopeEval(t *testing.T) { diff --git a/internal/json/json_marshal.go b/internal/json/json_marshal.go index d8bc9231..a4cf99d6 100644 --- a/internal/json/json_marshal.go +++ b/internal/json/json_marshal.go @@ -4,8 +4,8 @@ import ( "encoding/json" "fmt" - "github.com/cedar-policy/cedar-go/internal/ast" "github.com/cedar-policy/cedar-go/types" + "github.com/cedar-policy/cedar-go/x/exp/ast" ) func (s *scopeJSON) FromNode(src ast.IsScopeNode) { diff --git a/internal/json/json_test.go b/internal/json/json_test.go index 64c714e6..ed1f12e9 100644 --- a/internal/json/json_test.go +++ b/internal/json/json_test.go @@ -5,9 +5,9 @@ import ( "net/netip" "testing" - "github.com/cedar-policy/cedar-go/internal/ast" "github.com/cedar-policy/cedar-go/internal/testutil" "github.com/cedar-policy/cedar-go/types" + "github.com/cedar-policy/cedar-go/x/exp/ast" ) func TestUnmarshalJSON(t *testing.T) { diff --git a/internal/json/json_unmarshal.go b/internal/json/json_unmarshal.go index a5299a65..3f2cba80 100644 --- a/internal/json/json_unmarshal.go +++ b/internal/json/json_unmarshal.go @@ -6,10 +6,10 @@ import ( "fmt" "strings" - "github.com/cedar-policy/cedar-go/internal/ast" "github.com/cedar-policy/cedar-go/internal/consts" "github.com/cedar-policy/cedar-go/internal/extensions" "github.com/cedar-policy/cedar-go/types" + "github.com/cedar-policy/cedar-go/x/exp/ast" ) type isPrincipalResourceScopeNode interface { diff --git a/internal/parser/cedar_marshal.go b/internal/parser/cedar_marshal.go index cf39f7e4..0957d53c 100644 --- a/internal/parser/cedar_marshal.go +++ b/internal/parser/cedar_marshal.go @@ -4,9 +4,9 @@ import ( "bytes" "fmt" - "github.com/cedar-policy/cedar-go/internal/ast" "github.com/cedar-policy/cedar-go/internal/consts" "github.com/cedar-policy/cedar-go/internal/extensions" + "github.com/cedar-policy/cedar-go/x/exp/ast" ) func (p *Policy) MarshalCedar(buf *bytes.Buffer) { diff --git a/internal/parser/cedar_parse_test.go b/internal/parser/cedar_parse_test.go index 28593964..4b77c24f 100644 --- a/internal/parser/cedar_parse_test.go +++ b/internal/parser/cedar_parse_test.go @@ -4,9 +4,9 @@ import ( "bytes" "testing" - "github.com/cedar-policy/cedar-go/internal/ast" "github.com/cedar-policy/cedar-go/internal/parser" "github.com/cedar-policy/cedar-go/internal/testutil" + "github.com/cedar-policy/cedar-go/x/exp/ast" ) func TestParse(t *testing.T) { diff --git a/internal/parser/cedar_tokenize.go b/internal/parser/cedar_tokenize.go index 1c634da3..11bca615 100644 --- a/internal/parser/cedar_tokenize.go +++ b/internal/parser/cedar_tokenize.go @@ -9,8 +9,8 @@ import ( "strings" "unicode/utf8" - "github.com/cedar-policy/cedar-go/internal/ast" "github.com/cedar-policy/cedar-go/internal/rust" + "github.com/cedar-policy/cedar-go/x/exp/ast" ) //go:generate moq -pkg parser -fmt goimports -out tokenize_mocks_test.go . reader diff --git a/internal/parser/cedar_unmarshal.go b/internal/parser/cedar_unmarshal.go index 1971b40c..c255c375 100644 --- a/internal/parser/cedar_unmarshal.go +++ b/internal/parser/cedar_unmarshal.go @@ -5,11 +5,11 @@ import ( "strconv" "strings" - "github.com/cedar-policy/cedar-go/internal/ast" "github.com/cedar-policy/cedar-go/internal/consts" "github.com/cedar-policy/cedar-go/internal/extensions" "github.com/cedar-policy/cedar-go/internal/mapset" "github.com/cedar-policy/cedar-go/types" + "github.com/cedar-policy/cedar-go/x/exp/ast" ) func (p *PolicySlice) UnmarshalCedar(b []byte) error { diff --git a/internal/parser/cedar_unmarshal_test.go b/internal/parser/cedar_unmarshal_test.go index a654638d..5f349ac2 100644 --- a/internal/parser/cedar_unmarshal_test.go +++ b/internal/parser/cedar_unmarshal_test.go @@ -5,10 +5,10 @@ import ( "strings" "testing" - "github.com/cedar-policy/cedar-go/internal/ast" "github.com/cedar-policy/cedar-go/internal/parser" "github.com/cedar-policy/cedar-go/internal/testutil" "github.com/cedar-policy/cedar-go/types" + "github.com/cedar-policy/cedar-go/x/exp/ast" ) var johnny = types.EntityUID{ diff --git a/internal/parser/internal_test.go b/internal/parser/internal_test.go index 1079d1a5..c4e3c84e 100644 --- a/internal/parser/internal_test.go +++ b/internal/parser/internal_test.go @@ -3,8 +3,8 @@ package parser import ( "testing" - "github.com/cedar-policy/cedar-go/internal/ast" "github.com/cedar-policy/cedar-go/internal/testutil" + "github.com/cedar-policy/cedar-go/x/exp/ast" ) func TestScopeToNode(t *testing.T) { diff --git a/internal/parser/node.go b/internal/parser/node.go index 0e8dada8..36e55b0c 100644 --- a/internal/parser/node.go +++ b/internal/parser/node.go @@ -3,7 +3,7 @@ package parser import ( "bytes" - "github.com/cedar-policy/cedar-go/internal/ast" + "github.com/cedar-policy/cedar-go/x/exp/ast" ) type NodeTypeIf struct{ ast.NodeTypeIfThenElse } diff --git a/internal/parser/policy.go b/internal/parser/policy.go index 1f3d1b48..b625cf13 100644 --- a/internal/parser/policy.go +++ b/internal/parser/policy.go @@ -1,6 +1,6 @@ package parser -import "github.com/cedar-policy/cedar-go/internal/ast" +import "github.com/cedar-policy/cedar-go/x/exp/ast" type PolicySlice []*Policy type Policy ast.Policy diff --git a/policy.go b/policy.go index b9fc039b..152b714a 100644 --- a/policy.go +++ b/policy.go @@ -4,11 +4,11 @@ import ( "bytes" "github.com/cedar-policy/cedar-go/ast" - internalast "github.com/cedar-policy/cedar-go/internal/ast" "github.com/cedar-policy/cedar-go/internal/eval" "github.com/cedar-policy/cedar-go/internal/json" "github.com/cedar-policy/cedar-go/internal/parser" "github.com/cedar-policy/cedar-go/types" + internalast "github.com/cedar-policy/cedar-go/x/exp/ast" ) // A Policy is the parsed form of a single Cedar language policy statement. diff --git a/policy_list.go b/policy_list.go index 225dec1b..835d170d 100644 --- a/policy_list.go +++ b/policy_list.go @@ -4,8 +4,8 @@ import ( "bytes" "fmt" - internalast "github.com/cedar-policy/cedar-go/internal/ast" "github.com/cedar-policy/cedar-go/internal/parser" + internalast "github.com/cedar-policy/cedar-go/x/exp/ast" ) // PolicyList represents a list of un-named Policy's. Cedar documents, unlike the PolicySet form, don't have a means of diff --git a/policy_set.go b/policy_set.go index fad84204..2695c4bd 100644 --- a/policy_set.go +++ b/policy_set.go @@ -8,9 +8,9 @@ import ( "maps" "slices" - internalast "github.com/cedar-policy/cedar-go/internal/ast" internaljson "github.com/cedar-policy/cedar-go/internal/json" "github.com/cedar-policy/cedar-go/types" + internalast "github.com/cedar-policy/cedar-go/x/exp/ast" ) type PolicyID = types.PolicyID diff --git a/internal/ast/annotation.go b/x/exp/ast/annotation.go similarity index 100% rename from internal/ast/annotation.go rename to x/exp/ast/annotation.go diff --git a/internal/ast/ast_test.go b/x/exp/ast/ast_test.go similarity index 99% rename from internal/ast/ast_test.go rename to x/exp/ast/ast_test.go index fbce2c8d..b5998490 100644 --- a/internal/ast/ast_test.go +++ b/x/exp/ast/ast_test.go @@ -5,9 +5,9 @@ import ( "testing" "time" - "github.com/cedar-policy/cedar-go/internal/ast" "github.com/cedar-policy/cedar-go/internal/testutil" "github.com/cedar-policy/cedar-go/types" + "github.com/cedar-policy/cedar-go/x/exp/ast" ) // These tests serve mostly as examples of how to translate from Cedar text into programmatic AST construction. They diff --git a/x/exp/ast/doc.go b/x/exp/ast/doc.go new file mode 100644 index 00000000..8475cd11 --- /dev/null +++ b/x/exp/ast/doc.go @@ -0,0 +1,21 @@ +/* +The AST package exposes the internal AST used within cedar-go. This AST is +subject to change. The AST is most useful for analyzing existing policies +created by the Cedar / JSON parser or created using the external AST. The +external AST is a type definition of the internal AST, so you can cast from the +external to internal types. + +Example: + + import ( + "github.com/cedar-policy/cedar-go/ast" + internalast "github.com/cedar-policy/cedar-go/x/exp/ast" + ) + + func main() { + policy := ast.Permit() + internal := (*internalast.Policy)(policy) + _ = internal + } +*/ +package ast diff --git a/internal/ast/internal_test.go b/x/exp/ast/internal_test.go similarity index 100% rename from internal/ast/internal_test.go rename to x/exp/ast/internal_test.go diff --git a/internal/ast/node.go b/x/exp/ast/node.go similarity index 100% rename from internal/ast/node.go rename to x/exp/ast/node.go diff --git a/internal/ast/operator.go b/x/exp/ast/operator.go similarity index 100% rename from internal/ast/operator.go rename to x/exp/ast/operator.go diff --git a/internal/ast/policy.go b/x/exp/ast/policy.go similarity index 100% rename from internal/ast/policy.go rename to x/exp/ast/policy.go diff --git a/internal/ast/scope.go b/x/exp/ast/scope.go similarity index 100% rename from internal/ast/scope.go rename to x/exp/ast/scope.go diff --git a/internal/ast/value.go b/x/exp/ast/value.go similarity index 100% rename from internal/ast/value.go rename to x/exp/ast/value.go diff --git a/internal/ast/variable.go b/x/exp/ast/variable.go similarity index 100% rename from internal/ast/variable.go rename to x/exp/ast/variable.go diff --git a/x/exp/batch/batch.go b/x/exp/batch/batch.go index d65e36ce..9ae02165 100644 --- a/x/exp/batch/batch.go +++ b/x/exp/batch/batch.go @@ -14,11 +14,11 @@ import ( "slices" "github.com/cedar-policy/cedar-go" - "github.com/cedar-policy/cedar-go/internal/ast" "github.com/cedar-policy/cedar-go/internal/consts" "github.com/cedar-policy/cedar-go/internal/eval" "github.com/cedar-policy/cedar-go/internal/mapset" "github.com/cedar-policy/cedar-go/types" + "github.com/cedar-policy/cedar-go/x/exp/ast" ) // Ignore returns a value that should be ignored during batch evaluation. diff --git a/x/exp/batch/batch_test.go b/x/exp/batch/batch_test.go index aad2d945..47adbb26 100644 --- a/x/exp/batch/batch_test.go +++ b/x/exp/batch/batch_test.go @@ -9,10 +9,10 @@ import ( "github.com/cedar-policy/cedar-go" publicast "github.com/cedar-policy/cedar-go/ast" - "github.com/cedar-policy/cedar-go/internal/ast" "github.com/cedar-policy/cedar-go/internal/mapset" "github.com/cedar-policy/cedar-go/internal/testutil" "github.com/cedar-policy/cedar-go/types" + "github.com/cedar-policy/cedar-go/x/exp/ast" ) func TestBatch(t *testing.T) {