forked from kanmu/dgw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdgw.go
948 lines (827 loc) · 22.3 KB
/
dgw.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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
// go:generate go-bindata -o bindata.go template mapconfig
package main
import (
"bytes"
"database/sql"
"fmt"
"go/format"
"io/ioutil"
"log"
"regexp"
"sort"
"strings"
"text/template"
"unicode"
"github.com/iancoleman/strcase"
"github.com/lib/pq"
"github.com/BurntSushi/toml"
_ "github.com/lib/pq" // postgres
"github.com/pkg/errors"
)
// Queryer database/sql compatible query interface
type Queryer interface {
Exec(string, ...interface{}) (sql.Result, error)
Query(string, ...interface{}) (*sql.Rows, error)
QueryRow(string, ...interface{}) *sql.Row
}
// OpenDB opens database connection
func OpenDB(connStr string) (*sql.DB, error) {
conn, err := sql.Open("postgres", connStr)
if err != nil {
return nil, errors.Wrap(err, "failed to connect to database")
}
return conn, nil
}
const pgLoadEnumDef = `
SELECT n.nspname AS schema,
pg_catalog.format_type ( t.oid, NULL ) AS name,
ARRAY( SELECT e.enumlabel
FROM pg_catalog.pg_enum e
WHERE e.enumtypid = t.oid
ORDER BY e.oid )
AS elements
FROM pg_catalog.pg_type t
LEFT JOIN pg_catalog.pg_namespace n
ON n.oid = t.typnamespace
WHERE ( t.typrelid = 0
OR ( SELECT c.relkind = 'c'
FROM pg_catalog.pg_class c
WHERE c.oid = t.typrelid
)
)
AND NOT EXISTS
( SELECT 1
FROM pg_catalog.pg_type el
WHERE el.oid = t.typelem
AND el.typarray = t.oid
)
AND n.nspname = $1
AND pg_catalog.pg_type_is_visible ( t.oid )
ORDER BY 1, 2;
`
const queryInterface = `
// Queryer database/sql compatible query interface
type Queryer interface {
Exec(string, ...interface{}) (sql.Result, error)
Query(string, ...interface{}) (*sql.Rows, error)
QueryRow(string, ...interface{}) *sql.Row
}
`
const pgLoadColumnDef = `
SELECT
a.attnum AS field_ordinal,
a.attname AS column_name,
format_type(a.atttypid, a.atttypmod) AS data_type,
a.attnotnull AS not_null,
COALESCE(pg_get_expr(ad.adbin, ad.adrelid), '') AS default_value,
COALESCE(ct.contype = 'p', false) AS is_primary_key,
CASE
WHEN a.atttypid = ANY ('{int,int8,int2}'::regtype[])
AND EXISTS (
SELECT 1 FROM pg_attrdef ad
WHERE ad.adrelid = a.attrelid
AND ad.adnum = a.attnum
AND pg_get_expr(ad.adbin, ad.adrelid) = 'nextval('''
|| (pg_get_serial_sequence (a.attrelid::regclass::text
, a.attname))::regclass
|| '''::regclass)'
)
THEN CASE a.atttypid
WHEN 'int'::regtype THEN 'serial'
WHEN 'int8'::regtype THEN 'bigserial'
WHEN 'int2'::regtype THEN 'smallserial'
END
WHEN a.atttypid = ANY ('{uuid}'::regtype[]) AND COALESCE(pg_get_expr(ad.adbin, ad.adrelid), '') != ''
THEN 'autogenuuid'
ELSE format_type(a.atttypid, a.atttypmod)
END AS data_type
FROM pg_attribute a
JOIN ONLY pg_class c ON c.oid = a.attrelid
JOIN ONLY pg_namespace n ON n.oid = c.relnamespace
LEFT JOIN pg_constraint ct ON ct.conrelid = c.oid
AND a.attnum = ANY(ct.conkey) AND ct.contype = 'p'
LEFT JOIN pg_attrdef ad ON ad.adrelid = c.oid AND ad.adnum = a.attnum
WHERE a.attisdropped = false
AND n.nspname = $1
AND c.relname = $2
AND a.attnum > 0
ORDER BY a.attnum
`
const pgLoadTableDef = `
SELECT
c.relkind AS type,
c.relname AS table_name
FROM pg_class c
JOIN ONLY pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = $1
AND c.relkind = 'r'
ORDER BY c.relname
`
const pgLoadColumnConstraint = `
SELECT pg_get_constraintdef(ct.oid) as condef
FROM pg_attribute a
JOIN ONLY pg_class c ON c.oid = a.attrelid
JOIN ONLY pg_namespace n ON n.oid = c.relnamespace
LEFT JOIN pg_constraint ct ON a.attnum = ANY(ct.conkey) AND ct.conrelid = c.oid
AND ct.contype = $4
WHERE
n.nspname = $1
AND c.relname = $2
AND a.attname = $3
`
// TypeMap go/db type map struct
type TypeMap struct {
Tables []string `toml:"tables"`
Fields []string `toml:"fields"`
DBTypes []string `toml:"db_types"`
NotNullGoType string `toml:"notnull_go_type"`
NullableGoType string `toml:"nullable_go_type"`
Tags string `toml:"tags"`
compiled bool
rePatterns []*regexp.Regexp
}
func (t *TypeMap) match(table, field, s string) bool {
if !t.compiled {
for _, v := range t.DBTypes {
if strings.HasPrefix(v, "re/") {
t.rePatterns = append(t.rePatterns, regexp.MustCompile(v[3:]))
}
}
}
if len(t.Tables) != 0 && !contains(table, t.Tables) {
return false
}
if len(t.Fields) != 0 && !contains(field, t.Fields) {
return false
}
if contains(s, t.DBTypes) || len(t.DBTypes) == 0 {
return true
}
for _, v := range t.rePatterns {
if v.MatchString(s) {
return true
}
}
return false
}
// AutoKeyMap auto generating key config
type AutoKeyMap struct {
Types []string `toml:"db_types"`
}
// PgTypeMapConfig go/db type map struct toml config
type PgTypeMapConfig map[string]*TypeMap
func (c PgTypeMapConfig) Match(table, field, s string) *TypeMap {
for _, v := range c {
if v.Tables == nil {
continue
}
if v.match(table, field, s) && (v.NotNullGoType != "" || v.NullableGoType != "") {
return v
}
}
for _, v := range c {
if v.Tables != nil {
continue
}
if v.match(table, field, s) && (v.NotNullGoType != "" || v.NullableGoType != "") {
return v
}
}
return nil
}
func (c PgTypeMapConfig) Tags(table, field, s string) string {
for _, v := range c {
if v.Tables == nil {
continue
}
if v.match(table, field, s) {
return v.Tags
}
}
return ""
}
// PgTable postgres table
type PgTable struct {
Schema string
Name string
DataType string
AutoGenPk bool
PrimaryKeys []*PgColumn
Columns []*PgColumn
}
var autoGenKeyCfg = &AutoKeyMap{
Types: []string{"smallserial", "serial", "bigserial", "autogenuuid"},
}
func (t *PgTable) setPrimaryKeyInfo(cfg *AutoKeyMap) {
t.AutoGenPk = false
for _, c := range t.Columns {
if c.IsPrimaryKey {
t.PrimaryKeys = append(t.PrimaryKeys, c)
for _, typ := range cfg.Types {
if c.DDLType == typ {
t.AutoGenPk = true
}
}
}
}
}
// PgColumn postgres columns
type PgColumn struct {
FieldOrdinal int
Name string
DataType string
DDLType string
NotNull bool
DefaultValue sql.NullString
IsPrimaryKey bool
IsUnique bool
IsForeignKey bool
ForeignKey string
}
// Struct go struct
type Struct struct {
Name string
Table *PgTable
Comment string
Fields []*StructField
}
// StructTmpl go struct passed to template
type StructTmpl struct {
Struct *Struct
}
type SchemaTmpl struct {
Package string
Structs []*Struct
}
// StructField go struct field
type StructField struct {
Name string
Type string
Tag string
Column *PgColumn
}
// PgLoadTypeMapFromFile load type map from toml file
func PgLoadTypeMapFromFile(filePath string) (*PgTypeMapConfig, error) {
var conf PgTypeMapConfig
if _, err := toml.DecodeFile(filePath, &conf); err != nil {
return nil, errors.Wrap(err, "faild to parse config file")
}
return &conf, nil
}
type PgEnum struct {
Schema string
Name string
Values []string
}
type EnumValue struct {
Type *EnumType
Name string
Value string
}
type EnumType struct {
Name string
Enum *PgEnum
Comment string
Values []EnumValue
}
func PgLoadEnumDef(db Queryer, schema string) ([]*PgEnum, error) {
enumDefs, err := db.Query(pgLoadEnumDef, schema)
if err != nil {
return nil, errors.Wrap(err, "failed to load enum def")
}
enums := []*PgEnum{}
for enumDefs.Next() {
e := &PgEnum{}
var vals pq.StringArray
err := enumDefs.Scan(
&e.Schema,
&e.Name,
&vals,
)
e.Values = vals
if err != nil {
return nil, errors.Wrap(err, "failed to scan")
}
e.Name = strings.Replace(e.Name, `"`, "", -1)
enums = append(enums, e)
}
return enums, nil
}
// PgLoadColumnDef load Postgres column definition
func PgLoadColumnDef(db Queryer, schema string, table string) ([]*PgColumn, error) {
colDefs, err := db.Query(pgLoadColumnDef, schema, table)
if err != nil {
return nil, errors.Wrap(err, "failed to load table def")
}
cols := []*PgColumn{}
for colDefs.Next() {
c := &PgColumn{}
err := colDefs.Scan(
&c.FieldOrdinal,
&c.Name,
&c.DataType,
&c.NotNull,
&c.DefaultValue,
&c.IsPrimaryKey,
&c.DDLType,
)
if err != nil {
return nil, errors.Wrap(err, "failed to scan")
}
c.DataType = strings.Replace(c.DataType, `"`, "", -1)
// Some data types have an extra part e.g, "character varying(16)" and
// "numeric(10, 5)". We want to drop the extra part.
if i := strings.Index(c.DataType, "("); i > 0 {
c.DataType = c.DataType[0:i]
}
cols = append(cols, c)
}
return cols, nil
}
// PgLoadTableDef load Postgres table definition
func PgLoadTableDef(db Queryer, schema string) ([]*PgTable, error) {
tbDefs, err := db.Query(pgLoadTableDef, schema)
if err != nil {
return nil, errors.Wrap(err, "failed to load table def")
}
tbs := []*PgTable{}
for tbDefs.Next() {
t := &PgTable{Schema: schema}
err := tbDefs.Scan(
&t.DataType,
&t.Name,
)
if err != nil {
return nil, errors.Wrap(err, "failed to scan")
}
cols, err := PgLoadColumnDef(db, schema, t.Name)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("failed to get columns of %s", t.Name))
}
t.Columns = cols
tbs = append(tbs, t)
}
return tbs, nil
}
func contains(v string, l []string) bool {
sort.Strings(l)
i := sort.SearchStrings(l, v)
if i < len(l) && l[i] == v {
return true
}
return false
}
// PgConvertType converts type
func PgConvertType(t *PgTable, col *PgColumn, typeCfg PgTypeMapConfig) string {
v := typeCfg.Match(t.Name, col.Name, col.DataType)
if v == nil {
return typeCfg["default"].NotNullGoType
}
if col.NotNull {
return v.NotNullGoType
}
return v.NullableGoType
}
// PgColToField converts pg column to go struct field
func PgColToField(db Queryer, t *PgTable, col *PgColumn, typeCfg PgTypeMapConfig) (*StructField, error) {
stfType := PgConvertType(t, col, typeCfg)
stf := &StructField{
Name: PublicVarName(col.Name, false),
Type: stfType,
Column: col,
}
return fillStructTags(db, stf, t, col, typeCfg)
}
func fillStructTags(db Queryer, st *StructField, t *PgTable, col *PgColumn, typeCfg PgTypeMapConfig) (*StructField, error) {
var tags []string
if col.IsPrimaryKey {
tags = append(tags, `pk:"true"`)
}
unique, err := isUnique(db, st, t, col)
if err != nil {
return nil, err
}
if unique {
tags = append(tags, `unique:"true"`)
}
fk, err := hasForeignKey(db, st, t, col)
if err != nil {
return nil, err
}
if fk != "" {
tags = append(tags, fmt.Sprintf(`fk:"%s"`, fk))
col.IsForeignKey = true
col.ForeignKey = fk
}
var omitEmpty bool
if strings.Contains(col.DefaultValue.String, "nextval") {
tags = append(tags, `auto:"true"`)
omitEmpty = true
} else {
if col.DefaultValue.Valid {
parts := strings.Split(col.DefaultValue.String, "::")
defaultVal := parts[0]
if defaultVal != "" {
omitEmpty = true
tags = append(tags, fmt.Sprintf(`default:"%s"`, defaultVal))
}
}
}
if col.DataType == "json" || col.DataType == "jsonb" || col.DataType == "numeric" || col.DataType == "date" {
tags = append(tags, fmt.Sprintf(`type:"%s"`, col.DataType))
}
if !col.NotNull {
omitEmpty = true
}
var omit string
if omitEmpty {
omit = ",omitempty"
}
cfg := typeCfg.Tags(t.Name, col.Name, col.DataType)
if cfg != "" {
tags = append(tags, cfg)
}
tags = append([]string{fmt.Sprintf(`db:"%s" json:"%s%s"`, col.Name, col.Name, omit)}, tags...)
st.Tag = strings.Join(tags, " ")
return st, nil
}
func isUnique(db Queryer, st *StructField, t *PgTable, col *PgColumn) (bool, error) {
colDefs, err := db.Query(pgLoadColumnConstraint, t.Schema, t.Name, col.Name, "u")
if err != nil {
return false, errors.Wrap(err, "failed to load column constraint")
}
defer colDefs.Close()
var cont sql.NullString
if colDefs.Next() {
if err := colDefs.Scan(&cont); err != nil {
return false, err
}
}
return cont.String != "", nil
}
func hasForeignKey(db Queryer, st *StructField, t *PgTable, col *PgColumn) (string, error) {
colDefs, err := db.Query(pgLoadColumnConstraint, t.Schema, t.Name, col.Name, "f")
if err != nil {
return "", errors.Wrap(err, "failed to load column constraint")
}
defer colDefs.Close()
var cont sql.NullString
if colDefs.Next() {
if err := colDefs.Scan(&cont); err != nil {
return "", err
}
}
parts := strings.Split(cont.String, "REFERENCES ")
if len(parts) == 1 {
return "", nil
}
parts = strings.Split(parts[1], `)`)
fk := strings.ReplaceAll(parts[0], `"`, "") + ")"
return fk, nil
}
// PgTableToStruct converts table def to go struct
func PgTableToStruct(db Queryer, t *PgTable, typeCfg PgTypeMapConfig, keyConfig *AutoKeyMap) (*Struct, error) {
t.setPrimaryKeyInfo(keyConfig)
s := &Struct{
Name: PublicVarName(t.Name, true),
Table: t,
}
var fs []*StructField
for _, c := range t.Columns {
f, err := PgColToField(db, t, c, typeCfg)
if err != nil {
return nil, errors.Wrap(err, "failed to convert col to field")
}
fs = append(fs, f)
}
s.Fields = fs
return s, nil
}
// PgExecuteDefaultTmpl execute struct template with *Struct
func PgExecuteDefaultTmpl(st interface{}, path string) ([]byte, error) {
var src []byte
d, err := Asset(path)
if err != nil {
return src, errors.Wrap(err, "failed to load asset")
}
tpl, err := template.New("struct").Funcs(tmplFuncMap).Parse(string(d))
if err != nil {
return src, errors.Wrap(err, "failed to parse template")
}
buf := new(bytes.Buffer)
if err := tpl.Execute(buf, st); err != nil {
return src, errors.Wrap(err, fmt.Sprintf("failed to execute template:\n%s", src))
}
src, err = format.Source(buf.Bytes())
if err != nil {
return src, errors.Wrap(err, fmt.Sprintf("failed to format code:\n%s", src))
}
return src, nil
}
// PgExecuteCustomTmpl execute custom template
func PgExecuteCustomTmpl(st interface{}, customTmpl string) ([]byte, error) {
var src []byte
tpl, err := template.New("struct").Funcs(tmplFuncMap).Parse(customTmpl)
if err != nil {
return src, errors.Wrap(err, "failed to parse template")
}
buf := new(bytes.Buffer)
if err := tpl.Execute(buf, st); err != nil {
return src, errors.Wrap(err, fmt.Sprintf("failed to execute custom template:\n%s", src))
}
src, err = format.Source(buf.Bytes())
if err != nil {
return src, errors.Wrap(err, fmt.Sprintf("failed to format code:\n%s", src))
}
return src, nil
}
func getPgTypeMapConfig(typeMapPath string) (PgTypeMapConfig, error) {
cfg := make(PgTypeMapConfig)
if typeMapPath == "" {
if _, err := toml.Decode(typeMap, &cfg); err != nil {
return nil, errors.Wrap(err, "failed to read type map")
}
} else {
if _, err := toml.DecodeFile(typeMapPath, &cfg); err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("failed to decode type map file %s", typeMapPath))
}
}
return cfg, nil
}
func PgEnumToType(e *PgEnum, typeCfg PgTypeMapConfig, keyConfig *AutoKeyMap) (*EnumType, error) {
en := &EnumType{
Name: PublicVarName(e.Name, true),
Enum: e,
}
for _, v := range e.Values {
en.Values = append(en.Values, EnumValue{
Type: en,
Name: PublicVarName(v, true) + en.Name,
Value: v,
})
}
if _, ok := typeCfg[e.Name]; !ok {
typeCfg[e.Name] = &TypeMap{
DBTypes: []string{e.Name},
NotNullGoType: en.Name,
NullableGoType: "*" + en.Name,
compiled: true,
rePatterns: nil,
}
}
return en, nil
}
func PgCreateEnums(db Queryer, schema string, cfg PgTypeMapConfig, customTmpl string) (map[string][]byte, error) {
out := make(map[string][]byte, 0)
enums, err := PgLoadEnumDef(db, schema)
if err != nil {
return nil, errors.Wrap(err, "failed to load enum definitions")
}
for _, pgEnum := range enums {
var src []byte
enum, err := PgEnumToType(pgEnum, cfg, autoGenKeyCfg)
if err != nil {
return nil, errors.Wrap(err, "failed to convert enum definition to type")
}
if customTmpl != "" {
tmpl, err := ioutil.ReadFile(customTmpl)
if err != nil {
return nil, err
}
s, err := PgExecuteCustomTmpl(enum, string(tmpl))
if err != nil {
return nil, errors.Wrap(err, "PgExecuteCustomTmpl failed")
}
src = append(src, s...)
} else {
s, err := PgExecuteDefaultTmpl(enum, "template/enum.tmpl")
if err != nil {
return nil, errors.Wrap(err, "failed to execute template")
}
src = append(src, s...)
}
out[pgEnum.Name] = src
}
return out, nil
}
// PgCreateStruct creates struct from given schema
func PgCreateStruct(
db Queryer, schemaSQL string, cfg PgTypeMapConfig, pkgName, customTmpl, customEnumTmpl, schemaTmpl string, exTbls []string) (map[string][]byte, error) {
enums, err := PgCreateEnums(db, schemaSQL, cfg, customEnumTmpl)
if err != nil {
log.Fatal(err)
}
out := make(map[string][]byte, 0)
tbls, err := PgLoadTableDef(db, schemaSQL)
if err != nil {
return nil, errors.Wrap(err, "failed to load table definitions")
}
schemaTmplSrc, err := ioutil.ReadFile(schemaTmpl)
if err != nil {
return nil, err
}
sch := &SchemaTmpl{Package: pkgName}
for _, tbl := range tbls {
src := []byte(fmt.Sprintf("package %s\n\n", pkgName))
if contains(tbl.Name, exTbls) {
continue
}
st, err := PgTableToStruct(db, tbl, cfg, autoGenKeyCfg)
if err != nil {
return nil, errors.Wrap(err, "failed to convert table definition to struct")
}
sch.Structs = append(sch.Structs, st)
if customTmpl != "" {
tmpl, err := ioutil.ReadFile(customTmpl)
if err != nil {
return nil, err
}
s, err := PgExecuteCustomTmpl(&StructTmpl{Struct: st}, string(tmpl))
if err != nil {
return nil, errors.Wrap(err, "PgExecuteCustomTmpl failed")
}
src = append(src, s...)
} else {
s, err := PgExecuteDefaultTmpl(&StructTmpl{Struct: st}, "template/struct.tmpl")
if err != nil {
return nil, errors.Wrap(err, "failed to execute template")
}
m, err := PgExecuteDefaultTmpl(&StructTmpl{Struct: st}, "template/method.tmpl")
if err != nil {
return nil, errors.Wrap(err, "failed to execute template")
}
src = append(src, s...)
src = append(src, m...)
}
filename := fmt.Sprintf("%s.go", strcase.ToSnake(st.Name))
out[filename] = src
for _, col := range tbl.Columns {
// To generate the enums for types that use it in an array we need to remove
// the suffix from the array type definition.
dataType := strings.TrimRight(col.DataType, "[]")
if enum, ok := enums[dataType]; ok {
out[filename] = append(out[filename], enum...)
delete(enums, dataType)
}
}
}
s, err := PgExecuteCustomTmpl(sch, string(schemaTmplSrc))
if err != nil {
return nil, errors.Wrap(err, "PgExecuteCustomTmpl failed")
}
out["schema.go"] = s
return out, nil
}
var commonInitialisms = map[string]bool{
"API": true,
"ASCII": true,
"CPU": true,
"CSS": true,
"DNS": true,
"EOF": true,
"GUID": true,
"HTML": true,
"HTTP": true,
"HTTPS": true,
"ID": true,
"IP": true,
"JSON": true,
"LHS": true,
"QPS": true,
"RAM": true,
"RHS": true,
"RPC": true,
"SLA": true,
"SMTP": true,
"SSH": true,
"TLS": true,
"TTL": true,
"UI": true,
"UID": true,
"UUID": true,
"URI": true,
"URL": true,
"UTF8": true,
"VM": true,
"XML": true,
"PTO": true,
"UTM": true,
"QA": true,
"ML": true,
}
// PublicVarName formats a string as a public go variable name
func PublicVarName(s string, isTable bool) string {
name := lintFieldName(s)
runes := []rune(name)
for i, c := range runes {
ok := unicode.IsLetter(c) || unicode.IsDigit(c)
if i == 0 {
ok = unicode.IsLetter(c)
}
if !ok {
runes[i] = '_'
}
}
newName := string(runes)
if strings.Index(newName, "BackAc") != -1 {
newName = strings.Replace(newName, "BackAc", "BankAc", 1)
}
if strings.Contains(newName, "Bonuses") {
return strings.Replace(newName, "Bonuses", "Bonus", 1)
}
if !isTable {
return newName
}
if strings.Index(newName, "Policies") != -1 {
newName = strings.Replace(newName, "Policies", "Policy", 1)
}
if strings.HasSuffix(newName, "People") {
newName = strings.TrimSuffix(newName, "People") + "Person"
}
if strings.Index(newName, "Pto") != -1 {
newName = strings.Replace(newName, "Pto", "PTO", 1)
}
if strings.Index(newName, "Utm") != -1 {
newName = strings.Replace(newName, "Utm", "UTM", 1)
}
if strings.HasSuffix(newName, "ies") {
newName = strings.TrimSuffix(newName, "ies") + "y"
}
if strings.Index(newName, "Kids") == -1 &&
strings.Index(newName, "Days") == -1 &&
strings.Index(newName, "Comments") == -1 &&
strings.Index(newName, "atus") == -1 &&
strings.Index(newName, "ress") == -1 &&
strings.Index(newName, "Bonus") == -1 &&
name[len(newName)-1] == 's' {
newName = newName[:len(newName)-1]
}
return newName
}
func lintFieldName(name string) string {
// Fast path for simple cases: "_" and all lowercase.
if name == "_" {
return name
}
for len(name) > 0 && name[0] == '_' {
name = name[1:]
}
allLower := true
for _, r := range name {
if !unicode.IsLower(r) {
allLower = false
break
}
}
if allLower {
runes := []rune(name)
if u := strings.ToUpper(name); commonInitialisms[u] {
copy(runes[0:], []rune(u))
} else {
runes[0] = unicode.ToUpper(runes[0])
}
return string(runes)
}
// Split camelCase at any lower->upper transition, and split on underscores.
// Check each word for common initialisms.
runes := []rune(name)
w, i := 0, 0 // index of start of word, scan
for i+1 <= len(runes) {
eow := false // whether we hit the end of a word
if i+1 == len(runes) {
eow = true
} else if runes[i+1] == '_' {
// underscore; shift the remainder forward over any run of underscores
eow = true
n := 1
for i+n+1 < len(runes) && runes[i+n+1] == '_' {
n++
}
// Leave at most one underscore if the underscore is between two digits
if i+n+1 < len(runes) && unicode.IsDigit(runes[i]) && unicode.IsDigit(runes[i+n+1]) {
n--
}
copy(runes[i+1:], runes[i+n+1:])
runes = runes[:len(runes)-n]
} else if unicode.IsLower(runes[i]) && !unicode.IsLower(runes[i+1]) {
// lower->non-lower
eow = true
}
i++
if !eow {
continue
}
// [w,i) is a word.
word := string(runes[w:i])
if u := strings.ToUpper(word); commonInitialisms[u] {
// All the common initialisms are ASCII,
// so we can replace the bytes exactly.
copy(runes[w:], []rune(u))
} else if strings.ToLower(word) == word {
// already all lowercase, and not the first word, so uppercase the first character.
runes[w] = unicode.ToUpper(runes[w])
}
w = i
}
return string(runes)
}