schema

package
v1.46.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 24, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

schema/v2 provides a convenient Go representation of SpiceDB's schema definitions, built on top of the raw protocol buffer types from core.v1.

This package converts low-level protobuf structures (NamespaceDefinition, CaveatDefinition) into ergonomic Schema views that are easier to reason about and work with programatically. The basic types map very closely to the schema language itself.

The proto remains the source of truth; this makes traversing the schema to build queries or determine reachability much easier.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WalkBaseRelation added in v1.46.1

func WalkBaseRelation[T any](br *BaseRelation, v Visitor[T], value T) (T, error)

WalkBaseRelation walks a base relation. Returns the final value and error if any visitor returns an error.

func WalkCaveat added in v1.46.1

func WalkCaveat[T any](c *Caveat, v Visitor[T], value T) (T, error)

WalkCaveat walks a caveat. Returns the final value and error if any visitor returns an error.

func WalkDefinition added in v1.46.1

func WalkDefinition[T any](d *Definition, v Visitor[T], value T) (T, error)

WalkDefinition walks a definition and its relations and permissions. Returns the final value and error if any visitor returns an error.

func WalkFlattenedSchema added in v1.46.1

func WalkFlattenedSchema[T any](fs *FlattenedSchema, v Visitor[T], value T) (T, error)

WalkFlattenedSchema walks the flattened schema tree, calling appropriate visitor methods on the provided Visitor for each node encountered. This is a convenience function that delegates to WalkSchema on the underlying schema.

func WalkOperation added in v1.46.1

func WalkOperation[T any](op Operation, v Visitor[T], value T) (T, error)

WalkOperation walks an operation tree recursively. Returns the final value and error if any visitor returns an error.

func WalkPermission added in v1.46.1

func WalkPermission[T any](p *Permission, v Visitor[T], value T) (T, error)

WalkPermission walks a permission and its operation tree. Returns the final value and error if any visitor returns an error.

func WalkRelation added in v1.46.1

func WalkRelation[T any](r *Relation, v Visitor[T], value T) (T, error)

WalkRelation walks a relation and its base relations. Returns the final value and error if any visitor returns an error.

func WalkResolvedSchema added in v1.46.1

func WalkResolvedSchema[T any](rs *ResolvedSchema, v Visitor[T], value T) (T, error)

WalkResolvedSchema walks the resolved schema tree, calling appropriate visitor methods on the provided Visitor for each node encountered. This is a convenience function that delegates to WalkSchema on the underlying schema.

func WalkSchema added in v1.46.1

func WalkSchema[T any](s *Schema, v Visitor[T], value T) (T, error)

WalkSchema walks the entire schema tree, calling appropriate visitor methods on the provided Visitor for each node encountered. Returns the final value and error if any visitor returns an error.

Types

type ArrowOperation added in v1.46.2

type ArrowOperation interface {
	Operation

	// Left returns the relation on the resource (left side of the arrow).
	Left() string

	// Right returns the relation/permission on the subject (right side of the arrow).
	Right() string

	// Function returns the function type applied to the arrow.
	// For standard arrows (->), this returns FunctionTypeAny.
	// For functioned arrows (.any()/.all()), this returns the specific function type.
	Function() FunctionType
}

ArrowOperation is an interface implemented by all arrow-based operations (both standard and functioned arrows). This includes ArrowReference, FunctionedArrowReference, ResolvedArrowReference, and ResolvedFunctionedArrowReference.

type ArrowOperationVisitor added in v1.46.2

type ArrowOperationVisitor[T any] interface {
	VisitArrowOperation(ao ArrowOperation, value T) (T, error)
}

ArrowOperationVisitor is called when visiting any ArrowOperation (ArrowReference, FunctionedArrowReference, ResolvedArrowReference, or ResolvedFunctionedArrowReference). Returns the value to pass to subsequent visits, and error to halt immediately.

type ArrowReference

type ArrowReference struct {
	// contains filtered or unexported fields
}

ArrowReference is an Operation that represents `permission foo = Left->Right`.

func (*ArrowReference) Function added in v1.46.2

func (a *ArrowReference) Function() FunctionType

Function returns FunctionTypeAny for standard arrows.

func (*ArrowReference) Left

func (a *ArrowReference) Left() string

Left returns the relation on the resource.

func (*ArrowReference) Right

func (a *ArrowReference) Right() string

Right returns the relation/permission on the subject.

type ArrowReferenceVisitor added in v1.46.1

type ArrowReferenceVisitor[T any] interface {
	VisitArrowReference(ar *ArrowReference, value T) (T, error)
}

ArrowReferenceVisitor is called when visiting an ArrowReference. Returns the value to pass to subsequent visits, and error to halt immediately.

type BaseRelation

type BaseRelation struct {
	// contains filtered or unexported fields
}

BaseRelation is a single type, and its potential caveats, and expiration options. These features are written directly to the database with the parent Relation and Definition as the resource type and relation, and contains the subject type and optional subrelation.

func NewTestBaseRelation added in v1.46.1

func NewTestBaseRelation(defName, relationName, subjectType, subrelation string) *BaseRelation

NewTestBaseRelation creates a BaseRelation for testing with the proper parent structure. This is exported for use by external test packages that cannot directly create struct literals with unexported fields.

func NewTestBaseRelationWithFeatures added in v1.46.1

func NewTestBaseRelationWithFeatures(defName, relationName, subjectType, subrelation, caveat string, expiration bool) *BaseRelation

NewTestBaseRelationWithFeatures creates a BaseRelation with caveat and expiration features.

func NewTestWildcardBaseRelation added in v1.46.1

func NewTestWildcardBaseRelation(defName, relationName, subjectType string) *BaseRelation

NewTestWildcardBaseRelation creates a BaseRelation for wildcard testing.

func NewTestWildcardBaseRelationWithFeatures added in v1.46.1

func NewTestWildcardBaseRelationWithFeatures(defName, relationName, subjectType, caveat string, expiration bool) *BaseRelation

NewTestWildcardBaseRelationWithFeatures creates a wildcard BaseRelation with caveat and expiration features.

func (*BaseRelation) Caveat

func (b *BaseRelation) Caveat() string

Caveat returns the caveat of the base relation.

func (*BaseRelation) DefinitionName

func (b *BaseRelation) DefinitionName() string

DefinitionName returns the name of the Definition in which this BaseRelation appears.

func (*BaseRelation) Expiration

func (b *BaseRelation) Expiration() bool

Expiration returns whether the base relation has expiration.

func (*BaseRelation) Parent

func (b *BaseRelation) Parent() *Relation

Parent returns the parent relation.

func (*BaseRelation) RelationName

func (b *BaseRelation) RelationName() string

RelationName returns the name of the Relation in which this BaseRelation appears.

func (*BaseRelation) Subrelation

func (b *BaseRelation) Subrelation() string

Subrelation returns the subrelation of the base relation.

func (*BaseRelation) Type

func (b *BaseRelation) Type() string

Type returns the subject type of the base relation.

func (*BaseRelation) Wildcard

func (b *BaseRelation) Wildcard() bool

Wildcard returns whether the base relation is a wildcard.

type BaseRelationVisitor added in v1.46.1

type BaseRelationVisitor[T any] interface {
	VisitBaseRelation(br *BaseRelation, value T) (T, error)
}

BaseRelationVisitor is called when visiting a BaseRelation. Returns the value to pass to subsequent visits, and error to halt immediately.

type Caveat

type Caveat struct {
	// contains filtered or unexported fields
}

Caveat is a single, top-level caveat definition and it's internal expresion.

func (*Caveat) Expression

func (c *Caveat) Expression() string

Expression returns the expression of the caveat.

func (*Caveat) Name

func (c *Caveat) Name() string

Name returns the name of the caveat.

func (*Caveat) ParameterTypes

func (c *Caveat) ParameterTypes() []string

ParameterTypes returns the parameter types of the caveat.

func (*Caveat) Parent

func (c *Caveat) Parent() *Schema

Parent returns the parent schema.

type CaveatVisitor added in v1.46.1

type CaveatVisitor[T any] interface {
	VisitCaveat(c *Caveat, value T) (T, error)
}

CaveatVisitor is called when visiting a Caveat. Returns the value to pass to subsequent visits, and error to halt immediately.

type Definition

type Definition struct {
	// contains filtered or unexported fields
}

Definition is a single schema object type, with relations and permissions.

func (*Definition) Name

func (d *Definition) Name() string

Name returns the name of the definition.

func (*Definition) Parent

func (d *Definition) Parent() *Schema

Parent returns the parent schema.

func (*Definition) Permissions

func (d *Definition) Permissions() map[string]*Permission

Permissions returns the permissions in the definition.

func (*Definition) Relations

func (d *Definition) Relations() map[string]*Relation

Relations returns the relations in the definition.

type DefinitionVisitor added in v1.46.1

type DefinitionVisitor[T any] interface {
	VisitDefinition(d *Definition, value T) (T, bool, error)
}

DefinitionVisitor is called when visiting a Definition. Returns the value to pass to subsequent visits, true to continue walking, false to stop, and error to halt immediately.

type ExclusionOperation

type ExclusionOperation struct {
	// contains filtered or unexported fields
}

ExclusionOperation is an Operation that represents `permission foo = a - b`.

func (*ExclusionOperation) Left

func (e *ExclusionOperation) Left() Operation

Left returns the operation from which we are excluding.

func (*ExclusionOperation) Right

func (e *ExclusionOperation) Right() Operation

Right returns the operation that is being excluded.

type ExclusionOperationVisitor added in v1.46.1

type ExclusionOperationVisitor[T any] interface {
	VisitExclusionOperation(eo *ExclusionOperation, value T) (T, bool, error)
}

ExclusionOperationVisitor is called when visiting an ExclusionOperation. Returns the value to pass to subsequent visits, true to continue walking, false to stop, and error to halt immediately.

type FlattenOptions added in v1.46.2

type FlattenOptions struct {
	// Separator controls what separator is used between permission names and hashes
	// in synthetic permissions ($ or __).
	Separator FlattenSeparator

	// FlattenNonUnionOperations controls whether non-union operations (intersections,
	// exclusions) should be flattened. When true, nested compound operations are
	// extracted into synthetic permissions.
	FlattenNonUnionOperations bool

	// FlattenArrows controls whether arrow operations (->) should be flattened.
	// When true, arrow operations are treated as leaf nodes and are extracted into
	// their own synthetic permissions. When false, they remain as-is in the operation tree.
	FlattenArrows bool
}

FlattenOptions contains options for flattening a schema.

type FlattenSeparator added in v1.46.1

type FlattenSeparator string

FlattenSeparator is the separator used between permission names and hashes in synthetic permissions.

const (
	// FlattenSeparatorDollar uses $ as separator (e.g., view$abc123).
	// Note: $ is not valid in schema DSL identifiers, so this is only for internal use.
	FlattenSeparatorDollar FlattenSeparator = "$"

	// FlattenSeparatorDoubleUnderscore uses __ as separator (e.g., view__abc123).
	// This is valid in schema DSL identifiers.
	FlattenSeparatorDoubleUnderscore FlattenSeparator = "__"
)

type FlattenedSchema added in v1.46.1

type FlattenedSchema struct {
	// contains filtered or unexported fields
}

FlattenedSchema wraps a ResolvedSchema where all nested operations under permissions have been replaced with references to synthetic permissions.

func FlattenSchema added in v1.46.1

func FlattenSchema(rs *ResolvedSchema, separator FlattenSeparator) (*FlattenedSchema, error)

FlattenSchema takes a resolved schema and recursively flattens all nested operations under each permission's root expression by replacing them with references to new synthetic permissions. The synthetic permissions are named using the pattern: `{permissionName}{separator}{hash}` where hash is computed using rudd BDD canonicalization. The separator parameter controls what separator is used ($ or __).

This function uses default flattening options that flatten both non-union operations and arrows.

func FlattenSchemaWithOptions added in v1.46.2

func FlattenSchemaWithOptions(rs *ResolvedSchema, options FlattenOptions) (*FlattenedSchema, error)

FlattenSchemaWithOptions takes a resolved schema and recursively flattens operations according to the provided options. Nested operations under each permission's root expression are replaced with references to new synthetic permissions. The synthetic permissions are named using the pattern: `{permissionName}{separator}{hash}` where hash is computed using rudd BDD canonicalization.

func (*FlattenedSchema) ResolvedSchema added in v1.46.1

func (f *FlattenedSchema) ResolvedSchema() *ResolvedSchema

ResolvedSchema returns the underlying resolved schema with flattened operations.

type FunctionType added in v1.46.1

type FunctionType int

FunctionType represents the type of function applied to a tupleset.

const (
	FunctionTypeAny FunctionType = iota
	FunctionTypeAll
)

type FunctionedArrowReference added in v1.46.2

type FunctionedArrowReference struct {
	// contains filtered or unexported fields
}

FunctionedArrowReference is an Operation that represents functioned arrows like `permission foo = relation.any(other)` or `permission foo = relation.all(other)`.

func (*FunctionedArrowReference) Function added in v1.46.2

func (f *FunctionedArrowReference) Function() FunctionType

func (*FunctionedArrowReference) Left added in v1.46.2

func (f *FunctionedArrowReference) Left() string

func (*FunctionedArrowReference) Right added in v1.46.2

func (f *FunctionedArrowReference) Right() string

type IntersectionOperation

type IntersectionOperation struct {
	// contains filtered or unexported fields
}

IntersectionOperation is an Operation that represents `permission foo = a & b & c`.

func (*IntersectionOperation) Children

func (i *IntersectionOperation) Children() []Operation

Children returns the sub-operations that are intersected together.

type IntersectionOperationVisitor added in v1.46.1

type IntersectionOperationVisitor[T any] interface {
	VisitIntersectionOperation(io *IntersectionOperation, value T) (T, bool, error)
}

IntersectionOperationVisitor is called when visiting an IntersectionOperation. Returns the value to pass to subsequent visits, true to continue walking, false to stop, and error to halt immediately.

type Operation

type Operation interface {
	// contains filtered or unexported methods
}

Operation is a closed enum of things that can exist on the right-hand-side of a permission. It forms a tree of unions, intersections and exclusions, until the leaves are things like references to other permissions or relations, or are arrows.

type OperationVisitor added in v1.46.1

type OperationVisitor[T any] interface {
	VisitOperation(op Operation, value T) (T, bool, error)
}

OperationVisitor is called when visiting any Operation. Returns the value to pass to subsequent visits, true to continue walking, false to stop, and error to halt immediately.

type Permission

type Permission struct {
	// contains filtered or unexported fields
}

Permission is a single `permission` line belonging to a definition. It has a name and a // and/or/not tree of operations representing it's right-hand-side.

func (*Permission) IsSynthetic added in v1.46.1

func (p *Permission) IsSynthetic() bool

IsSynthetic returns true if this permission was synthesized by the schema system.

func (*Permission) Name

func (p *Permission) Name() string

Name returns the name of the permission.

func (*Permission) Operation

func (p *Permission) Operation() Operation

Operation returns the operation of the permission.

func (*Permission) Parent

func (p *Permission) Parent() *Definition

Parent returns the parent definition.

type PermissionVisitor added in v1.46.1

type PermissionVisitor[T any] interface {
	VisitPermission(p *Permission, value T) (T, bool, error)
}

PermissionVisitor is called when visiting a Permission. Returns the value to pass to subsequent visits, true to continue walking, false to stop, and error to halt immediately.

type Relation

type Relation struct {
	// contains filtered or unexported fields
}

Relation is a single `relation` line belonging to a definition. It has a name and list of types appearing on the right hand side.

func (*Relation) AliasingRelation

func (r *Relation) AliasingRelation() string

AliasingRelation returns the aliasing relation of the relation.

func (*Relation) BaseRelations

func (r *Relation) BaseRelations() []*BaseRelation

BaseRelations returns the base relations of the relation.

func (*Relation) Name

func (r *Relation) Name() string

Name returns the name of the relation.

func (*Relation) Parent

func (r *Relation) Parent() *Definition

Parent returns the parent definition.

type RelationOrPermission added in v1.46.1

type RelationOrPermission interface {
	// contains filtered or unexported methods
}

type RelationReference

type RelationReference struct {
	// contains filtered or unexported fields
}

RelationReference is an Operation that is a simple relation, such as `permission foo = bar`.

func (*RelationReference) RelationName

func (r *RelationReference) RelationName() string

RelationName returns the name of the relation or permission being referenced.

type RelationReferenceVisitor added in v1.46.1

type RelationReferenceVisitor[T any] interface {
	VisitRelationReference(rr *RelationReference, value T) (T, error)
}

RelationReferenceVisitor is called when visiting a RelationReference. Returns the value to pass to subsequent visits, and error to halt immediately.

type RelationVisitor added in v1.46.1

type RelationVisitor[T any] interface {
	VisitRelation(r *Relation, value T) (T, bool, error)
}

RelationVisitor is called when visiting a Relation. Returns the value to pass to subsequent visits, true to continue walking, false to stop, and error to halt immediately.

type ResolvedArrowReference added in v1.46.1

type ResolvedArrowReference struct {
	// contains filtered or unexported fields
}

ResolvedArrowReference is an Operation that represents a resolved arrow reference. It contains the resolved left side relation and the name of the right side.

func (*ResolvedArrowReference) Function added in v1.46.2

func (a *ResolvedArrowReference) Function() FunctionType

Function returns FunctionTypeAny for standard resolved arrows.

func (*ResolvedArrowReference) Left added in v1.46.1

func (a *ResolvedArrowReference) Left() string

Left returns the name of the relation on the resource.

func (*ResolvedArrowReference) ResolvedLeft added in v1.46.1

func (a *ResolvedArrowReference) ResolvedLeft() *Relation

ResolvedLeft returns the actual Relation being referenced on the left side.

func (*ResolvedArrowReference) Right added in v1.46.1

func (a *ResolvedArrowReference) Right() string

Right returns the name of the relation/permission on the subject.

type ResolvedArrowReferenceVisitor added in v1.46.1

type ResolvedArrowReferenceVisitor[T any] interface {
	VisitResolvedArrowReference(rar *ResolvedArrowReference, value T) (T, error)
}

ResolvedArrowReferenceVisitor is called when visiting a ResolvedArrowReference. Returns the value to pass to subsequent visits, and error to halt immediately.

type ResolvedFunctionedArrowReference added in v1.46.2

type ResolvedFunctionedArrowReference struct {
	// contains filtered or unexported fields
}

ResolvedFunctionedArrowReference is an Operation that represents a resolved functioned arrow reference. It contains the resolved left side relation, the name of the right side, and the function type.

func (*ResolvedFunctionedArrowReference) Function added in v1.46.2

Function returns the function type.

func (*ResolvedFunctionedArrowReference) Left added in v1.46.2

Left returns the name of the relation on the resource.

func (*ResolvedFunctionedArrowReference) ResolvedLeft added in v1.46.2

func (a *ResolvedFunctionedArrowReference) ResolvedLeft() *Relation

ResolvedLeft returns the actual Relation being referenced on the left side.

func (*ResolvedFunctionedArrowReference) Right added in v1.46.2

Right returns the name of the relation/permission on the subject.

type ResolvedFunctionedArrowReferenceVisitor added in v1.46.2

type ResolvedFunctionedArrowReferenceVisitor[T any] interface {
	VisitResolvedFunctionedArrowReference(rfar *ResolvedFunctionedArrowReference, value T) (T, error)
}

ResolvedFunctionedArrowReferenceVisitor is called when visiting a ResolvedFunctionedArrowReference. Returns the value to pass to subsequent visits, and error to halt immediately.

type ResolvedRelationReference added in v1.46.1

type ResolvedRelationReference struct {
	// contains filtered or unexported fields
}

ResolvedRelationReference is an Operation that is a resolved relation reference. It contains both the name and the actual RelationOrPermission being referenced.

func (*ResolvedRelationReference) RelationName added in v1.46.1

func (r *ResolvedRelationReference) RelationName() string

RelationName returns the name of the relation or permission being referenced.

func (*ResolvedRelationReference) Resolved added in v1.46.1

Resolved returns the actual RelationOrPermission being referenced.

type ResolvedRelationReferenceVisitor added in v1.46.1

type ResolvedRelationReferenceVisitor[T any] interface {
	VisitResolvedRelationReference(rrr *ResolvedRelationReference, value T) (T, error)
}

ResolvedRelationReferenceVisitor is called when visiting a ResolvedRelationReference. Returns the value to pass to subsequent visits, and error to halt immediately.

type ResolvedSchema added in v1.46.1

type ResolvedSchema struct {
	// contains filtered or unexported fields
}

ResolvedSchema wraps a Schema where all RelationReferences and ArrowReferences have been resolved to their actual RelationOrPermission targets.

func ResolveSchema added in v1.46.1

func ResolveSchema(s *Schema) (*ResolvedSchema, error)

ResolveSchema takes a schema, clones it, walks through all operations, and replaces RelationReference and ArrowReference nodes with their resolved versions. Returns an error if any relation or arrow left side cannot be resolved.

func (*ResolvedSchema) Schema added in v1.46.1

func (r *ResolvedSchema) Schema() *Schema

Schema returns the underlying resolved schema.

type Schema

type Schema struct {
	// contains filtered or unexported fields
}

Schema is a view of a complete schema, with all definitions and caveats.

func BuildSchemaFromCompiledSchema

func BuildSchemaFromCompiledSchema(schema compiler.CompiledSchema) (*Schema, error)

BuildSchemaFromCompiledSchema generates a Schema view from a CompiledSchema.

func BuildSchemaFromDefinitions

func BuildSchemaFromDefinitions(objectDefs []*corev1.NamespaceDefinition, caveatDefs []*corev1.CaveatDefinition) (*Schema, error)

BuildSchemaFromDefinitions generates a Schema view from the base core.v1 protos.

func CloneSchema added in v1.46.1

func CloneSchema(s *Schema) *Schema

CloneSchema creates a deep copy of a Schema and all its nested structures. All parent references are properly maintained in the cloned structure.

func (*Schema) Caveats

func (s *Schema) Caveats() map[string]*Caveat

Caveats returns the caveats in the schema.

func (*Schema) Definitions

func (s *Schema) Definitions() map[string]*Definition

Definitions returns the definitions in the schema.

func (*Schema) ToNamespaceDefinition added in v1.46.1

func (s *Schema) ToNamespaceDefinition(name string) ([]*core.NamespaceDefinition, []*core.CaveatDefinition, error)

ToNamespaceDefinition converts a Schema to a core.NamespaceDefinition. This is useful for converting schemas back to the protobuf format for serialization.

type SchemaVisitor added in v1.46.1

type SchemaVisitor[T any] interface {
	VisitSchema(s *Schema, value T) (T, bool, error)
}

SchemaVisitor is called when visiting a Schema. Returns the value to pass to subsequent visits, true to continue walking, false to stop, and error to halt immediately.

type SyntheticPermission added in v1.46.1

type SyntheticPermission struct {
	Permission
	// contains filtered or unexported fields
}

SyntheticPermission is a permission that has been synthesized by the schema system (e.g., during flattening operations). It is functionally identical to a Permission but is marked as synthetic for tracking purposes.

func (*SyntheticPermission) IsSynthetic added in v1.46.1

func (sp *SyntheticPermission) IsSynthetic() bool

IsSynthetic returns true for synthetic permissions.

type UnionOperation

type UnionOperation struct {
	// contains filtered or unexported fields
}

UnionOperation is an Operation that represents `permission foo = a | b | c`.

func (*UnionOperation) Children

func (u *UnionOperation) Children() []Operation

Children returns the sub-operations that are unioned together.

type UnionOperationVisitor added in v1.46.1

type UnionOperationVisitor[T any] interface {
	VisitUnionOperation(uo *UnionOperation, value T) (T, bool, error)
}

UnionOperationVisitor is called when visiting a UnionOperation. Returns the value to pass to subsequent visits, true to continue walking, false to stop, and error to halt immediately.

type Visitor added in v1.46.1

type Visitor[T any] any

Visitor is the base interface that all specific visitor interfaces embed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL