-
Notifications
You must be signed in to change notification settings - Fork 279
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 #2116 from authzed/update-compiler-to-consume-imports
Update compiler to consume local imports
- Loading branch information
Showing
25 changed files
with
301 additions
and
18 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
1 change: 1 addition & 0 deletions
1
internal/services/integrationtesting/testconfigs/caveatmultiplebranchessamerel.yaml
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
--- | ||
schema: >- | ||
definition user {} | ||
|
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,5 @@ | ||
# Test Structure | ||
|
||
Every folder should have a `root.zed`. This is the target for compilation. | ||
|
||
Every folder will have an `expected.zed`, which is the output of the compilation process. |
6 changes: 6 additions & 0 deletions
6
pkg/composableschemadsl/compiler/importer-test/nested-local-with-hop/expected.zed
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,6 @@ | ||
definition user {} | ||
|
||
definition resource { | ||
relation viewer: user | ||
permission view = viewer | ||
} |
6 changes: 6 additions & 0 deletions
6
pkg/composableschemadsl/compiler/importer-test/nested-local-with-hop/root.zed
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,6 @@ | ||
from .transitive.transitive import user | ||
|
||
definition resource { | ||
relation viewer: user | ||
permission view = viewer | ||
} |
1 change: 1 addition & 0 deletions
1
...omposableschemadsl/compiler/importer-test/nested-local-with-hop/transitive/transitive.zed
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 @@ | ||
from .user.user import user |
1 change: 1 addition & 0 deletions
1
...composableschemadsl/compiler/importer-test/nested-local-with-hop/transitive/user/user.zed
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 @@ | ||
definition user {} |
6 changes: 6 additions & 0 deletions
6
pkg/composableschemadsl/compiler/importer-test/nested-local/expected.zed
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,6 @@ | ||
definition user {} | ||
|
||
definition resource { | ||
relation viewer: user | ||
permission view = viewer | ||
} |
6 changes: 6 additions & 0 deletions
6
pkg/composableschemadsl/compiler/importer-test/nested-local/root.zed
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,6 @@ | ||
from .user.user import user | ||
|
||
definition resource { | ||
relation viewer: user | ||
permission view = viewer | ||
} |
1 change: 1 addition & 0 deletions
1
pkg/composableschemadsl/compiler/importer-test/nested-local/user/user.zed
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 @@ | ||
definition user {} |
1 change: 1 addition & 0 deletions
1
...mposableschemadsl/compiler/importer-test/nested-two-layer-local/definitions/user/user.zed
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 @@ | ||
definition user {} |
6 changes: 6 additions & 0 deletions
6
pkg/composableschemadsl/compiler/importer-test/nested-two-layer-local/expected.zed
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,6 @@ | ||
definition user {} | ||
|
||
definition resource { | ||
relation viewer: user | ||
permission view = viewer | ||
} |
6 changes: 6 additions & 0 deletions
6
pkg/composableschemadsl/compiler/importer-test/nested-two-layer-local/root.zed
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,6 @@ | ||
from .definitions.user.user import user | ||
|
||
definition resource { | ||
relation viewer: user | ||
permission view = viewer | ||
} |
6 changes: 6 additions & 0 deletions
6
pkg/composableschemadsl/compiler/importer-test/simple-local-with-hop/expected.zed
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,6 @@ | ||
definition user {} | ||
|
||
definition resource { | ||
relation viewer: user | ||
permission view = viewer | ||
} |
1 change: 1 addition & 0 deletions
1
pkg/composableschemadsl/compiler/importer-test/simple-local-with-hop/indirection.zed
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 @@ | ||
from .user import user |
6 changes: 6 additions & 0 deletions
6
pkg/composableschemadsl/compiler/importer-test/simple-local-with-hop/root.zed
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,6 @@ | ||
from .indirection import user | ||
|
||
definition resource { | ||
relation viewer: user | ||
permission view = viewer | ||
} |
1 change: 1 addition & 0 deletions
1
pkg/composableschemadsl/compiler/importer-test/simple-local-with-hop/user.zed
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 @@ | ||
definition user {} |
6 changes: 6 additions & 0 deletions
6
pkg/composableschemadsl/compiler/importer-test/simple-local/expected.zed
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,6 @@ | ||
definition user {} | ||
|
||
definition resource { | ||
relation viewer: user | ||
permission view = viewer | ||
} |
6 changes: 6 additions & 0 deletions
6
pkg/composableschemadsl/compiler/importer-test/simple-local/root.zed
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,6 @@ | ||
from .user import user | ||
|
||
definition resource { | ||
relation viewer: user | ||
permission view = viewer | ||
} |
1 change: 1 addition & 0 deletions
1
pkg/composableschemadsl/compiler/importer-test/simple-local/user.zed
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 @@ | ||
definition user {} |
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,52 @@ | ||
package compiler | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
|
||
"github.com/rs/zerolog/log" | ||
|
||
"github.com/authzed/spicedb/pkg/composableschemadsl/input" | ||
"github.com/authzed/spicedb/pkg/genutil/mapz" | ||
) | ||
|
||
type importContext struct { | ||
pathSegments []string | ||
sourceFolder string | ||
names *mapz.Set[string] | ||
} | ||
|
||
const SchemaFileSuffix = ".zed" | ||
|
||
func importFile(importContext importContext) (*CompiledSchema, error) { | ||
relativeFilepath := constructFilePath(importContext.pathSegments) | ||
filePath := path.Join(importContext.sourceFolder, relativeFilepath) | ||
|
||
newSourceFolder := filepath.Dir(filePath) | ||
|
||
var schemaBytes []byte | ||
schemaBytes, err := os.ReadFile(filePath) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read schema file: %w", err) | ||
} | ||
log.Trace().Str("schema", string(schemaBytes)).Str("file", filePath).Msg("read schema from file") | ||
|
||
compiled, err := compileImpl(InputSchema{ | ||
Source: input.Source(filePath), | ||
SchemaString: string(schemaBytes), | ||
}, | ||
importContext.names, | ||
AllowUnprefixedObjectType(), | ||
SourceFolder(newSourceFolder), | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return compiled, nil | ||
} | ||
|
||
func constructFilePath(segments []string) string { | ||
return path.Join(segments...) + SchemaFileSuffix | ||
} |
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,91 @@ | ||
package compiler_test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/authzed/spicedb/pkg/composableschemadsl/compiler" | ||
"github.com/authzed/spicedb/pkg/composableschemadsl/generator" | ||
"github.com/authzed/spicedb/pkg/composableschemadsl/input" | ||
) | ||
|
||
type importerTest struct { | ||
name string | ||
folder string | ||
} | ||
|
||
func (it *importerTest) input() string { | ||
b, err := os.ReadFile(fmt.Sprintf("importer-test/%s/root.zed", it.folder)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return string(b) | ||
} | ||
|
||
func (it *importerTest) relativePath() string { | ||
return fmt.Sprintf("importer-test/%s", it.folder) | ||
} | ||
|
||
func (it *importerTest) expected() string { | ||
b, err := os.ReadFile(fmt.Sprintf("importer-test/%s/expected.zed", it.folder)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return string(b) | ||
} | ||
|
||
func (it *importerTest) writeExpected(schema string) { | ||
err := os.WriteFile(fmt.Sprintf("importer-test/%s/expected.zed", it.folder), []byte(schema), 0o_600) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func TestImporter(t *testing.T) { | ||
workingDir, err := os.Getwd() | ||
require.NoError(t, err) | ||
|
||
importerTests := []importerTest{ | ||
{"simple local import", "simple-local"}, | ||
{"simple local import with transitive hop", "simple-local-with-hop"}, | ||
{"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"}, | ||
} | ||
|
||
for _, test := range importerTests { | ||
t.Run(test.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
sourceFolder := path.Join(workingDir, test.relativePath()) | ||
|
||
inputSchema := test.input() | ||
|
||
compiled, err := compiler.Compile(compiler.InputSchema{ | ||
Source: input.Source("schema"), | ||
SchemaString: inputSchema, | ||
}, compiler.AllowUnprefixedObjectType(), | ||
compiler.SourceFolder(sourceFolder)) | ||
require.NoError(t, err) | ||
|
||
generated, _, err := generator.GenerateSchema(compiled.OrderedDefinitions) | ||
require.NoError(t, err) | ||
|
||
if os.Getenv("REGEN") == "true" { | ||
test.writeExpected(generated) | ||
} else { | ||
expected := test.expected() | ||
if !assert.Equal(t, expected, generated, test.name) { | ||
t.Log(generated) | ||
} | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.