generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
schema.go
163 lines (140 loc) · 3.98 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
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
160
161
162
163
package schema
import (
"crypto/sha256"
"errors"
"fmt"
"reflect"
"strings"
"github.com/alecthomas/types/optional"
"google.golang.org/protobuf/proto"
schemapb "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/schema"
)
var ErrNotFound = errors.New("not found")
type Schema struct {
Pos Position `parser:"" protobuf:"1,optional"`
Modules []*Module `parser:"@@*" protobuf:"2"`
}
var _ Node = (*Schema)(nil)
func (s *Schema) Position() Position { return s.Pos }
func (s *Schema) String() string {
out := &strings.Builder{}
for i, m := range s.Modules {
if i != 0 {
fmt.Fprintln(out)
}
fmt.Fprint(out, m)
}
return out.String()
}
func (s *Schema) schemaChildren() []Node {
out := make([]Node, len(s.Modules))
for i, m := range s.Modules {
out[i] = m
}
return out
}
func (s *Schema) Hash() [sha256.Size]byte {
return sha256.Sum256([]byte(s.String()))
}
// ResolveMonomorphised resolves a reference to a monomorphised Data type.
// If a Ref is not found, returns ErrNotFound.
func (s *Schema) ResolveMonomorphised(ref *Ref) (*Data, error) {
out := &Data{}
err := s.ResolveToType(ref, out)
if err != nil {
// If a ref is not found, returns ErrNotFound
return nil, err
}
return out.Monomorphise(ref)
}
// Resolve a reference to a declaration.
func (s *Schema) Resolve(ref *Ref) optional.Option[Decl] {
for _, module := range s.Modules {
if module.Name == ref.Module {
for _, decl := range module.Decls {
if decl.GetName() == ref.Name {
return optional.Some(decl)
}
}
}
}
return optional.None[Decl]()
}
// ResolveToType resolves a reference to a declaration of the given type.
//
// The out parameter must be a pointer to a non-nil Decl implementation or this
// will panic.
//
// data := &Data{}
// err := s.ResolveToType(ref, data)
func (s *Schema) ResolveToType(ref *Ref, out Decl) error {
// Programmer error.
if reflect.ValueOf(out).Kind() != reflect.Ptr {
panic(fmt.Errorf("out parameter is not a pointer"))
}
if reflect.ValueOf(out).Elem().Kind() == reflect.Invalid {
panic(fmt.Errorf("out parameter is a nil pointer"))
}
for _, module := range s.Modules {
if module.Name == ref.Module {
for _, decl := range module.Decls {
if decl.GetName() == ref.Name {
declType := reflect.TypeOf(decl)
outType := reflect.TypeOf(out)
if declType.Elem().AssignableTo(outType.Elem()) {
reflect.ValueOf(out).Elem().Set(reflect.ValueOf(decl).Elem())
return nil
}
return fmt.Errorf("resolved declaration is not of the expected type: want %s, got %s",
outType, declType)
}
}
}
}
return fmt.Errorf("could not resolve reference %v: %w", ref, ErrNotFound)
}
// Module returns the named module if it exists.
func (s *Schema) Module(name string) optional.Option[*Module] {
for _, module := range s.Modules {
if module.Name == name {
return optional.Some(module)
}
}
return optional.None[*Module]()
}
// Upsert inserts or replaces a module.
func (s *Schema) Upsert(module *Module) {
for i, m := range s.Modules {
if m.Name == module.Name {
s.Modules[i] = module
return
}
}
s.Modules = append(s.Modules, module)
}
func (s *Schema) ToProto() proto.Message {
return &schemapb.Schema{
Pos: posToProto(s.Pos),
Modules: nodeListToProto[*schemapb.Module](s.Modules),
}
}
// TypeName returns the name of a type as a string, stripping any package prefix and correctly handling Ref aliases.
func TypeName(v any) string {
t := reflect.Indirect(reflect.ValueOf(v)).Type()
// handle AbstractRefs like "AbstractRef[github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/schema.DataRef]"
if strings.HasPrefix(t.Name(), "AbstractRef[") {
return strings.TrimSuffix(strings.Split(t.Name(), ".")[2], "]")
}
return t.Name()
}
// FromProto converts a protobuf Schema to a Schema and validates it.
func FromProto(s *schemapb.Schema) (*Schema, error) {
modules, err := moduleListToSchema(s.Modules)
if err != nil {
return nil, err
}
schema := &Schema{
Modules: modules,
}
return ValidateSchema(schema)
}