diff --git a/pkg/composableschemadsl/compiler/importer-test/simple-local-with-extra-def/expected.zed b/pkg/composableschemadsl/compiler/importer-test/simple-local-with-extra-def/expected.zed new file mode 100644 index 0000000000..b7f82df684 --- /dev/null +++ b/pkg/composableschemadsl/compiler/importer-test/simple-local-with-extra-def/expected.zed @@ -0,0 +1,6 @@ +definition user {} + +definition resource { + relation viewer: user + permission view = viewer +} \ No newline at end of file diff --git a/pkg/composableschemadsl/compiler/importer-test/simple-local-with-extra-def/root.zed b/pkg/composableschemadsl/compiler/importer-test/simple-local-with-extra-def/root.zed new file mode 100644 index 0000000000..321bccb119 --- /dev/null +++ b/pkg/composableschemadsl/compiler/importer-test/simple-local-with-extra-def/root.zed @@ -0,0 +1,6 @@ +from .user import user + +definition resource { + relation viewer: user + permission view = viewer +} diff --git a/pkg/composableschemadsl/compiler/importer-test/simple-local-with-extra-def/user.zed b/pkg/composableschemadsl/compiler/importer-test/simple-local-with-extra-def/user.zed new file mode 100644 index 0000000000..cbe74631d2 --- /dev/null +++ b/pkg/composableschemadsl/compiler/importer-test/simple-local-with-extra-def/user.zed @@ -0,0 +1,9 @@ +definition user {} + +// This shouldn't be present in the output +definition persona {} + +// Nor should this +caveat only_on_tuesday(day_of_week string) { + day_of_week == 'tuesday' +} diff --git a/pkg/composableschemadsl/compiler/importer_test.go b/pkg/composableschemadsl/compiler/importer_test.go index 03475359e4..4480a0db1a 100644 --- a/pkg/composableschemadsl/compiler/importer_test.go +++ b/pkg/composableschemadsl/compiler/importer_test.go @@ -55,6 +55,7 @@ func TestImporter(t *testing.T) { importerTests := []importerTest{ {"simple local import", "simple-local"}, {"simple local import with transitive hop", "simple-local-with-hop"}, + {"simple local import with extra def", "simple-local-with-extra-def"}, {"nested local import", "nested-local"}, {"nested local import with transitive hop", "nested-local-with-hop"}, {"nested local two layers deep import", "nested-two-layer-local"}, diff --git a/pkg/composableschemadsl/compiler/translator.go b/pkg/composableschemadsl/compiler/translator.go index 66fd63a810..fc6d806328 100644 --- a/pkg/composableschemadsl/compiler/translator.go +++ b/pkg/composableschemadsl/compiler/translator.go @@ -694,13 +694,10 @@ func addWithCaveats(tctx translationContext, typeRefNode *dslNode, ref *core.All } func translateImport(tctx translationContext, importNode *dslNode, names *mapz.Set[string]) (*CompiledSchema, error) { - // NOTE: this function currently just grabs everything that's in the target file. - // TODO: only grab the requested definitions - // TODO: import cycle tracking + // Get the filepath segments out of the AST nodes pathNodes := importNode.List(dslshape.NodeImportPredicatePathSegment) pathSegments := make([]string, 0, len(pathNodes)) - // Get the filepath segments out of the AST nodes for _, pathSegmentNode := range pathNodes { segment, err := pathSegmentNode.GetString(dslshape.NodeIdentiferPredicateValue) if err != nil { @@ -709,9 +706,23 @@ func translateImport(tctx translationContext, importNode *dslNode, names *mapz.S pathSegments = append(pathSegments, segment) } + // Get requested definitions out of the AST nodes + requestedDefinitionNodes := importNode.List(dslshape.NodeImportPredicateDefinitionName) + requestedDefinitions := make([]string, 0, len(requestedDefinitionNodes)) + + for _, requestedDefinitionNode := range requestedDefinitionNodes { + requested, err := requestedDefinitionNode.GetString(dslshape.NodeIdentiferPredicateValue) + if err != nil { + return nil, err + } + requestedDefinitions = append(requestedDefinitions, requested) + } + + return importFile(importContext{ names: names, pathSegments: pathSegments, + requestedDefinitions: requestedDefinitions, sourceFolder: tctx.sourceFolder, }) }