generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
data.go
215 lines (191 loc) · 6.05 KB
/
data.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package schema
import (
"fmt"
"strings"
"google.golang.org/protobuf/proto"
schemapb "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/schema"
"github.com/TBD54566975/ftl/internal/reflect"
)
// A Data structure.
type Data struct {
Pos Position `parser:"" protobuf:"1,optional"`
Comments []string `parser:"@Comment*" protobuf:"2"`
Export bool `parser:"@'export'?" protobuf:"3"`
Name string `parser:"'data' @Ident" protobuf:"4"`
TypeParameters []*TypeParameter `parser:"( '<' @@ (',' @@)* '>' )?" protobuf:"5"`
Fields []*Field `parser:"'{' @@* '}'" protobuf:"6"`
Metadata []Metadata `parser:"@@*" protobuf:"7"`
}
var _ Decl = (*Data)(nil)
var _ Symbol = (*Data)(nil)
var _ Scoped = (*Data)(nil)
func (d *Data) Scope() Scope {
scope := Scope{}
for _, t := range d.TypeParameters {
scope[t.Name] = ModuleDecl{Symbol: t}
}
return scope
}
func (d *Data) FieldByName(name string) *Field {
for _, f := range d.Fields {
if f.Name == name {
return f
}
}
return nil
}
// Monomorphise this data type with the given type arguments.
//
// If this data type has no type parameters, it will be returned as-is.
//
// This will return a new Data structure with all type parameters replaced with
// the given types.
func (d *Data) Monomorphise(ref *Ref) (*Data, error) {
if len(d.TypeParameters) != len(ref.TypeParameters) {
return nil, fmt.Errorf("%s: expected %d type arguments, got %d", ref.Pos, len(d.TypeParameters), len(ref.TypeParameters))
}
if len(d.TypeParameters) == 0 {
return d, nil
}
names := map[string]Type{}
for i, t := range d.TypeParameters {
names[t.Name] = ref.TypeParameters[i]
}
monomorphised := reflect.DeepCopy(d)
monomorphised.TypeParameters = nil
// Because we don't have parent links in the AST allowing us to visit on
// Type and replace it on the parent, we have to do a full traversal to find
// the parents of all the Type nodes we need to replace. This will be a bit
// tricky to maintain, but it's basically any type that has parametric
// types: maps, slices, fields, etc.
err := Visit(monomorphised, func(n Node, next func() error) error {
switch n := n.(type) {
case *Map:
k, err := maybeMonomorphiseType(n.Key, names)
if err != nil {
return fmt.Errorf("%s: map key: %w", n.Key.Position(), err)
}
v, err := maybeMonomorphiseType(n.Value, names)
if err != nil {
return fmt.Errorf("%s: map value: %w", n.Value.Position(), err)
}
n.Key = k
n.Value = v
case *Array:
t, err := maybeMonomorphiseType(n.Element, names)
if err != nil {
return fmt.Errorf("%s: array element: %w", n.Element.Position(), err)
}
n.Element = t
case *Field:
t, err := maybeMonomorphiseType(n.Type, names)
if err != nil {
return fmt.Errorf("%s: field type: %w", n.Type.Position(), err)
}
n.Type = t
case *Optional:
t, err := maybeMonomorphiseType(n.Type, names)
if err != nil {
return fmt.Errorf("%s: optional type: %w", n.Type.Position(), err)
}
n.Type = t
case *Config:
t, err := maybeMonomorphiseType(n.Type, names)
if err != nil {
return fmt.Errorf("%s: config type: %w", n.Type.Position(), err)
}
n.Type = t
case *Secret:
t, err := maybeMonomorphiseType(n.Type, names)
if err != nil {
return fmt.Errorf("%s: secret type: %w", n.Type.Position(), err)
}
n.Type = t
case *Any, *Bool, *Bytes, *Data, *Ref, *Database, Decl, *Float,
IngressPathComponent, *IngressPathLiteral, *IngressPathParameter,
*Int, Metadata, *MetadataCalls, *MetadataDatabases, *MetadataRetry,
*MetadataIngress, *MetadataCronJob, *MetadataAlias, *Module,
*Schema, *String, *Time, Type, *TypeParameter, *Unit, *Verb, *Enum,
*EnumVariant, Value, *IntValue, *StringValue, *TypeValue, Symbol,
Named, *FSM, *FSMTransition, *TypeAlias:
}
return next()
})
if err != nil {
return nil, fmt.Errorf("%s: failed to monomorphise: %w", d.Pos, err)
}
return monomorphised, nil
}
func (d *Data) Position() Position { return d.Pos }
func (*Data) schemaDecl() {}
func (*Data) schemaSymbol() {}
func (d *Data) schemaChildren() []Node {
children := make([]Node, 0, len(d.Fields)+len(d.Metadata))
for _, t := range d.TypeParameters {
children = append(children, t)
}
for _, f := range d.Fields {
children = append(children, f)
}
for _, c := range d.Metadata {
children = append(children, c)
}
return children
}
func (d *Data) GetName() string { return d.Name }
func (d *Data) IsExported() bool { return d.Export }
func (d *Data) String() string {
w := &strings.Builder{}
fmt.Fprint(w, EncodeComments(d.Comments))
typeParameters := ""
if len(d.TypeParameters) > 0 {
typeParameters = "<"
for i, t := range d.TypeParameters {
if i != 0 {
typeParameters += ", "
}
typeParameters += t.String()
}
typeParameters += ">"
}
if d.Export {
fmt.Fprint(w, "export ")
}
fmt.Fprintf(w, "data %s%s {\n", d.Name, typeParameters)
for _, f := range d.Fields {
fmt.Fprintln(w, indent(f.String()))
}
fmt.Fprintf(w, "}")
fmt.Fprint(w, indent(encodeMetadata(d.Metadata)))
return w.String()
}
func (d *Data) ToProto() proto.Message {
return &schemapb.Data{
Pos: posToProto(d.Pos),
Name: d.Name,
Export: d.Export,
TypeParameters: nodeListToProto[*schemapb.TypeParameter](d.TypeParameters),
Fields: nodeListToProto[*schemapb.Field](d.Fields),
Comments: d.Comments,
}
}
func DataFromProto(s *schemapb.Data) *Data {
return &Data{
Pos: posFromProto(s.Pos),
Name: s.Name,
Export: s.Export,
TypeParameters: typeParametersToSchema(s.TypeParameters),
Fields: fieldListToSchema(s.Fields),
Comments: s.Comments,
}
}
// MonoType returns the monomorphised type of this data type if applicable, or returns the original type.
func maybeMonomorphiseType(t Type, typeParameters map[string]Type) (Type, error) {
if t, ok := t.(*Ref); ok && t.Module == "" {
if tp, ok := typeParameters[t.Name]; ok {
return tp, nil
}
return nil, fmt.Errorf("%s: unknown type parameter %q", t.Position(), t)
}
return t, nil
}