diff options
Diffstat (limited to 'vendor/github.com/authzed/spicedb/pkg/validationfile/blocks/schema.go')
| -rw-r--r-- | vendor/github.com/authzed/spicedb/pkg/validationfile/blocks/schema.go | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/vendor/github.com/authzed/spicedb/pkg/validationfile/blocks/schema.go b/vendor/github.com/authzed/spicedb/pkg/validationfile/blocks/schema.go new file mode 100644 index 0000000..814eebc --- /dev/null +++ b/vendor/github.com/authzed/spicedb/pkg/validationfile/blocks/schema.go @@ -0,0 +1,70 @@ +package blocks + +import ( + "errors" + "fmt" + + yamlv3 "gopkg.in/yaml.v3" + + "github.com/ccoveille/go-safecast" + + "github.com/authzed/spicedb/pkg/schemadsl/compiler" + "github.com/authzed/spicedb/pkg/schemadsl/input" + "github.com/authzed/spicedb/pkg/spiceerrors" +) + +// ParsedSchema is the parsed schema in a validationfile. +type ParsedSchema struct { + // Schema is the schema found. + Schema string + + // SourcePosition is the position of the schema in the file. + SourcePosition spiceerrors.SourcePosition + + // CompiledSchema is the compiled schema. + CompiledSchema *compiler.CompiledSchema +} + +// UnmarshalYAML is a custom unmarshaller. +func (ps *ParsedSchema) UnmarshalYAML(node *yamlv3.Node) error { + err := node.Decode(&ps.Schema) + if err != nil { + return convertYamlError(err) + } + + compiled, err := compiler.Compile(compiler.InputSchema{ + Source: input.Source("schema"), + SchemaString: ps.Schema, + }, compiler.AllowUnprefixedObjectType()) + if err != nil { + var errWithContext compiler.WithContextError + if errors.As(err, &errWithContext) { + line, col, lerr := errWithContext.SourceRange.Start().LineAndColumn() + if lerr != nil { + return lerr + } + + uintLine, err := safecast.ToUint64(line) + if err != nil { + return err + } + uintCol, err := safecast.ToUint64(col) + if err != nil { + return err + } + + return spiceerrors.NewWithSourceError( + fmt.Errorf("error when parsing schema: %s", errWithContext.BaseMessage), + errWithContext.ErrorSourceCode, + uintLine+1, // source line is 0-indexed + uintCol+1, // source col is 0-indexed + ) + } + + return fmt.Errorf("error when parsing schema: %w", err) + } + + ps.CompiledSchema = compiled + ps.SourcePosition = spiceerrors.SourcePosition{LineNumber: node.Line, ColumnPosition: node.Column} + return nil +} |
