forked from xo/xo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
funcs.go
638 lines (548 loc) · 14.7 KB
/
funcs.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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
package internal
import (
"strconv"
"strings"
"text/template"
"github.com/knq/snaker"
"github.com/xo/xo/models"
)
// NewTemplateFuncs returns a set of template funcs bound to the supplied args.
func (a *ArgType) NewTemplateFuncs() template.FuncMap {
return template.FuncMap{
"colcount": a.colcount,
"colnames": a.colnames,
"colnamesmulti": a.colnamesmulti,
"colnamesquery": a.colnamesquery,
"colnamesquerymulti": a.colnamesquerymulti,
"colprefixnames": a.colprefixnames,
"colvals": a.colvals,
"colvalsmulti": a.colvalsmulti,
"fieldnames": a.fieldnames,
"fieldnamesmulti": a.fieldnamesmulti,
"goparamlist": a.goparamlist,
"reniltype": a.reniltype,
"retype": a.retype,
"shortname": a.shortname,
"convext": a.convext,
"schema": a.schemafn,
"colname": a.colname,
"hascolumn": a.hascolumn,
"hasfield": a.hasfield,
"getstartcount": a.getstartcount,
}
}
// retype checks typ against known types, and prefixing
// ArgType.CustomTypePackage (if applicable).
func (a *ArgType) retype(typ string) string {
if strings.Contains(typ, ".") {
return typ
}
prefix := ""
for strings.HasPrefix(typ, "[]") {
typ = typ[2:]
prefix = prefix + "[]"
}
if _, ok := a.KnownTypeMap[typ]; !ok {
pkg := a.CustomTypePackage
if pkg != "" {
pkg = pkg + "."
}
return prefix + pkg + typ
}
return prefix + typ
}
// reniltype checks typ against known nil types (similar to retype), prefixing
// ArgType.CustomTypePackage (if applicable).
func (a *ArgType) reniltype(typ string) string {
if strings.Contains(typ, ".") {
return typ
}
if strings.HasSuffix(typ, "{}") {
if _, ok := a.KnownTypeMap[typ[:len(typ)-2]]; ok {
return typ
}
pkg := a.CustomTypePackage
if pkg != "" {
pkg = pkg + "."
}
return pkg + typ
}
return typ
}
// shortname generates a safe Go identifier for typ. typ is first checked
// against ArgType.ShortNameTypeMap, and if not found, then the value is
// calculated and stored in the ShortNameTypeMap for future use.
//
// A shortname is the concatentation of the lowercase of the first character in
// the words comprising the name. For example, "MyCustomName" will have have
// the shortname of "mcn".
//
// If a generated shortname conflicts with a Go reserved name, then the
// corresponding value in goReservedNames map will be used.
//
// Generated shortnames that have conflicts with any scopeConflicts member will
// have ArgType.NameConflictSuffix appended.
//
// Note: recognized types for scopeConflicts are string, []*Field,
// []*QueryParam.
func (a *ArgType) shortname(typ string, scopeConflicts ...interface{}) string {
var v string
var ok bool
// check short name map
if v, ok = a.ShortNameTypeMap[typ]; !ok {
// calc the short name
u := []string{}
for _, s := range strings.Split(strings.ToLower(snaker.CamelToSnake(typ)), "_") {
if len(s) > 0 && s != "id" {
u = append(u, s[:1])
}
}
v = strings.Join(u, "")
// check go reserved names
if n, ok := goReservedNames[v]; ok {
v = n
}
// store back to short name map
a.ShortNameTypeMap[typ] = v
}
// initial conflicts are the default imported packages from
// xo_package.go.tpl
conflicts := map[string]bool{
"sql": true,
"driver": true,
"csv": true,
"errors": true,
"fmt": true,
"regexp": true,
"strings": true,
"time": true,
}
// add scopeConflicts to conflicts
for _, c := range scopeConflicts {
switch k := c.(type) {
case string:
conflicts[k] = true
case []*Field:
for _, f := range k {
conflicts[f.Name] = true
}
case []*QueryParam:
for _, f := range k {
conflicts[f.Name] = true
}
default:
panic("not implemented")
}
}
// append suffix if conflict exists
if _, ok := conflicts[v]; ok {
v = v + a.NameConflictSuffix
}
return v
}
// colnames creates a list of the column names found in fields, excluding any
// Field with Name contained in ignoreNames.
//
// Used to present a comma separated list of column names, that can be used in
// a SELECT, or UPDATE, or other SQL clause requiring an list of identifiers
// (ie, "field_1, field_2, field_3, ...").
func (a *ArgType) colnames(fields []*Field, ignoreNames ...string) string {
ignore := map[string]bool{}
for _, n := range ignoreNames {
ignore[n] = true
}
str := ""
i := 0
for _, f := range fields {
if ignore[f.Name] {
continue
}
if i != 0 {
str = str + ", "
}
str = str + a.colname(f.Col)
i++
}
return str
}
// colnamesmulti creates a list of the column names found in fields, excluding any
// Field with Name contained in ignoreNames.
//
// Used to present a comma separated list of column names, that can be used in
// a SELECT, or UPDATE, or other SQL clause requiring an list of identifiers
// (ie, "field_1, field_2, field_3, ...").
func (a *ArgType) colnamesmulti(fields []*Field, ignoreNames []*Field) string {
ignore := map[string]bool{}
for _, f := range ignoreNames {
ignore[f.Name] = true
}
str := ""
i := 0
for _, f := range fields {
if ignore[f.Name] {
continue
}
if i != 0 {
str = str + ", "
}
str = str + a.colname(f.Col)
i++
}
return str
}
// colnamesquery creates a list of the column names in fields as a query and
// joined by sep, excluding any Field with Name contained in ignoreNames.
//
// Used to create a list of column names in a WHERE clause (ie, "field_1 = $1
// AND field_2 = $2 AND ...") or in an UPDATE clause (ie, "field = $1, field =
// $2, ...").
func (a *ArgType) colnamesquery(fields []*Field, sep string, ignoreNames ...string) string {
ignore := map[string]bool{}
for _, n := range ignoreNames {
ignore[n] = true
}
str := ""
i := 0
for _, f := range fields {
if ignore[f.Name] {
continue
}
if i != 0 {
str = str + sep
}
str = str + a.colname(f.Col) + " = " + a.Loader.NthParam(i)
i++
}
return str
}
// colnamesquerymulti creates a list of the column names in fields as a query and
// joined by sep, excluding any Field with Name contained in the slice of fields in ignoreNames.
//
// Used to create a list of column names in a WHERE clause (ie, "field_1 = $1
// AND field_2 = $2 AND ...") or in an UPDATE clause (ie, "field = $1, field =
// $2, ...").
func (a *ArgType) colnamesquerymulti(fields []*Field, sep string, startCount int, ignoreNames []*Field) string {
ignore := map[string]bool{}
for _, f := range ignoreNames {
ignore[f.Name] = true
}
str := ""
i := startCount
for _, f := range fields {
if ignore[f.Name] {
continue
}
if i > startCount {
str = str + sep
}
str = str + a.colname(f.Col) + " = " + a.Loader.NthParam(i)
i++
}
return str
}
// colprefixnames creates a list of the column names found in fields with the
// supplied prefix, excluding any Field with Name contained in ignoreNames.
//
// Used to present a comma separated list of column names with a prefix. Used in
// a SELECT, or UPDATE (ie, "t.field_1, t.field_2, t.field_3, ...").
func (a *ArgType) colprefixnames(fields []*Field, prefix string, ignoreNames ...string) string {
ignore := map[string]bool{}
for _, n := range ignoreNames {
ignore[n] = true
}
str := ""
i := 0
for _, f := range fields {
if ignore[f.Name] {
continue
}
if i != 0 {
str = str + ", "
}
str = str + prefix + "." + a.colname(f.Col)
i++
}
return str
}
// colvals creates a list of value place holders for fields excluding any Field
// with Name contained in ignoreNames.
//
// Used to present a comma separated list of column place holders, used in a
// SELECT or UPDATE statement (ie, "$1, $2, $3 ...").
func (a *ArgType) colvals(fields []*Field, ignoreNames ...string) string {
ignore := map[string]bool{}
for _, n := range ignoreNames {
ignore[n] = true
}
str := ""
i := 0
for _, f := range fields {
if ignore[f.Name] {
continue
}
if i != 0 {
str = str + ", "
}
str = str + a.Loader.NthParam(i)
i++
}
return str
}
// colvalsmulti creates a list of value place holders for fields excluding any Field
// with Name contained in ignoreNames.
//
// Used to present a comma separated list of column place holders, used in a
// SELECT or UPDATE statement (ie, "$1, $2, $3 ...").
func (a *ArgType) colvalsmulti(fields []*Field, ignoreNames []*Field) string {
ignore := map[string]bool{}
for _, f := range ignoreNames {
ignore[f.Name] = true
}
str := ""
i := 0
for _, f := range fields {
if ignore[f.Name] {
continue
}
if i != 0 {
str = str + ", "
}
str = str + a.Loader.NthParam(i)
i++
}
return str
}
// fieldnames creates a list of field names from fields of the adding the
// provided prefix, and excluding any Field with Name contained in ignoreNames.
//
// Used to present a comma separated list of field names, ie in a Go statement
// (ie, "t.Field1, t.Field2, t.Field3 ...")
func (a *ArgType) fieldnames(fields []*Field, prefix string, ignoreNames ...string) string {
ignore := map[string]bool{}
for _, n := range ignoreNames {
ignore[n] = true
}
str := ""
i := 0
for _, f := range fields {
if ignore[f.Name] {
continue
}
if i != 0 {
str = str + ", "
}
str = str + prefix + "." + f.Name
i++
}
return str
}
// fieldnamesmulti creates a list of field names from fields of the adding the
// provided prefix, and excluding any Field with the slice contained in ignoreNames.
//
// Used to present a comma separated list of field names, ie in a Go statement
// (ie, "t.Field1, t.Field2, t.Field3 ...")
func (a *ArgType) fieldnamesmulti(fields []*Field, prefix string, ignoreNames []*Field) string {
ignore := map[string]bool{}
for _, f := range ignoreNames {
ignore[f.Name] = true
}
str := ""
i := 0
for _, f := range fields {
if ignore[f.Name] {
continue
}
if i != 0 {
str = str + ", "
}
str = str + prefix + "." + f.Name
i++
}
return str
}
// colcount returns the 1-based count of fields, excluding any Field with Name
// contained in ignoreNames.
//
// Used to get the count of fields, and useful for specifying the last SQL
// parameter.
func (a *ArgType) colcount(fields []*Field, ignoreNames ...string) int {
ignore := map[string]bool{}
for _, n := range ignoreNames {
ignore[n] = true
}
i := 1
for _, f := range fields {
if ignore[f.Name] {
continue
}
i++
}
return i
}
// goReservedNames is a map of of go reserved names to "safe" names.
var goReservedNames = map[string]string{
"break": "brk",
"case": "cs",
"chan": "chn",
"const": "cnst",
"continue": "cnt",
"default": "def",
"defer": "dfr",
"else": "els",
"fallthrough": "flthrough",
"for": "fr",
"func": "fn",
"go": "goVal",
"goto": "gt",
"if": "ifVal",
"import": "imp",
"interface": "iface",
"map": "mp",
"package": "pkg",
"range": "rnge",
"return": "ret",
"select": "slct",
"struct": "strct",
"switch": "swtch",
"type": "typ",
"var": "vr",
// go types
"error": "e",
"bool": "b",
"string": "str",
"byte": "byt",
"rune": "r",
"uintptr": "uptr",
"int": "i",
"int8": "i8",
"int16": "i16",
"int32": "i32",
"int64": "i64",
"uint": "u",
"uint8": "u8",
"uint16": "u16",
"uint32": "u32",
"uint64": "u64",
"float32": "z",
"float64": "f",
"complex64": "c",
"complex128": "c128",
}
// goparamlist converts a list of fields into their named Go parameters,
// skipping any Field with Name contained in ignoreNames. addType will cause
// the go Type to be added after each variable name. addPrefix will cause the
// returned string to be prefixed with ", " if the generated string is not
// empty.
//
// Any field name encountered will be checked against goReservedNames, and will
// have its name substituted by its corresponding looked up value.
//
// Used to present a comma separated list of Go variable names for use with as
// either a Go func parameter list, or in a call to another Go func.
// (ie, ", a, b, c, ..." or ", a T1, b T2, c T3, ...").
func (a *ArgType) goparamlist(fields []*Field, addPrefix bool, addType bool, ignoreNames ...string) string {
ignore := map[string]bool{}
for _, n := range ignoreNames {
ignore[n] = true
}
i := 0
vals := []string{}
for _, f := range fields {
if ignore[f.Name] {
continue
}
s := "v" + strconv.Itoa(i)
if len(f.Name) > 0 {
n := strings.Split(snaker.CamelToSnake(f.Name), "_")
s = strings.ToLower(n[0]) + f.Name[len(n[0]):]
}
// check go reserved names
if r, ok := goReservedNames[strings.ToLower(s)]; ok {
s = r
}
// add the go type
if addType {
s += " " + a.retype(f.Type)
}
// add to vals
vals = append(vals, s)
i++
}
// concat generated values
str := strings.Join(vals, ", ")
if addPrefix && str != "" {
return ", " + str
}
return str
}
// convext generates the Go conversion for f in order for it to be assignable
// to t.
//
// FIXME: this should be a better name, like "goconversion" or some such.
func (a *ArgType) convext(prefix string, f *Field, t *Field) string {
expr := prefix + "." + f.Name
if f.Type == t.Type {
return expr
}
ft := f.Type
if strings.HasPrefix(ft, "sql.Null") {
expr = expr + "." + f.Type[8:]
ft = strings.ToLower(f.Type[8:])
}
if t.Type != ft {
expr = t.Type + "(" + expr + ")"
}
return expr
}
// schemafn takes a series of names and joins them with the schema name.
func (a *ArgType) schemafn(s string, names ...string) string {
// escape table names
if a.EscapeTableNames {
for i, t := range names {
names[i] = a.Loader.Escape(TableEsc, t)
}
}
n := strings.Join(names, ".")
if s == "" && n == "" {
return ""
}
if s != "" && n != "" {
if a.EscapeSchemaName {
s = a.Loader.Escape(SchemaEsc, s)
}
s = s + "."
}
return s + n
}
// colname returns the ColumnName of col, optionally escaping it if
// ArgType.EscapeColumnNames is toggled.
func (a *ArgType) colname(col *models.Column) string {
if a.EscapeColumnNames {
return a.Loader.Escape(ColumnEsc, col.ColumnName)
}
return col.ColumnName
}
// hascolumn takes a list of fields and determines if field with the specified
// column name is in the list.
func (a *ArgType) hascolumn(fields []*Field, name string) bool {
for _, f := range fields {
if f.Col.ColumnName == name {
return true
}
}
return false
}
// hasfield takes a list of fields and determines if field with the specified
// field name is in the list.
func (a *ArgType) hasfield(fields []*Field, name string) bool {
for _, f := range fields {
if f.Name == name {
return true
}
}
return false
}
// getstartcount returns a starting count for numbering columsn in queries
func (a *ArgType) getstartcount(fields []*Field, pkFields []*Field) int {
return len(fields) - len(pkFields)
}