generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
enum.go
152 lines (132 loc) 路 3.62 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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 Enum struct {
Pos Position `parser:"" protobuf:"1,optional"`
Comments []string `parser:"@Comment*" protobuf:"2"`
Export bool `parser:"@'export'?" protobuf:"3"`
Name string `parser:"'enum' @Ident" protobuf:"4"`
Type Type `parser:"(':' @@)?" protobuf:"5,optional"`
Variants []*EnumVariant `parser:"'{' @@* '}'" protobuf:"6"`
}
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))
if e.Export {
fmt.Fprint(w, "export ")
}
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,
Export: e.Export,
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 (e *Enum) IsExported() bool { return e.Export }
func EnumFromProto(s *schemapb.Enum) *Enum {
e := &Enum{
Pos: posFromProto(s.Pos),
Name: s.Name,
Export: s.Export,
Comments: s.Comments,
Variants: enumVariantListToSchema(s.Variants),
}
if s.Type != nil {
e.Type = typeToSchema(s.Type)
}
return e
}
// IsValueEnum determines whether this is a type or value enum using `e.Type` alone
// because value enums must always have a unified type across all variants, whereas type
// enums by definition cannot have a unified type.
func (e *Enum) IsValueEnum() bool {
return e.Type != nil
}
func (e *Enum) VariantForName(name string) optional.Option[*EnumVariant] {
for _, v := range e.Variants {
if name == v.Name {
return optional.Some(v)
}
}
return optional.None[*EnumVariant]()
}
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),
}
}