github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/pkg/development/schema.go (about) 1 package development 2 3 import ( 4 "errors" 5 6 devinterface "github.com/authzed/spicedb/pkg/proto/developer/v1" 7 "github.com/authzed/spicedb/pkg/schemadsl/compiler" 8 "github.com/authzed/spicedb/pkg/schemadsl/input" 9 ) 10 11 // CompileSchema compiles a schema into its caveat and namespace definition(s), returning a developer 12 // error if the schema could not be compiled. The non-developer error is returned only if an 13 // internal errors occurred. 14 func CompileSchema(schema string) (*compiler.CompiledSchema, *devinterface.DeveloperError, error) { 15 compiled, err := compiler.Compile(compiler.InputSchema{ 16 Source: input.Source("schema"), 17 SchemaString: schema, 18 }, compiler.AllowUnprefixedObjectType()) 19 20 var contextError compiler.ErrorWithContext 21 if errors.As(err, &contextError) { 22 line, col, lerr := contextError.SourceRange.Start().LineAndColumn() 23 if lerr != nil { 24 return nil, nil, lerr 25 } 26 27 return nil, &devinterface.DeveloperError{ 28 Message: contextError.BaseCompilerError.BaseMessage, 29 Kind: devinterface.DeveloperError_SCHEMA_ISSUE, 30 Source: devinterface.DeveloperError_SCHEMA, 31 Line: uint32(line) + 1, // 0-indexed in parser. 32 Column: uint32(col) + 1, // 0-indexed in parser. 33 Context: contextError.ErrorSourceCode, 34 }, nil 35 } 36 37 if err != nil { 38 return nil, nil, err 39 } 40 41 return compiled, nil, nil 42 }