Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/lsp/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func (s *Server) textDocHover(_ context.Context, r *jsonrpc2.Request) (*Hover, e
return hoverContents, nil
}

func (s *Server) textDocFormat(_ context.Context, r *jsonrpc2.Request) ([]lsp.TextEdit, error) {
func (s *Server) textDocFormat(ctx context.Context, r *jsonrpc2.Request) ([]lsp.TextEdit, error) {
params, err := unmarshalParams[lsp.DocumentFormattingParams](r)
if err != nil {
return nil, err
Expand All @@ -295,7 +295,7 @@ func (s *Server) textDocFormat(_ context.Context, r *jsonrpc2.Request) ([]lsp.Te
return err
}

formattedSchema, _, err := generator.GenerateSchema(compiled.OrderedDefinitions)
formattedSchema, _, err := generator.GenerateSchema(ctx, compiled.OrderedDefinitions)
if err != nil {
return err
}
Expand Down
10 changes: 10 additions & 0 deletions internal/services/shared/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"context"
"maps"

"go.opentelemetry.io/otel"

log "github.com/authzed/spicedb/internal/logging"
"github.com/authzed/spicedb/internal/namespace"
caveattypes "github.com/authzed/spicedb/pkg/caveats/types"
Expand All @@ -21,6 +23,8 @@
"github.com/authzed/spicedb/pkg/tuple"
)

var tracer = otel.Tracer("spicedb/internal/services/shared")

// ValidatedSchemaChanges is a set of validated schema changes that can be applied to the datastore.
type ValidatedSchemaChanges struct {
compiled *compiler.CompiledSchema
Expand All @@ -36,6 +40,8 @@
// ValidateSchemaChanges validates the schema found in the compiled schema and returns a
// ValidatedSchemaChanges, if fully validated.
func ValidateSchemaChanges(ctx context.Context, compiled *compiler.CompiledSchema, caveatTypeSet *caveattypes.TypeSet, additiveOnly bool, schemaText string) (*ValidatedSchemaChanges, error) {
ctx, span := tracer.Start(ctx, "schema.ValidateSchemaChanges")
defer span.End()

Check warning on line 44 in internal/services/shared/schema.go

View check run for this annotation

Codecov / codecov/patch

internal/services/shared/schema.go#L43-L44

Added lines #L43 - L44 were not covered by tests
// 1) Validate the caveats defined.
newCaveatDefNames := mapz.NewSet[string]()
for _, caveatDef := range compiled.CaveatDefinitions {
Expand Down Expand Up @@ -94,6 +100,8 @@
// ApplySchemaChanges applies schema changes found in the validated changes struct, via the specified
// ReadWriteTransaction.
func ApplySchemaChanges(ctx context.Context, rwt datalayer.ReadWriteTransaction, caveatTypeSet *caveattypes.TypeSet, validated *ValidatedSchemaChanges) (*AppliedSchemaChanges, error) {
ctx, span := tracer.Start(ctx, "schema.ApplySchemaChanges")
defer span.End()

Check warning on line 104 in internal/services/shared/schema.go

View check run for this annotation

Codecov / codecov/patch

internal/services/shared/schema.go#L103-L104

Added lines #L103 - L104 were not covered by tests
sr, err := rwt.ReadSchema(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -132,6 +140,8 @@
existingCaveats []*core.CaveatDefinition,
existingObjectDefs []*core.NamespaceDefinition,
) (*AppliedSchemaChanges, error) {
ctx, span := tracer.Start(ctx, "schema.ApplySchemaChangesOverExisting")
defer span.End()

Check warning on line 144 in internal/services/shared/schema.go

View check run for this annotation

Codecov / codecov/patch

internal/services/shared/schema.go#L143-L144

Added lines #L143 - L144 were not covered by tests
// Build a map of existing caveats to determine those being removed, if any.
existingCaveatDefMap := make(map[string]*core.CaveatDefinition, len(existingCaveats))
existingCaveatDefNames := mapz.NewSet[string]()
Expand Down
2 changes: 1 addition & 1 deletion internal/services/v1/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
defs = append(defs, ns.Definition)
}

schema, _, err := generator.GenerateSchema(defs)
schema, _, err := generator.GenerateSchema(ctx, defs)

Check warning on line 63 in internal/services/v1/debug.go

View check run for this annotation

Codecov / codecov/patch

internal/services/v1/debug.go#L63

Added line #L63 was not covered by tests
if err != nil {
return "", err
}
Expand Down
6 changes: 6 additions & 0 deletions internal/services/v1/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

"buf.build/go/protovalidate"
grpcvalidate "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/protovalidate"
"go.opentelemetry.io/otel"

v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"

Expand All @@ -29,6 +30,8 @@
"github.com/authzed/spicedb/pkg/zedtoken"
)

var tracer = otel.Tracer("spicedb/internal/services/v1/schema")

type SchemaServerConfig struct {
// CaveatTypeSet is the set of caveat types that are allowed in the schema.
CaveatTypeSet *caveattypes.TypeSet
Expand Down Expand Up @@ -142,13 +145,16 @@
// the user must first compile them with `zed`
opts = append(opts, compiler.DisallowImportFlag())

_, span := tracer.Start(ctx, "compile")

Check warning on line 148 in internal/services/v1/schema.go

View check run for this annotation

Codecov / codecov/patch

internal/services/v1/schema.go#L148

Added line #L148 was not covered by tests
compiled, err := compiler.Compile(compiler.InputSchema{
Source: input.Source("schema"),
SchemaString: in.GetSchema(),
}, compiler.AllowUnprefixedObjectType(), opts...)
if err != nil {
span.End()

Check warning on line 154 in internal/services/v1/schema.go

View check run for this annotation

Codecov / codecov/patch

internal/services/v1/schema.go#L154

Added line #L154 was not covered by tests
return nil, ss.rewriteError(ctx, err)
}
span.End()

Check warning on line 157 in internal/services/v1/schema.go

View check run for this annotation

Codecov / codecov/patch

internal/services/v1/schema.go#L157

Added line #L157 was not covered by tests
log.Ctx(ctx).Trace().Int("objectDefinitions", len(compiled.ObjectDefinitions)).Int("caveatDefinitions", len(compiled.CaveatDefinitions)).Msg("compiled namespace definitions")

// Do as much validation as we can before talking to the datastore.
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 @@ -371,7 +371,7 @@ definition document {
testutil.RequireProtoEqual(t, caveatDef, readCaveatDef, "found changed caveat definition")

// Ensure the read namespace's string form matches the input as an extra check.
generated, _, err := generator.GenerateSchema([]compiler.SchemaDefinition{readCaveatDef, readNsDef})
generated, _, err := generator.GenerateSchema(ctx, []compiler.SchemaDefinition{readCaveatDef, readNsDef})
require.NoError(err)
require.Equal(schemaString, generated)
}
2 changes: 1 addition & 1 deletion pkg/development/wasm/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
func runOperation(devContext *development.DevContext, operation *devinterface.Operation) (*devinterface.OperationResult, error) {
switch {
case operation.FormatSchemaParameters != nil:
formatted, _, err := generator.GenerateSchema(devContext.CompiledSchema.OrderedDefinitions)
formatted, _, err := generator.GenerateSchema(devContext.Ctx, devContext.CompiledSchema.OrderedDefinitions)
if err != nil {
return nil, err
}
Expand Down
35 changes: 18 additions & 17 deletions pkg/schema/v2/builder_example_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package schema_test

import (
"context"
"fmt"
"strings"

Expand Down Expand Up @@ -39,7 +40,7 @@ func ExampleSchemaBuilder() {
Build()

defs, caveats, _ := s.ToDefinitions()
schemaText, _, _ := generator.GenerateSchema(append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
schemaText, _, _ := generator.GenerateSchema(context.Background(), append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
fmt.Println(strings.TrimSpace(schemaText))
}

Expand Down Expand Up @@ -67,7 +68,7 @@ func ExampleSchemaBuilder_incrementalUnion() {
Build()

defs, caveats, _ := s.ToDefinitions()
schemaText, _, _ := generator.GenerateSchema(append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
schemaText, _, _ := generator.GenerateSchema(context.Background(), append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
fmt.Println(strings.TrimSpace(schemaText))
}

Expand Down Expand Up @@ -99,7 +100,7 @@ func ExampleSchemaBuilder_withArrow() {
Build()

defs, caveats, _ := s.ToDefinitions()
schemaText, _, _ := generator.GenerateSchema(append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
schemaText, _, _ := generator.GenerateSchema(context.Background(), append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
fmt.Println(strings.TrimSpace(schemaText))
}

Expand All @@ -119,7 +120,7 @@ func ExampleSchemaBuilder_withCaveat() {
Build()

defs, caveats, _ := s.ToDefinitions()
schemaText, _, _ := generator.GenerateSchema(append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
schemaText, _, _ := generator.GenerateSchema(context.Background(), append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
fmt.Println(strings.TrimSpace(schemaText))
}

Expand Down Expand Up @@ -148,7 +149,7 @@ func ExampleSchemaBuilder_withCaveatTypes() {
Build()

defs, caveats, _ := s.ToDefinitions()
schemaText, _, _ := generator.GenerateSchema(append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
schemaText, _, _ := generator.GenerateSchema(context.Background(), append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
fmt.Println(strings.TrimSpace(schemaText))
}

Expand All @@ -172,7 +173,7 @@ func ExampleSchemaBuilder_withExclusion() {
Build()

defs, caveats, _ := s.ToDefinitions()
schemaText, _, _ := generator.GenerateSchema(append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
schemaText, _, _ := generator.GenerateSchema(context.Background(), append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
fmt.Println(strings.TrimSpace(schemaText))
}

Expand All @@ -196,7 +197,7 @@ func ExampleSchemaBuilder_withIntersection() {
Build()

defs, caveats, _ := s.ToDefinitions()
schemaText, _, _ := generator.GenerateSchema(append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
schemaText, _, _ := generator.GenerateSchema(context.Background(), append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
fmt.Println(strings.TrimSpace(schemaText))
}

Expand All @@ -211,7 +212,7 @@ func ExampleSchemaBuilder_withWildcard() {
Build()

defs, caveats, _ := s.ToDefinitions()
schemaText, _, _ := generator.GenerateSchema(append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
schemaText, _, _ := generator.GenerateSchema(context.Background(), append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
fmt.Println(strings.TrimSpace(schemaText))
}

Expand Down Expand Up @@ -280,7 +281,7 @@ func ExampleSchemaBuilder_complex() {
Build()

defs, caveats, _ := s.ToDefinitions()
schemaText, _, _ := generator.GenerateSchema(append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
schemaText, _, _ := generator.GenerateSchema(context.Background(), append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
fmt.Println(strings.TrimSpace(schemaText))
}

Expand All @@ -306,7 +307,7 @@ func ExampleSchemaBuilder_constructorPattern() {
Build()

defs, caveats, _ := s.ToDefinitions()
schemaText, _, _ := generator.GenerateSchema(append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
schemaText, _, _ := generator.GenerateSchema(context.Background(), append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
fmt.Println(strings.TrimSpace(schemaText))
}

Expand All @@ -327,7 +328,7 @@ func ExampleSchemaBuilder_constructorWithIntersection() {
Build()

defs, caveats, _ := s.ToDefinitions()
schemaText, _, _ := generator.GenerateSchema(append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
schemaText, _, _ := generator.GenerateSchema(context.Background(), append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
fmt.Println(strings.TrimSpace(schemaText))
}

Expand All @@ -348,7 +349,7 @@ func ExampleSchemaBuilder_constructorWithExclusion() {
Build()

defs, caveats, _ := s.ToDefinitions()
schemaText, _, _ := generator.GenerateSchema(append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
schemaText, _, _ := generator.GenerateSchema(context.Background(), append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
fmt.Println(strings.TrimSpace(schemaText))
}

Expand All @@ -374,7 +375,7 @@ func ExampleSchemaBuilder_constructorWithArrow() {
Build()

defs, caveats, _ := s.ToDefinitions()
schemaText, _, _ := generator.GenerateSchema(append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
schemaText, _, _ := generator.GenerateSchema(context.Background(), append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
fmt.Println(strings.TrimSpace(schemaText))
}

Expand All @@ -395,7 +396,7 @@ func ExampleSchemaBuilder_constructorWithIntersectionArrow() {
Build()

defs, caveats, _ := s.ToDefinitions()
schemaText, _, _ := generator.GenerateSchema(append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
schemaText, _, _ := generator.GenerateSchema(context.Background(), append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
fmt.Println(strings.TrimSpace(schemaText))
}

Expand All @@ -418,7 +419,7 @@ func ExampleSchemaBuilder_constructorWithCaveat() {
Build()

defs, caveats, _ := s.ToDefinitions()
schemaText, _, _ := generator.GenerateSchema(append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
schemaText, _, _ := generator.GenerateSchema(context.Background(), append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
fmt.Println(strings.TrimSpace(schemaText))
}

Expand Down Expand Up @@ -475,7 +476,7 @@ func ExampleSchemaBuilder_constructorComplexNested() {
Build()

defs, caveats, _ := s.ToDefinitions()
schemaText, _, _ := generator.GenerateSchema(append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
schemaText, _, _ := generator.GenerateSchema(context.Background(), append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
fmt.Println(strings.TrimSpace(schemaText))
}

Expand All @@ -498,6 +499,6 @@ func ExampleSchemaBuilder_withIntersectionArrow() {
Build()

defs, caveats, _ := s.ToDefinitions()
schemaText, _, _ := generator.GenerateSchema(append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
schemaText, _, _ := generator.GenerateSchema(context.Background(), append(toSchemaDefinitions(defs), toSchemaDefinitions(caveats)...))
fmt.Println(strings.TrimSpace(schemaText))
}
12 changes: 6 additions & 6 deletions pkg/schema/v2/flatten_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ definition folder {
schemaDefinitions = append(schemaDefinitions, caveat)
}

generatedSchema, _, err := generator.GenerateSchema(schemaDefinitions)
generatedSchema, _, err := generator.GenerateSchema(t.Context(), schemaDefinitions)
require.NoError(t, err)

// Verify the generated schema compiles
Expand Down Expand Up @@ -365,7 +365,7 @@ definition folder {
expectedSchemaDefinitions = append(expectedSchemaDefinitions, caveat)
}

expectedGenerated, _, err := generator.GenerateSchema(expectedSchemaDefinitions)
expectedGenerated, _, err := generator.GenerateSchema(t.Context(), expectedSchemaDefinitions)
require.NoError(t, err)

// Compare the two generated schemas
Expand Down Expand Up @@ -674,7 +674,7 @@ definition user {}`,
schemaDefinitions = append(schemaDefinitions, caveat)
}

generatedSchema, _, err := generator.GenerateSchema(schemaDefinitions)
generatedSchema, _, err := generator.GenerateSchema(t.Context(), schemaDefinitions)
require.NoError(t, err)

// Verify the generated schema compiles
Expand Down Expand Up @@ -712,7 +712,7 @@ definition user {}`,
expectedSchemaDefinitions = append(expectedSchemaDefinitions, caveat)
}

expectedGenerated, _, err := generator.GenerateSchema(expectedSchemaDefinitions)
expectedGenerated, _, err := generator.GenerateSchema(t.Context(), expectedSchemaDefinitions)
require.NoError(t, err)

// Compare the two generated schemas
Expand Down Expand Up @@ -1116,7 +1116,7 @@ definition user {}
schemaDefinitions = append(schemaDefinitions, caveat)
}

generatedSchema, _, err := generator.GenerateSchema(schemaDefinitions)
generatedSchema, _, err := generator.GenerateSchema(t.Context(), schemaDefinitions)
require.NoError(t, err)

// Verify the generated schema compiles
Expand Down Expand Up @@ -1154,7 +1154,7 @@ definition user {}
expectedSchemaDefinitions = append(expectedSchemaDefinitions, caveat)
}

expectedGenerated, _, err := generator.GenerateSchema(expectedSchemaDefinitions)
expectedGenerated, _, err := generator.GenerateSchema(t.Context(), expectedSchemaDefinitions)
require.NoError(t, err)

// Compare the two generated schemas
Expand Down
2 changes: 1 addition & 1 deletion pkg/schema/v2/testing/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestExampleRunWithSchemaForTesting(t *testing.T) {
definitions = append(definitions, cd)
}

generated, _, err := generator.GenerateSchema(definitions)
generated, _, err := generator.GenerateSchema(t.Context(), definitions)
require.NoError(t, err)
t.Logf("Generated schema:\n%s", generated)

Expand Down
4 changes: 2 additions & 2 deletions pkg/schemadsl/compiler/importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestImporter(t *testing.T) {
compiler.SourceFolder(sourceFolder))
require.NoError(t, err)

generated, _, err := generator.GenerateSchema(compiled.OrderedDefinitions)
generated, _, err := generator.GenerateSchema(t.Context(), compiled.OrderedDefinitions)
require.NoError(t, err)

if os.Getenv("REGEN") == "true" {
Expand All @@ -107,7 +107,7 @@ func TestImporter(t *testing.T) {
compiler.SourceFS(fsys))
require.NoError(t, err)

generated, _, err := generator.GenerateSchema(compiled.OrderedDefinitions)
generated, _, err := generator.GenerateSchema(t.Context(), compiled.OrderedDefinitions)
require.NoError(t, err)

if os.Getenv("REGEN") == "true" {
Expand Down
4 changes: 2 additions & 2 deletions pkg/schemadsl/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ const Ellipsis = "..."
// MaxSingleLineCommentLength sets the maximum length for a comment to made single line.
const MaxSingleLineCommentLength = 70 // 80 - the comment parts and some padding

func GenerateSchema(definitions []compiler.SchemaDefinition) (string, bool, error) {
return GenerateSchemaWithCaveatTypeSet(context.TODO(), definitions, caveattypes.Default.TypeSet)
func GenerateSchema(ctx context.Context, definitions []compiler.SchemaDefinition) (string, bool, error) {
return GenerateSchemaWithCaveatTypeSet(ctx, definitions, caveattypes.Default.TypeSet)
}

// GenerateSchemaWithCaveatTypeSet generates a DSL view of the given schema.
Expand Down
2 changes: 1 addition & 1 deletion pkg/schemadsl/generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ definition user {
}, compiler.AllowUnprefixedObjectType())
require.NoError(err)

source, _, err := GenerateSchema(compiled.OrderedDefinitions)
source, _, err := GenerateSchema(t.Context(), compiled.OrderedDefinitions)
require.NoError(err)
require.Equal(test.expected, source)
})
Expand Down
Loading