generated from TBD54566975/tbd-project-template
/
enum.go
128 lines (110 loc) 路 2.91 KB
/
enum.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
package schema
import (
"fmt"
"strings"
"google.golang.org/protobuf/proto"
schemapb "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/schema"
)
type Enum struct {
Pos Position `parser:"" protobuf:"1,optional"`
Comments []string `parser:"@Comment*" protobuf:"2"`
Name string `parser:"'enum' @Ident" protobuf:"3"`
Type Type `parser:"(':' @@)?" protobuf:"4,optional"`
Variants []*EnumVariant `parser:"'{' @@* '}'" protobuf:"5"`
}
var _ Decl = (*Enum)(nil)
var _ Symbol = (*Enum)(nil)
func (e *Enum) Position() Position { return e.Pos }
func (e *Enum) String() string {
w := &strings.Builder{}
fmt.Fprint(w, encodeComments(e.Comments))
fmt.Fprintf(w, "enum %s", e.Name)
if e.Type != nil {
fmt.Fprintf(w, ": %s", e.Type)
}
fmt.Fprint(w, " {\n")
for _, v := range e.Variants {
fmt.Fprintln(w, indent(v.String()))
}
fmt.Fprint(w, "}")
return w.String()
}
func (*Enum) schemaDecl() {}
func (*Enum) schemaSymbol() {}
func (e *Enum) schemaChildren() []Node {
var children []Node
for _, v := range e.Variants {
children = append(children, v)
}
if e.Type != nil {
children = append(children, e.Type)
}
return children
}
func (e *Enum) ToProto() proto.Message {
se := &schemapb.Enum{
Pos: posToProto(e.Pos),
Comments: e.Comments,
Name: e.Name,
Variants: nodeListToProto[*schemapb.EnumVariant](e.Variants),
}
if e.Type != nil {
se.Type = typeToProto(e.Type)
}
return se
}
func (e *Enum) GetName() string { return e.Name }
func EnumFromProto(s *schemapb.Enum) *Enum {
e := &Enum{
Pos: posFromProto(s.Pos),
Name: s.Name,
Comments: s.Comments,
Variants: enumVariantListToSchema(s.Variants),
}
if s.Type != nil {
e.Type = typeToSchema(s.Type)
}
return e
}
type EnumVariant struct {
Pos Position `parser:"" protobuf:"1,optional"`
Comments []string `parser:"@Comment*" protobuf:"2"`
Name string `parser:"@Ident" protobuf:"3"`
Value Value `parser:"(('=' @@) | @@)!" protobuf:"4"`
}
func (e *EnumVariant) ToProto() proto.Message {
return &schemapb.EnumVariant{
Pos: posToProto(e.Pos),
Name: e.Name,
Value: valueToProto(e.Value),
}
}
func (e *EnumVariant) Position() Position { return e.Pos }
func (e *EnumVariant) schemaChildren() []Node { return []Node{e.Value} }
func (e *EnumVariant) String() string {
w := &strings.Builder{}
fmt.Fprint(w, encodeComments(e.Comments))
fmt.Fprintf(w, e.Name)
if e.Value != nil {
if _, ok := e.Value.(*TypeValue); ok {
fmt.Fprint(w, " ", e.Value)
} else {
fmt.Fprint(w, " = ", e.Value)
}
}
return w.String()
}
func enumVariantListToSchema(e []*schemapb.EnumVariant) []*EnumVariant {
out := make([]*EnumVariant, 0, len(e))
for _, v := range e {
out = append(out, enumVariantToSchema(v))
}
return out
}
func enumVariantToSchema(v *schemapb.EnumVariant) *EnumVariant {
return &EnumVariant{
Pos: posFromProto(v.Pos),
Name: v.Name,
Value: valueToSchema(v.Value),
}
}