generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
protobuf_enc.go
181 lines (139 loc) · 5.45 KB
/
protobuf_enc.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
//nolint:forcetypeassert
package schema
import (
"fmt"
"google.golang.org/protobuf/proto"
schemapb "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/schema"
)
func levelToProto(level ErrorLevel) schemapb.Error_ErrorLevel {
switch level {
case INFO:
return schemapb.Error_INFO
case WARN:
return schemapb.Error_WARN
case ERROR:
return schemapb.Error_ERROR
}
panic(fmt.Sprintf("unhandled ErrorLevel %v", level))
}
func posToProto(pos Position) *schemapb.Position {
return &schemapb.Position{Line: int64(pos.Line), Column: int64(pos.Column), Filename: pos.Filename}
}
func nodeListToProto[T proto.Message, U Node](nodes []U) []T {
out := make([]T, len(nodes))
for i, n := range nodes {
out[i] = n.ToProto().(T)
}
return out
}
func declListToProto(nodes []Decl) []*schemapb.Decl {
out := make([]*schemapb.Decl, len(nodes))
for i, n := range nodes {
var v schemapb.IsDeclValue
switch n := n.(type) {
case *Verb:
v = &schemapb.Decl_Verb{Verb: n.ToProto().(*schemapb.Verb)}
case *Data:
v = &schemapb.Decl_Data{Data: n.ToProto().(*schemapb.Data)}
case *Database:
v = &schemapb.Decl_Database{Database: n.ToProto().(*schemapb.Database)}
case *Enum:
v = &schemapb.Decl_Enum{Enum: n.ToProto().(*schemapb.Enum)}
case *Config:
v = &schemapb.Decl_Config{Config: n.ToProto().(*schemapb.Config)}
case *Secret:
v = &schemapb.Decl_Secret{Secret: n.ToProto().(*schemapb.Secret)}
case *FSM:
v = &schemapb.Decl_Fsm{Fsm: n.ToProto().(*schemapb.FSM)}
case *TypeAlias:
v = &schemapb.Decl_TypeAlias{TypeAlias: n.ToProto().(*schemapb.TypeAlias)}
case *Topic:
v = &schemapb.Decl_Topic{Topic: n.ToProto().(*schemapb.Topic)}
case *Subscription:
v = &schemapb.Decl_Subscription{Subscription: n.ToProto().(*schemapb.Subscription)}
}
out[i] = &schemapb.Decl{Value: v}
}
return out
}
func metadataListToProto(nodes []Metadata) []*schemapb.Metadata {
out := make([]*schemapb.Metadata, len(nodes))
for i, n := range nodes {
var v schemapb.IsMetadataValue
switch n := n.(type) {
case *MetadataCalls:
v = &schemapb.Metadata_Calls{Calls: n.ToProto().(*schemapb.MetadataCalls)}
case *MetadataDatabases:
v = &schemapb.Metadata_Databases{Databases: n.ToProto().(*schemapb.MetadataDatabases)}
case *MetadataIngress:
v = &schemapb.Metadata_Ingress{Ingress: n.ToProto().(*schemapb.MetadataIngress)}
case *MetadataCronJob:
v = &schemapb.Metadata_CronJob{CronJob: n.ToProto().(*schemapb.MetadataCronJob)}
case *MetadataAlias:
v = &schemapb.Metadata_Alias{Alias: n.ToProto().(*schemapb.MetadataAlias)}
case *MetadataRetry:
v = &schemapb.Metadata_Retry{Retry: n.ToProto().(*schemapb.MetadataRetry)}
case *MetadataSubscriber:
v = &schemapb.Metadata_Subscriber{Subscriber: n.ToProto().(*schemapb.MetadataSubscriber)}
default:
panic(fmt.Sprintf("unhandled metadata type %T", n))
}
out[i] = &schemapb.Metadata{Value: v}
}
return out
}
func ingressListToProto(nodes []IngressPathComponent) []*schemapb.IngressPathComponent {
out := make([]*schemapb.IngressPathComponent, len(nodes))
for i, n := range nodes {
switch n := n.(type) {
case *IngressPathLiteral:
out[i] = &schemapb.IngressPathComponent{Value: &schemapb.IngressPathComponent_IngressPathLiteral{IngressPathLiteral: n.ToProto().(*schemapb.IngressPathLiteral)}}
case *IngressPathParameter:
out[i] = &schemapb.IngressPathComponent{Value: &schemapb.IngressPathComponent_IngressPathParameter{IngressPathParameter: n.ToProto().(*schemapb.IngressPathParameter)}}
default:
panic(fmt.Sprintf("unhandled ingress path component type %T", n))
}
}
return out
}
// TypeToProto creates a schemapb.Type "sum type" from a concreate Type.
func TypeToProto(t Type) *schemapb.Type {
switch t := t.(type) {
case *Any:
return &schemapb.Type{Value: &schemapb.Type_Any{Any: t.ToProto().(*schemapb.Any)}}
case *Unit:
return &schemapb.Type{Value: &schemapb.Type_Unit{Unit: t.ToProto().(*schemapb.Unit)}}
case *Ref:
return &schemapb.Type{Value: &schemapb.Type_Ref{Ref: t.ToProto().(*schemapb.Ref)}}
case *Int:
return &schemapb.Type{Value: &schemapb.Type_Int{Int: t.ToProto().(*schemapb.Int)}}
case *Float:
return &schemapb.Type{Value: &schemapb.Type_Float{Float: t.ToProto().(*schemapb.Float)}}
case *String:
return &schemapb.Type{Value: &schemapb.Type_String_{String_: t.ToProto().(*schemapb.String)}}
case *Bytes:
return &schemapb.Type{Value: &schemapb.Type_Bytes{Bytes: t.ToProto().(*schemapb.Bytes)}}
case *Time:
return &schemapb.Type{Value: &schemapb.Type_Time{Time: t.ToProto().(*schemapb.Time)}}
case *Bool:
return &schemapb.Type{Value: &schemapb.Type_Bool{Bool: t.ToProto().(*schemapb.Bool)}}
case *Array:
return &schemapb.Type{Value: &schemapb.Type_Array{Array: t.ToProto().(*schemapb.Array)}}
case *Map:
return &schemapb.Type{Value: &schemapb.Type_Map{Map: t.ToProto().(*schemapb.Map)}}
case *Optional:
return &schemapb.Type{Value: &schemapb.Type_Optional{Optional: t.ToProto().(*schemapb.Optional)}}
}
panic(fmt.Sprintf("unhandled type: %T", t))
}
func valueToProto(v Value) *schemapb.Value {
switch t := v.(type) {
case *StringValue:
return &schemapb.Value{Value: &schemapb.Value_StringValue{StringValue: t.ToProto().(*schemapb.StringValue)}}
case *IntValue:
return &schemapb.Value{Value: &schemapb.Value_IntValue{IntValue: t.ToProto().(*schemapb.IntValue)}}
case *TypeValue:
return &schemapb.Value{Value: &schemapb.Value_TypeValue{TypeValue: t.ToProto().(*schemapb.TypeValue)}}
}
panic(fmt.Sprintf("unhandled value type: %T", v))
}