generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
field.go
79 lines (68 loc) 路 1.8 KB
/
field.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
package schema
import (
"fmt"
"strings"
"github.com/alecthomas/types/optional"
"google.golang.org/protobuf/proto"
schemapb "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/schema"
)
type Field struct {
Pos Position `parser:"" protobuf:"1,optional"`
Comments []string `parser:"@Comment*" protobuf:"3"`
Name string `parser:"@Ident" protobuf:"2"`
Type Type `parser:"@@" protobuf:"4"`
Metadata []Metadata `parser:"@@*" protobuf:"5"`
}
var _ Node = (*Field)(nil)
func (f *Field) Position() Position { return f.Pos }
func (f *Field) schemaChildren() []Node {
out := []Node{}
out = append(out, f.Type)
for _, md := range f.Metadata {
out = append(out, md)
}
return out
}
func (f *Field) String() string {
w := &strings.Builder{}
fmt.Fprint(w, EncodeComments(f.Comments))
fmt.Fprintf(w, "%s %s", f.Name, f.Type.String())
for _, md := range f.Metadata {
fmt.Fprintf(w, " %s", md.String())
}
return w.String()
}
func (f *Field) ToProto() proto.Message {
return &schemapb.Field{
Pos: posToProto(f.Pos),
Name: f.Name,
Type: typeToProto(f.Type),
Comments: f.Comments,
Metadata: metadataListToProto(f.Metadata),
}
}
// Alias returns the alias for the given kind.
func (f *Field) Alias(kind AliasKind) optional.Option[string] {
for _, md := range f.Metadata {
if a, ok := md.(*MetadataAlias); ok && a.Kind == kind {
return optional.Some(a.Alias)
}
}
return optional.None[string]()
}
func fieldListToSchema(s []*schemapb.Field) []*Field {
var out []*Field
for _, n := range s {
out = append(out, fieldToSchema(n))
}
return out
}
func fieldToSchema(s *schemapb.Field) *Field {
return &Field{
Pos: posFromProto(s.Pos),
Name: s.Name,
Comments: s.Comments,
Type: typeToSchema(s.Type),
Metadata: metadataListToSchema(s.Metadata),
}
}