-
Notifications
You must be signed in to change notification settings - Fork 246
/
compiler.go
159 lines (125 loc) · 4.3 KB
/
compiler.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package compiler
import (
"errors"
"fmt"
"google.golang.org/protobuf/proto"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
"github.com/authzed/spicedb/pkg/schemadsl/dslshape"
"github.com/authzed/spicedb/pkg/schemadsl/input"
"github.com/authzed/spicedb/pkg/schemadsl/parser"
)
// InputSchema defines the input for a Compile.
type InputSchema struct {
// Source is the source of the schema being compiled.
Source input.Source
// Schema is the contents being compiled.
SchemaString string
}
// SchemaDefinition represents an object or caveat definition in a schema.
type SchemaDefinition interface {
proto.Message
GetName() string
}
// CompiledSchema is the result of compiling a schema when there are no errors.
type CompiledSchema struct {
// ObjectDefinitions holds the object definitions in the schema.
ObjectDefinitions []*core.NamespaceDefinition
// CaveatDefinitions holds the caveat definitions in the schema.
CaveatDefinitions []*core.CaveatDefinition
// OrderedDefinitions holds the object and caveat definitions in the schema, in the
// order in which they were found.
OrderedDefinitions []SchemaDefinition
}
type config struct {
skipValidation bool
objectTypePrefix *string
}
func SkipValidation() Option { return func(cfg *config) { cfg.skipValidation = true } }
func ObjectTypePrefix(prefix string) ObjectPrefixOption {
return func(cfg *config) { cfg.objectTypePrefix = &prefix }
}
func RequirePrefixedObjectType() ObjectPrefixOption {
return func(cfg *config) { cfg.objectTypePrefix = nil }
}
func AllowUnprefixedObjectType() ObjectPrefixOption {
return func(cfg *config) { cfg.objectTypePrefix = new(string) }
}
type Option func(*config)
type ObjectPrefixOption func(*config)
// Compile compilers the input schema into a set of namespace definition protos.
func Compile(schema InputSchema, prefix ObjectPrefixOption, opts ...Option) (*CompiledSchema, error) {
cfg := &config{}
prefix(cfg) // required option
for _, fn := range opts {
fn(cfg)
}
mapper := newPositionMapper(schema)
root := parser.Parse(createAstNode, schema.Source, schema.SchemaString).(*dslNode)
errs := root.FindAll(dslshape.NodeTypeError)
if len(errs) > 0 {
err := errorNodeToError(errs[0], mapper)
return nil, err
}
compiled, err := translate(translationContext{
objectTypePrefix: cfg.objectTypePrefix,
mapper: mapper,
schemaString: schema.SchemaString,
skipValidate: cfg.skipValidation,
}, root)
if err != nil {
var errorWithNode errorWithNode
if errors.As(err, &errorWithNode) {
err = toContextError(errorWithNode.error.Error(), errorWithNode.errorSourceCode, errorWithNode.node, mapper)
}
return nil, err
}
return compiled, nil
}
func errorNodeToError(node *dslNode, mapper input.PositionMapper) error {
if node.GetType() != dslshape.NodeTypeError {
return fmt.Errorf("given none error node")
}
errMessage, err := node.GetString(dslshape.NodePredicateErrorMessage)
if err != nil {
return fmt.Errorf("could not get error message for error node: %w", err)
}
errorSourceCode := ""
if node.Has(dslshape.NodePredicateErrorSource) {
es, err := node.GetString(dslshape.NodePredicateErrorSource)
if err != nil {
return fmt.Errorf("could not get error source for error node: %w", err)
}
errorSourceCode = es
}
return toContextError(errMessage, errorSourceCode, node, mapper)
}
func toContextError(errMessage string, errorSourceCode string, node *dslNode, mapper input.PositionMapper) error {
sourceRange, err := node.Range(mapper)
if err != nil {
return fmt.Errorf("could not get range for error node: %w", err)
}
formattedRange, err := formatRange(sourceRange)
if err != nil {
return err
}
source, err := node.GetString(dslshape.NodePredicateSource)
if err != nil {
return fmt.Errorf("missing source for node: %w", err)
}
return ErrorWithContext{
BaseCompilerError: BaseCompilerError{
error: fmt.Errorf("parse error in %s: %s", formattedRange, errMessage),
BaseMessage: errMessage,
},
SourceRange: sourceRange,
Source: input.Source(source),
ErrorSourceCode: errorSourceCode,
}
}
func formatRange(rnge input.SourceRange) (string, error) {
startLine, startCol, err := rnge.Start().LineAndColumn()
if err != nil {
return "", err
}
return fmt.Sprintf("`%s`, line %v, column %v", rnge.Source(), startLine+1, startCol+1), nil
}