/
schema.go
62 lines (50 loc) · 1.6 KB
/
schema.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package blocks
import (
"errors"
"fmt"
yamlv3 "gopkg.in/yaml.v3"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
"github.com/authzed/spicedb/pkg/commonerrors"
"github.com/authzed/spicedb/pkg/schemadsl/compiler"
"github.com/authzed/spicedb/pkg/schemadsl/input"
)
// 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 commonerrors.SourcePosition
// Definitions are the compiled definitions for the schema.
Definitions []*core.NamespaceDefinition
}
// UnmarshalYAML is a custom unmarshaller.
func (ps *ParsedSchema) UnmarshalYAML(node *yamlv3.Node) error {
err := node.Decode(&ps.Schema)
if err != nil {
return convertYamlError(err)
}
empty := ""
defs, err := compiler.Compile([]compiler.InputSchema{{
Source: input.Source("schema"),
SchemaString: ps.Schema,
}}, &empty)
if err != nil {
var errWithContext compiler.ErrorWithContext
if errors.As(err, &errWithContext) {
line, col, lerr := errWithContext.SourceRange.Start().LineAndColumn()
if lerr != nil {
return lerr
}
return commonerrors.NewErrorWithSource(
fmt.Errorf("error when parsing schema: %s", errWithContext.BaseMessage),
errWithContext.ErrorSourceCode,
uint64(line+1), // source line is 0-indexed
uint64(col+1), // source col is 0-indexed
)
}
return fmt.Errorf("error when parsing schema: %w", err)
}
ps.Definitions = defs
ps.SourcePosition = commonerrors.SourcePosition{LineNumber: node.Line, ColumnPosition: node.Column}
return nil
}