Skip to content

Commit

Permalink
Merge pull request #1683 from authzed/fix-regression-compiler-contract
Browse files Browse the repository at this point in the history
fixes regression in compiler.Compile() contract
  • Loading branch information
vroldanbet authored Dec 13, 2023
2 parents fe6aebc + 5635ad0 commit 8e677b2
Show file tree
Hide file tree
Showing 16 changed files with 325 additions and 80 deletions.
2 changes: 1 addition & 1 deletion internal/graph/computed/computecheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ func writeCaveatedTuples(ctx context.Context, _ *testing.T, ds datastore.Datasto
compiled, err := compiler.Compile(compiler.InputSchema{
Source: "schema",
SchemaString: schema,
})
}, compiler.AllowUnprefixedObjectType())
if err != nil {
return datastore.NoRevision, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/namespace/annotate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestAnnotateNamespace(t *testing.T) {
permission other = editor - viewer
permission also_aliased = viewer
}`,
})
}, compiler.AllowUnprefixedObjectType())
require.NoError(err)

lastRevision, err := ds.HeadRevision(context.Background())
Expand Down
2 changes: 1 addition & 1 deletion internal/namespace/canonicalization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ func TestCanonicalizationComparison(t *testing.T) {
compiled, err := compiler.Compile(compiler.InputSchema{
Source: input.Source("schema"),
SchemaString: schemaText,
})
}, compiler.AllowUnprefixedObjectType())
require.NoError(err)

lastRevision, err := ds.HeadRevision(context.Background())
Expand Down
2 changes: 1 addition & 1 deletion internal/services/shared/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestApplySchemaChanges(t *testing.T) {
value == 22
}
`,
})
}, compiler.AllowUnprefixedObjectType())
require.NoError(err)

validated, err := ValidateSchemaChanges(context.Background(), compiled, false)
Expand Down
2 changes: 1 addition & 1 deletion internal/services/v1/permissions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ func TestCheckPermissionWithDebugInfo(t *testing.T) {
compiled, err := compiler.Compile(compiler.InputSchema{
Source: input.Source("schema"),
SchemaString: debugInfo.SchemaUsed,
})
}, compiler.AllowUnprefixedObjectType())
require.NoError(err, "Invalid schema: %s", debugInfo.SchemaUsed)
require.Equal(4, len(compiled.OrderedDefinitions))
}
Expand Down
2 changes: 1 addition & 1 deletion internal/services/v1/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (ss *schemaServer) WriteSchema(ctx context.Context, in *v1.WriteSchemaReque
compiled, err := compiler.Compile(compiler.InputSchema{
Source: input.Source("schema"),
SchemaString: in.GetSchema(),
})
}, compiler.AllowUnprefixedObjectType())
if err != nil {
return nil, ss.rewriteError(ctx, err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/testfixtures/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func DatastoreFromSchemaAndTestRelationships(ds datastore.Datastore, schema stri
compiled, err := compiler.Compile(compiler.InputSchema{
Source: input.Source("schema"),
SchemaString: schema,
})
}, compiler.AllowUnprefixedObjectType())
require.NoError(err)

_ = writeDefinitions(validating, require, compiled.ObjectDefinitions, compiled.CaveatDefinitions)
Expand Down
2 changes: 1 addition & 1 deletion pkg/datastore/test/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ definition document {
compiled, err := compiler.Compile(compiler.InputSchema{
Source: input.Source("schema"),
SchemaString: schemaString,
})
}, compiler.AllowUnprefixedObjectType())
require.NoError(err)
require.Equal(2, len(compiled.OrderedDefinitions))

Expand Down
2 changes: 1 addition & 1 deletion pkg/development/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func CompileSchema(schema string) (*compiler.CompiledSchema, *devinterface.Devel
compiled, err := compiler.Compile(compiler.InputSchema{
Source: input.Source("schema"),
SchemaString: schema,
})
}, compiler.AllowUnprefixedObjectType())

var contextError compiler.ErrorWithContext
if errors.As(err, &contextError) {
Expand Down
17 changes: 13 additions & 4 deletions pkg/schemadsl/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,24 @@ type config struct {
}

func SkipValidation() Option { return func(cfg *config) { cfg.skipValidation = true } }
func ObjectTypePrefix(prefix *string) Option {
return func(cfg *config) { cfg.objectTypePrefix = prefix }

func ObjectTypePrefix(prefix string) ObjectPrefixOption {
return func(cfg *config) { cfg.objectTypePrefix = &prefix }
}

func AllowUnprefixedObjectType() ObjectPrefixOption {
return func(cfg *config) { cfg.objectTypePrefix = new(string) }
}

type Option func(*config)

type ObjectPrefixOption func(*config)

// Compile compilers the input schema into a set of namespace definition protos.
func Compile(schema InputSchema, opts ...Option) (*CompiledSchema, error) {
cfg := &config{objectTypePrefix: new(string)}
func Compile(schema InputSchema, prefix ObjectPrefixOption, opts ...Option) (*CompiledSchema, error) {
cfg := &config{}
prefix(cfg) // required option

for _, fn := range opts {
fn(cfg)
}
Expand Down
Loading

0 comments on commit 8e677b2

Please sign in to comment.