-
Notifications
You must be signed in to change notification settings - Fork 803
/
v_one.go
142 lines (131 loc) · 4.82 KB
/
v_one.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
package config
import (
"fmt"
"io"
"path/filepath"
yaml "gopkg.in/yaml.v3"
)
type V1GenerateSettings struct {
Version string `json:"version" yaml:"version"`
Packages []v1PackageSettings `json:"packages" yaml:"packages"`
Overrides []Override `json:"overrides,omitempty" yaml:"overrides,omitempty"`
Rename map[string]string `json:"rename,omitempty" yaml:"rename,omitempty"`
}
type v1PackageSettings struct {
Name string `json:"name" yaml:"name"`
Engine Engine `json:"engine,omitempty" yaml:"engine"`
Path string `json:"path" yaml:"path"`
Schema Paths `json:"schema" yaml:"schema"`
Queries Paths `json:"queries" yaml:"queries"`
EmitInterface bool `json:"emit_interface" yaml:"emit_interface"`
EmitJSONTags bool `json:"emit_json_tags" yaml:"emit_json_tags"`
EmitDBTags bool `json:"emit_db_tags" yaml:"emit_db_tags"`
EmitPreparedQueries bool `json:"emit_prepared_queries" yaml:"emit_prepared_queries"`
EmitExactTableNames bool `json:"emit_exact_table_names,omitempty" yaml:"emit_exact_table_names"`
EmitEmptySlices bool `json:"emit_empty_slices,omitempty" yaml:"emit_empty_slices"`
EmitExportedQueries bool `json:"emit_exported_queries,omitempty" yaml:"emit_exported_queries"`
JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
SQLPackage string `json:"sql_package" yaml:"sql_package"`
Overrides []Override `json:"overrides" yaml:"overrides"`
OutputDBFileName string `json:"output_db_file_name,omitempty" yaml:"output_db_file_name"`
OutputModelsFileName string `json:"output_models_file_name,omitempty" yaml:"output_models_file_name"`
OutputQuerierFileName string `json:"output_querier_file_name,omitempty" yaml:"output_querier_file_name"`
OutputFilesSuffix string `json:"output_files_suffix,omitempty" yaml:"output_files_suffix"`
}
func v1ParseConfig(rd io.Reader) (Config, error) {
dec := yaml.NewDecoder(rd)
dec.KnownFields(true)
var settings V1GenerateSettings
var config Config
if err := dec.Decode(&settings); err != nil {
return config, err
}
if settings.Version == "" {
return config, ErrMissingVersion
}
if settings.Version != "1" {
return config, ErrUnknownVersion
}
if len(settings.Packages) == 0 {
return config, ErrNoPackages
}
if err := settings.ValidateGlobalOverrides(); err != nil {
return config, err
}
for i := range settings.Overrides {
if err := settings.Overrides[i].Parse(); err != nil {
return config, err
}
}
for j := range settings.Packages {
if settings.Packages[j].Path == "" {
return config, ErrNoPackagePath
}
for i := range settings.Packages[j].Overrides {
if err := settings.Packages[j].Overrides[i].Parse(); err != nil {
return config, err
}
}
if settings.Packages[j].Name == "" {
settings.Packages[j].Name = filepath.Base(settings.Packages[j].Path)
}
if settings.Packages[j].Engine == "" {
settings.Packages[j].Engine = EnginePostgreSQL
}
}
return settings.Translate(), nil
}
func (c *V1GenerateSettings) ValidateGlobalOverrides() error {
engines := map[Engine]struct{}{}
for _, pkg := range c.Packages {
if _, ok := engines[pkg.Engine]; !ok {
engines[pkg.Engine] = struct{}{}
}
}
usesMultipleEngines := len(engines) > 1
for _, oride := range c.Overrides {
if usesMultipleEngines && oride.Engine == "" {
return fmt.Errorf(`the "engine" field is required for global type overrides because your configuration uses multiple database engines`)
}
}
return nil
}
func (c *V1GenerateSettings) Translate() Config {
conf := Config{
Version: c.Version,
}
for _, pkg := range c.Packages {
conf.SQL = append(conf.SQL, SQL{
Engine: pkg.Engine,
Schema: pkg.Schema,
Queries: pkg.Queries,
Gen: SQLGen{
Go: &SQLGo{
EmitInterface: pkg.EmitInterface,
EmitJSONTags: pkg.EmitJSONTags,
EmitDBTags: pkg.EmitDBTags,
EmitPreparedQueries: pkg.EmitPreparedQueries,
EmitExactTableNames: pkg.EmitExactTableNames,
EmitEmptySlices: pkg.EmitEmptySlices,
EmitExportedQueries: pkg.EmitExportedQueries,
Package: pkg.Name,
Out: pkg.Path,
SQLPackage: pkg.SQLPackage,
Overrides: pkg.Overrides,
JSONTagsCaseStyle: pkg.JSONTagsCaseStyle,
OutputDBFileName: pkg.OutputDBFileName,
OutputModelsFileName: pkg.OutputModelsFileName,
OutputQuerierFileName: pkg.OutputQuerierFileName,
OutputFilesSuffix: pkg.OutputFilesSuffix,
},
},
})
}
if len(c.Overrides) > 0 || len(c.Rename) > 0 {
conf.Gen.Go = &GenGo{
Overrides: c.Overrides,
Rename: c.Rename,
}
}
return conf
}