/
typemap.go
547 lines (513 loc) · 16.1 KB
/
typemap.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
// Copyright 2015-2017, Cyrill @ Schumacher.fm and the CoreStore contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dmlgen
import (
"fmt"
"strings"
"unicode"
"unicode/utf8"
"github.com/corestoreio/errors"
"github.com/corestoreio/pkg/sql/ddl"
"github.com/corestoreio/pkg/util/strs"
)
// TypeDef used in variable `mysqlTypeToGo` to map a MySQL/MariaDB type to its
// appropriate Go and serializer type. Those types are getting printed in the
// generated files.
type TypeDef struct {
GoUNull string // unsigned null
GoUNotNull string // unsigned not null
GoNull string // A go type which can be null/nil
GoNotNull string // A usually native Go primitive type
SerializerUNull string
SerializerUNotNull string
SerializerNull string
SerializerNotNull string
}
var typeMap = map[string]map[string]*TypeDef{ // immutable
// Go native type, covers also unsigned and NULL
"int64": {
// implementation name. Default uses no serializer.
"default": &TypeDef{
GoUNull: "null.Uint64", // Go unsigned null
GoUNotNull: "uint64", // Go unsigned not null
GoNull: "null.Int64", // Go signed null
GoNotNull: "int64", // Go signed not null
},
// proto uses protocol buffers or gogoproto as serializer. It requires a
// mapping of the go type to the protobuf type. Some native Go types are
// not supported in protobuf. For example if the DB column type is
// smallint, Go and protobuf types must be minimum int32. This can cause
// data loss or overflow when writing int32 into the DB. So you think it
// runs correctly until you load the values back from the DB...
"protobuf": {
GoUNull: "null.Uint64", // Go unsigned null
GoUNotNull: "uint64", // Go unsigned not null
GoNull: "null.Int64", // Go signed null
GoNotNull: "int64", // Go signed not null
// native types of the protobuf implementation. pkg null refers to
// storage/null/null.proto file
SerializerUNull: "optional uint64", // proto unsigned null | no pun intended ;-)
SerializerUNotNull: "uint64", // proto unsigned not null
SerializerNull: "optional int64", // proto signed null
SerializerNotNull: "int64", // proto signed not null
},
"fbs": {
GoUNull: "null.Uint64",
GoUNotNull: "uint64",
GoNull: "null.Int64",
GoNotNull: "int64",
// native types of the flatbuffers implementation. pkg null refers
// to storage/null/null.fbs file
SerializerUNull: "optional uint64", // fbs unsigned null
SerializerUNotNull: "ulong", // fbs unsigned not null
SerializerNull: "optional int64", // fbs signed null
SerializerNotNull: "long", // fbs signed not null
},
},
"int32": {
"default": &TypeDef{
GoUNull: "null.Uint32",
GoUNotNull: "uint32",
GoNull: "null.Int32",
GoNotNull: "int32",
},
"protobuf": {
GoUNull: "null.Uint32",
GoUNotNull: "uint32",
GoNull: "null.Int32",
GoNotNull: "int32",
SerializerUNull: "optional uint32",
SerializerUNotNull: "uint32",
SerializerNull: "optional int32",
SerializerNotNull: "int32",
},
"fbs": {
GoUNull: "null.Uint32",
GoUNotNull: "uint32",
GoNull: "null.Int32",
GoNotNull: "int32",
SerializerUNull: "optional uint32",
SerializerUNotNull: "uint",
SerializerNull: "optional int32",
SerializerNotNull: "int",
},
},
"int16": {
"default": &TypeDef{
GoUNull: "null.Uint16",
GoUNotNull: "uint16",
GoNull: "null.Int16",
GoNotNull: "int16",
},
"protobuf": {
GoUNull: "null.Uint32",
GoUNotNull: "uint32",
GoNull: "null.Int32",
GoNotNull: "int32",
SerializerUNull: "optional uint32",
SerializerUNotNull: "uint32",
SerializerNull: "optional int32",
SerializerNotNull: "int32",
},
"fbs": {
GoUNull: "null.Uint16",
GoUNotNull: "uint16",
GoNull: "null.Int16",
GoNotNull: "int16",
SerializerUNull: "optional uint16",
SerializerUNotNull: "ushort",
SerializerNull: "null.Int16",
SerializerNotNull: "short",
},
},
"int8": {
"default": &TypeDef{
GoUNull: "null.Uint8",
GoUNotNull: "uint8",
GoNull: "null.Int8",
GoNotNull: "int8",
},
"protobuf": {
GoUNull: "null.Uint32",
GoUNotNull: "uint32",
GoNull: "null.Int32",
GoNotNull: "int32",
SerializerUNull: "optional uint32",
SerializerUNotNull: "uint32",
SerializerNull: "optional int32",
SerializerNotNull: "int32",
},
"fbs": {
GoUNull: "null.Uint8",
GoUNotNull: "uint8",
GoNull: "null.Int8",
GoNotNull: "int8",
SerializerUNull: "optional uint8",
SerializerUNotNull: "ubyte",
SerializerNull: "optional int8",
SerializerNotNull: "byte",
},
},
"float64": {
"default": &TypeDef{
GoUNull: "null.Uint8",
GoUNotNull: "float64",
GoNull: "null.Int8",
GoNotNull: "float64",
},
"protobuf": {
GoUNull: "null.Float64",
GoUNotNull: "float64",
GoNull: "null.Float64",
GoNotNull: "float64",
SerializerUNull: "optional float64",
SerializerUNotNull: "double",
SerializerNull: "optional float64",
SerializerNotNull: "double",
},
"fbs": {
GoUNull: "null.Float64",
GoUNotNull: "float64",
GoNull: "null.Float64",
GoNotNull: "float64",
SerializerUNull: "null.Double",
SerializerUNotNull: "double",
SerializerNull: "null.Double",
SerializerNotNull: "double",
},
},
"time": {
"default": &TypeDef{
GoUNull: "null.Time",
GoUNotNull: "time.Time",
GoNull: "null.Time",
GoNotNull: "time.Time",
},
"protobuf": {
GoUNull: "null.Time",
GoUNotNull: "time.Time",
GoNull: "null.Time",
GoNotNull: "time.Time",
SerializerUNull: "optional google.protobuf.Timestamp",
SerializerUNotNull: "google.protobuf.Timestamp",
SerializerNull: "optional google.protobuf.Timestamp",
SerializerNotNull: "google.protobuf.Timestamp",
},
"fbs": {
GoUNull: "null.Time",
GoUNotNull: "time.Time",
GoNull: "null.Time",
GoNotNull: "time.Time",
SerializerUNull: "null.Time",
SerializerUNotNull: "null.Time",
SerializerNull: "null.Time",
SerializerNotNull: "null.Time",
},
},
"string": {
"default": &TypeDef{
GoUNull: "null.String",
GoUNotNull: "string",
GoNull: "null.String",
GoNotNull: "string",
},
"protobuf": {
GoUNull: "null.String",
GoUNotNull: "string",
GoNull: "null.String",
GoNotNull: "string",
SerializerUNull: "optional string",
SerializerUNotNull: "string",
SerializerNull: "optional string",
SerializerNotNull: "string",
},
"fbs": {
GoUNull: "null.String",
GoUNotNull: "string",
GoNull: "null.String",
GoNotNull: "string",
SerializerUNull: "null.String",
SerializerUNotNull: "string",
SerializerNull: "null.String",
SerializerNotNull: "string",
},
},
"bool": {
"default": &TypeDef{
GoUNull: "null.Bool",
GoUNotNull: "bool",
GoNull: "null.Bool",
GoNotNull: "bool",
},
"protobuf": {
GoUNull: "null.Bool",
GoUNotNull: "bool",
GoNull: "null.Bool",
GoNotNull: "bool",
SerializerUNull: "optional bool",
SerializerUNotNull: "bool",
SerializerNull: "optional bool",
SerializerNotNull: "bool",
},
"fbs": {
GoUNull: "null.Bool",
GoUNotNull: "bool",
GoNull: "null.Bool",
GoNotNull: "bool",
SerializerUNull: "null.Bool",
SerializerUNotNull: "bool",
SerializerNull: "null.Bool",
SerializerNotNull: "bool",
},
},
"decimal": {
"default": &TypeDef{
GoUNull: "null.Decimal",
GoUNotNull: "null.Decimal",
GoNull: "null.Decimal",
GoNotNull: "null.Decimal",
},
"protobuf": {
GoUNull: "null.Decimal",
GoUNotNull: "null.Decimal",
GoNull: "null.Decimal",
GoNotNull: "null.Decimal",
SerializerUNull: "null.Decimal",
SerializerUNotNull: "null.Decimal",
SerializerNull: "null.Decimal",
SerializerNotNull: "null.Decimal",
},
"fbs": {
GoUNull: "null.Decimal",
GoUNotNull: "null.Decimal",
GoNull: "null.Decimal",
GoNotNull: "null.Decimal",
SerializerUNull: "null.Decimal",
SerializerUNotNull: "null.Decimal",
SerializerNull: "null.Decimal",
SerializerNotNull: "null.Decimal",
},
},
"byte": {
"default": &TypeDef{
GoUNull: "[]byte",
GoUNotNull: "[]byte",
GoNull: "[]byte",
GoNotNull: "[]byte",
},
"protobuf": {
GoUNull: "[]byte",
GoUNotNull: "[]byte",
GoNull: "[]byte",
GoNotNull: "[]byte",
SerializerUNull: "bytes",
SerializerUNotNull: "bytes",
SerializerNull: "bytes",
SerializerNotNull: "bytes",
},
"fbs": {
GoUNull: "[]byte",
GoUNotNull: "[]byte",
GoNull: "[]byte",
GoNotNull: "[]byte",
SerializerUNull: "[ubyte]",
SerializerUNotNull: "[ubyte]",
SerializerNull: "[ubyte]",
SerializerNotNull: "[ubyte]",
},
},
}
func mustTMK(key string) map[string]*TypeDef {
if m, ok := typeMap[key]; ok {
return m
}
panic(fmt.Sprintf("[dmlgen] Key %q not found in typeMap", key))
}
// mysqlTypeToGo maps the MySql/MariaDB field type to the correct Go/protobuf
// type. See the type TypeDef for more details. This exported variable allows to
// set custom types before code generation.
var mysqlTypeToGo = map[string]map[string]*TypeDef{ // immutable
// key1=MySQL Type; key2=serializer;
"int": mustTMK("int32"),
"bigint": mustTMK("int64"),
"smallint": mustTMK("int16"),
"tinyint": mustTMK("int8"),
"mediumint": mustTMK("int32"),
"double": mustTMK("float64"),
"float": mustTMK("float64"),
"decimal": mustTMK("decimal"),
"date": mustTMK("time"),
"datetime": mustTMK("time"),
"timestamp": mustTMK("time"),
"time": mustTMK("time"),
"char": mustTMK("string"),
"varchar": mustTMK("string"),
"enum": mustTMK("string"),
"set": mustTMK("string"),
"text": mustTMK("string"),
"longtext": mustTMK("string"),
"mediumtext": mustTMK("string"),
"tinytext": mustTMK("string"),
"blob": mustTMK("byte"),
"longblob": mustTMK("byte"),
"mediumblob": mustTMK("byte"),
"tinyblob": mustTMK("byte"),
"binary": mustTMK("byte"),
"varbinary": mustTMK("byte"),
"bit": mustTMK("bool"),
// TODO add more MySQL types like JSON or GEO
}
func mustGetTypeDef(mysqlDataType, serializer string) *TypeDef {
if serializer == "" {
serializer = "default"
}
myType, ok := mysqlTypeToGo[mysqlDataType] // readonly access so safe for concurrent access
if !ok {
panic(errors.NotFound.Newf("[dmlgen] MySQL type %q not found", mysqlDataType))
}
goType, ok := myType[serializer]
if !ok {
panic(errors.NotFound.Newf("[dmlgen] Serializer %q not found", serializer))
}
return goType
}
func (g *Generator) findType(c *ddl.Column) *TypeDef {
goType := mustGetTypeDef(c.DataType, g.Serializer)
// The switch block overwrites the already retrieved goType by checking for
// bool columns and columns which contains a money unit.
switch {
case c.IsBool():
goType = mustGetTypeDef("bit", g.Serializer)
case c.IsFloat() && c.IsMoney():
goType = mustGetTypeDef("decimal", g.Serializer)
}
return goType
}
func (g *Generator) goTypeNull(c *ddl.Column) string { return g.mySQLToGoType(c, true) }
func (g *Generator) goType(c *ddl.Column) string { return g.mySQLToGoType(c, false) }
func (g *Generator) goFuncNull(c *ddl.Column) string { return g.mySQLToGoDmlColumnMap(c, true) }
// mySQLToGoType calculates the data type of the field DataType. For example
// bigint, smallint, tinyint will result in "int". If withNull is true the
// returned type can store a null value.
func (g *Generator) mySQLToGoType(c *ddl.Column, withNull bool) string {
goType := g.findType(c)
var t string
switch {
case c.IsUnsigned() && c.IsNull() && withNull:
t = goType.GoUNull // unsigned null
case c.IsUnsigned() && (!c.IsNull() || !withNull):
t = goType.GoUNotNull // unsigned not null
case !c.IsUnsigned() && c.IsNull() && withNull:
t = goType.GoNull // signed null
case !c.IsUnsigned() && (!c.IsNull() || !withNull):
t = goType.GoNotNull // signed not null
}
return t
}
// toGoPrimitiveFromNull returns for a Go type or structure the final primitive:
// int->int but NullInt->.Int. Either it is the struct field name or final type
// of a composite nullable type.
func (g *Generator) toGoPrimitiveFromNull(c *ddl.Column) string {
t := g.mySQLToGoType(c, true)
field := strs.ToGoCamelCase(c.Field)
if strings.HasPrefix(t, "null.") && t != "null.Decimal" {
f := t[5:] // 5 == len("null.")
if t == "null.String" {
f = "Data" // null.String type has field name `Data` instead of `String`
}
t = field + "." + f
} else {
t = field
}
return t
}
func (g *Generator) mySQLToGoDmlColumnMap(c *ddl.Column, withNull bool) string {
gt := g.mySQLToGoType(c, withNull)
if gt == "[]byte" {
return "Byte"
}
if dot := strings.IndexByte(gt, '.'); dot > 0 {
pkg := gt[:dot]
fnName := gt[dot+1:]
if fnName == "Decimal" || (pkg == "time" && fnName == "Time") {
return fnName
}
r, n := utf8.DecodeRuneInString(pkg)
return string(unicode.ToUpper(r)) + pkg[n:] + fnName
}
r, n := utf8.DecodeRuneInString(gt)
return string(unicode.ToUpper(r)) + gt[n:]
}
func (g *Generator) convertType(from, to *ddl.Column, fieldName string) string {
if from.HasEqualType(to) {
return fieldName
}
var targetFn, toTargetFn string
targetFn = "panic"
targetFn = g.mySQLToGoType(to, to.IsNull())
if to.IsNull() {
// null.Uint32 => null.MakeUint32
targetFn = targetFn[0:5] + "Make" + targetFn[5:]
}
toTargetFn = g.mySQLToGoType(to, false)
if targetFn == toTargetFn {
return fieldName
}
return fmt.Sprintf("%s(%s(%s))", targetFn, toTargetFn, fieldName)
}
func (g *Generator) toSerializerType(c *ddl.Column, withNull bool) string {
goType := g.findType(c)
var t string
switch {
case c.IsUnsigned() && c.IsNull() && withNull:
t = goType.SerializerUNull // unsigned null
case c.IsUnsigned() && (!c.IsNull() || !withNull):
t = goType.SerializerUNotNull // unsigned not null
case !c.IsUnsigned() && c.IsNull() && withNull:
t = goType.SerializerNull // signed null
case !c.IsUnsigned() && (!c.IsNull() || !withNull):
t = goType.SerializerNotNull // signed not null
}
return t
}
// serializerType converts the column type to the supported type of the current
// serializer. For now supports only protobuf.
func (g *Generator) serializerType(c *ddl.Column) string {
pt := g.toSerializerType(c, true)
if strings.IndexByte(pt, '/') > 0 { // slash identifies an import path
return "bytes"
}
return pt
}
func mySQLType2GoComparisonOperator(c *ddl.Column) string {
switch c.DataType {
case "blob", "char", "longblob", "longtext", "mediumblob", "mediumtext",
"text", "tinytext", "varbinary", "varchar", "enum", "set":
return ` != ""`
case "date", "datetime", "time", "timestamp":
return ".IsZero() == false"
case "decimal":
return ".Valid "
case "bigint", "double", "float", "int", "smallint", "tinyint":
if c.IsNull() {
return ".Valid"
}
if c.IsUnsigned() {
return " > 0"
}
return " != 0"
default:
return " > 0 /*TODO find correct case*/"
}
}