forked from volatiletech/sqlboiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
178 lines (148 loc) · 5.1 KB
/
config.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
package boilingcore
import (
"github.com/volatiletech/sqlboiler/drivers"
"github.com/volatiletech/sqlboiler/importers"
)
// Config for the running of the commands
type Config struct {
DriverName string `toml:"driver_name,omitempty" json:"driver_name,omitempty"`
DriverConfig drivers.Config `toml:"driver_config,omitempty" json:"driver_config,omitempty"`
PkgName string `toml:"pkg_name,omitempty" json:"pkg_name,omitempty"`
OutFolder string `toml:"out_folder,omitempty" json:"out_folder,omitempty"`
TemplateDirs []string `toml:"template_dirs,omitempty" json:"template_dirs,omitempty"`
Tags []string `toml:"tags,omitempty" json:"tags,omitempty"`
Replacements []string `toml:"replacements,omitempty" json:"replacements,omitempty"`
Debug bool `toml:"debug,omitempty" json:"debug,omitempty"`
AddGlobal bool `toml:"add_global,omitempty" json:"add_global,omitempty"`
AddPanic bool `toml:"add_panic,omitempty" json:"add_panic,omitempty"`
NoContext bool `toml:"no_context,omitempty" json:"no_context,omitempty"`
NoTests bool `toml:"no_tests,omitempty" json:"no_tests,omitempty"`
NoHooks bool `toml:"no_hooks,omitempty" json:"no_hooks,omitempty"`
NoAutoTimestamps bool `toml:"no_auto_timestamps,omitempty" json:"no_auto_timestamps,omitempty"`
NoRowsAffected bool `toml:"no_rows_affected,omitempty" json:"no_rows_affected,omitempty"`
Wipe bool `toml:"wipe,omitempty" json:"wipe,omitempty"`
StructTagCasing string `toml:"struct_tag_casing,omitempty" json:"struct_tag_casing,omitempty"`
Imports importers.Collection `toml:"imports,omitempty" json:"imports,omitempty"`
Aliases Aliases `toml:"aliases,omitempty" json:"aliases,omitempty"`
TypeReplaces []TypeReplace `toml:"type_replaces,omitempty" json:"type_replaces,omitempty"`
}
// TypeReplace replaces a column type with something else
type TypeReplace struct {
Match drivers.Column `toml:"match,omitempty" json:"match,omitempty"`
Replace drivers.Column `toml:"replace,omitempty" json:"replace,omitempty"`
Imports importers.Set `toml:"imports,omitempty" json:"imports,omitempty"`
}
// ConvertAliases is necessary because viper
func ConvertAliases(i interface{}) (a Aliases) {
if i == nil {
return a
}
topLevel := i.(map[string]interface{})
var tables map[string]interface{}
tablesIntf, ok := topLevel["tables"]
if ok {
tables = tablesIntf.(map[string]interface{})
}
for name, tIntf := range tables {
if a.Tables == nil {
a.Tables = make(map[string]TableAlias)
}
t := tIntf.(map[string]interface{})
var ta TableAlias
if s := t["up_plural"]; s != nil {
ta.UpPlural = s.(string)
}
if s := t["up_singular"]; s != nil {
ta.UpSingular = s.(string)
}
if s := t["down_plural"]; s != nil {
ta.DownPlural = s.(string)
}
if s := t["down_singular"]; s != nil {
ta.DownSingular = s.(string)
}
if colsIntf, ok := t["columns"]; ok {
cols := colsIntf.(map[string]interface{})
ta.Columns = make(map[string]string)
for k, v := range cols {
ta.Columns[k] = v.(string)
}
}
var relationships map[string]interface{}
relationshipsIntf, ok := t["relationships"]
if ok {
relationships = relationshipsIntf.(map[string]interface{})
for name, rIntf := range relationships {
if ta.Relationships == nil {
ta.Relationships = make(map[string]RelationshipAlias)
}
var ra RelationshipAlias
rel := rIntf.(map[string]interface{})
if s := rel["local"]; s != nil {
ra.Local = s.(string)
}
if s := rel["foreign"]; s != nil {
ra.Foreign = s.(string)
}
ta.Relationships[name] = ra
}
}
a.Tables[name] = ta
}
return a
}
// ConvertTypeReplace is necessary because viper
func ConvertTypeReplace(i interface{}) []TypeReplace {
if i == nil {
return nil
}
intfArray := i.([]interface{})
var replaces []TypeReplace
for _, r := range intfArray {
replaceIntf := r.(map[string]interface{})
replace := TypeReplace{}
if replaceIntf["match"] == nil || replaceIntf["replace"] == nil {
panic("replace types must specify both match and replace")
}
replace.Match = columnFromInterface(replaceIntf["match"])
replace.Replace = columnFromInterface(replaceIntf["replace"])
if imps := replaceIntf["imports"]; imps != nil {
var err error
replace.Imports, err = importers.SetFromInterface(imps)
if err != nil {
panic(err)
}
}
replaces = append(replaces, replace)
}
return replaces
}
func columnFromInterface(i interface{}) (col drivers.Column) {
m := i.(map[string]interface{})
if s := m["name"]; s != nil {
col.Name = s.(string)
}
if s := m["type"]; s != nil {
col.Type = s.(string)
}
if s := m["db_type"]; s != nil {
col.DBType = s.(string)
}
if s := m["udt_name"]; s != nil {
col.UDTName = s.(string)
}
if s := m["full_db_type"]; s != nil {
col.FullDBType = s.(string)
}
if s := m["arr_type"]; s != nil {
col.ArrType = new(string)
*col.ArrType = s.(string)
}
if b := m["auto_generated"]; b != nil {
col.AutoGenerated = b.(bool)
}
if b := m["nullable"]; b != nil {
col.Nullable = b.(bool)
}
return col
}