-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgdb_field_common.go
290 lines (273 loc) · 7.53 KB
/
pgdb_field_common.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package pgdb
import (
"fmt"
pgdb_v1 "github.com/ductone/protoc-gen-pgdb/pgdb/v1"
pgs "github.com/lyft/protoc-gen-star"
pgsgo "github.com/lyft/protoc-gen-star/lang/go"
"google.golang.org/protobuf/types/descriptorpb"
)
type fieldConvert struct {
// starts as "m.self.", nested messages append
goPrefix string
ctx pgsgo.Context
ix *importTracker
F pgs.Field
varName string
NestedPrefix string
// https://www.postgresql.org/docs/current/datatype-numeric.html
PostgresTypeName string
IsArray bool
TypeConversion goTypeConversion
FullTextType pgdb_v1.FieldOptions_FullTextType
FullTextWeight pgdb_v1.FieldOptions_FullTextWeight
}
type goTypeConversion int64
const (
//nolint:deadcode,varcheck // i like unsued unspecified
gtUnspecified goTypeConversion = 0
gtFloat32 goTypeConversion = 1
gtFloat64 goTypeConversion = 2
gtInt32 goTypeConversion = 3
gtInt64 goTypeConversion = 4
gtUint32 goTypeConversion = 5
gtUint64 goTypeConversion = 6
gtBool goTypeConversion = 7
gtString goTypeConversion = 8
gtBytes goTypeConversion = 9
gtEnum goTypeConversion = 10
gtPbWktAny goTypeConversion = 11
gtPbWktTimestamp goTypeConversion = 12
gtPbWktDuration goTypeConversion = 13
gtPbWktStruct goTypeConversion = 14
gtPbGenericMsg goTypeConversion = 15
gtPbNestedMsg goTypeConversion = 16
gtPbWktBoolValue goTypeConversion = 17
gtPbWktStringValue goTypeConversion = 18
gtInetAddr goTypeConversion = 19
)
type formatContext struct {
VarName string
InputName string
CastType string
IsArray bool
NestedPrefix string
}
const fieldConvertString = "string"
func (fc *fieldConvert) GoType() (string, error) {
switch fc.TypeConversion {
case gtFloat32:
return "float32", nil
case gtFloat64:
return "float64", nil
case gtInt32:
return "int32", nil
case gtInt64:
return "int64", nil
case gtUint32:
return "uint32", nil
case gtUint64:
return "uint64", nil
case gtBool:
return "bool", nil
case gtString:
return fieldConvertString, nil
case gtBytes:
return "[]byte", nil
case gtEnum:
return "int32", nil
case gtPbWktAny, gtPbWktStruct, gtPbGenericMsg:
// objects are stored as JSONB, take input as interface{}, convert to
return "any", nil
case gtPbWktTimestamp:
return "time.Time", nil
case gtPbWktDuration:
return "time.Duration", nil
case gtPbWktBoolValue:
return "invalid", nil
case gtPbWktStringValue:
return "invalid", nil
case gtPbNestedMsg:
return string(fc.ctx.Type(fc.F)), nil
case gtInetAddr:
return "string", nil
default:
panic(fmt.Errorf("pgdb: Implement fieldConvert.GoType for %v", fc.TypeConversion))
}
}
func (fc *fieldConvert) CodeForValue() (string, error) {
selfName := fc.goPrefix + ".Get" + fc.ctx.Name(fc.F).String() + "()"
switch fc.TypeConversion {
case gtFloat32:
return templateExecToString("proto_format_cast.tmpl", &formatContext{
VarName: fc.varName,
InputName: selfName,
CastType: "float32",
IsArray: fc.IsArray,
})
case gtFloat64:
return templateExecToString("proto_format_cast.tmpl", &formatContext{
VarName: fc.varName,
InputName: selfName,
CastType: "float64",
IsArray: fc.IsArray,
})
case gtInt32:
return templateExecToString("proto_format_cast.tmpl", &formatContext{
VarName: fc.varName,
InputName: selfName,
CastType: "int32",
IsArray: fc.IsArray,
})
case gtInt64:
return templateExecToString("proto_format_cast.tmpl", &formatContext{
VarName: fc.varName,
InputName: selfName,
CastType: "int64",
IsArray: fc.IsArray,
})
case gtUint32:
return templateExecToString("proto_format_cast.tmpl", &formatContext{
VarName: fc.varName,
InputName: selfName,
CastType: "uint32",
IsArray: fc.IsArray,
})
case gtUint64:
return templateExecToString("proto_format_cast.tmpl", &formatContext{
VarName: fc.varName,
InputName: selfName,
CastType: "uint64",
IsArray: fc.IsArray,
})
case gtBool:
return templateExecToString("proto_format_cast.tmpl", &formatContext{
VarName: fc.varName,
InputName: selfName,
CastType: pgTypeBool,
IsArray: fc.IsArray,
})
case gtString:
return templateExecToString("proto_format_cast.tmpl", &formatContext{
VarName: fc.varName,
InputName: selfName,
CastType: fieldConvertString,
IsArray: fc.IsArray,
})
case gtInetAddr:
fc.ix.NetIP = true
return templateExecToString("proto_format_inet.tmpl", &formatContext{
VarName: fc.varName,
InputName: selfName,
})
case gtBytes:
return templateExecToString("proto_format_cast.tmpl", &formatContext{
VarName: fc.varName,
InputName: selfName,
CastType: "[]byte",
IsArray: fc.IsArray,
})
case gtEnum:
return templateExecToString("proto_format_cast.tmpl", &formatContext{
VarName: fc.varName,
InputName: selfName,
CastType: "int32",
IsArray: fc.IsArray,
})
case gtPbWktTimestamp:
fc.ix.Time = true
return templateExecToString("proto_format_time.tmpl", &formatContext{
VarName: fc.varName,
InputName: selfName,
})
case gtPbWktDuration:
fc.ix.PgType = true
return templateExecToString("proto_format_duration.tmpl", &formatContext{
VarName: fc.varName,
InputName: selfName,
})
case gtPbWktStringValue, gtPbWktBoolValue:
return templateExecToString("proto_format_wrapper.tmpl", &formatContext{
VarName: fc.varName,
InputName: selfName,
})
case gtPbWktStruct, gtPbWktAny, gtPbGenericMsg:
useProtoJSON := false
ft := fc.F.Type()
pt := ft.ProtoType()
if pt.Proto() == descriptorpb.FieldDescriptorProto_TYPE_MESSAGE {
if !ft.IsMap() {
useProtoJSON = true
}
}
if useProtoJSON {
fc.ix.ProtobufEncodingJSON = true
if fc.IsArray {
fc.ix.Bytes = true
}
return templateExecToString("proto_format_json_proto.tmpl", &formatContext{
VarName: fc.varName,
InputName: selfName,
IsArray: fc.IsArray,
})
} else {
fc.ix.JSON = true
return templateExecToString("proto_format_json_std.tmpl", &formatContext{
VarName: fc.varName,
InputName: selfName,
IsArray: fc.IsArray,
})
}
case gtPbNestedMsg:
return templateExecToString("proto_format_nested.tmpl", &formatContext{
VarName: fc.varName,
InputName: selfName,
IsArray: fc.IsArray,
NestedPrefix: fc.NestedPrefix,
})
default:
panic(fmt.Errorf("pgdb: Implement CodeForValue for %v", fc.TypeConversion))
}
}
func (fc *fieldConvert) VarForValue() (string, error) {
if fc.TypeConversion == gtPbNestedMsg {
return "", nil
}
return fc.varName, nil
}
func (fc *fieldConvert) VarForAppend() (string, error) {
if fc.TypeConversion != gtPbNestedMsg {
return "", nil
}
return fc.varName, nil
}
func (fc *fieldConvert) EnumForValue() (string, error) {
return "", nil
}
type stringFormatContext struct {
IsFloat bool
IsInt bool
IsUint bool
IsString bool
VarName string
}
func typeToString(ix *importTracker, pt pgs.ProtoType, varName string) (string, error) {
c := stringFormatContext{
VarName: varName,
}
switch pt {
case pgs.DoubleT, pgs.FloatT:
ix.Strconv = true
c.IsFloat = true
case pgs.Int64T, pgs.SFixed64, pgs.SInt64, pgs.Int32T, pgs.SFixed32, pgs.SInt32, pgs.EnumT:
ix.Strconv = true
c.IsInt = true
case pgs.UInt64T, pgs.Fixed64T, pgs.UInt32T, pgs.Fixed32T:
ix.Strconv = true
c.IsUint = true
case pgs.StringT:
c.IsString = true
default:
panic("typeToString: need to implement for your type")
}
return templateExecToString("proto_convert_string.tmpl", c)
}