-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathv_two.go
More file actions
163 lines (157 loc) · 4.52 KB
/
v_two.go
File metadata and controls
163 lines (157 loc) · 4.52 KB
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
package config
import (
"fmt"
"io"
"os"
"path/filepath"
yaml "gopkg.in/yaml.v3"
)
func v2ParseConfig(rd io.Reader) (Config, error) {
dec := yaml.NewDecoder(rd)
dec.KnownFields(true)
var conf Config
if err := dec.Decode(&conf); err != nil {
return conf, err
}
if conf.Version == "" {
return conf, ErrMissingVersion
}
if conf.Version != "2" {
return conf, ErrUnknownVersion
}
if len(conf.SQL) == 0 {
return conf, ErrNoPackages
}
if err := conf.validateGlobalOverrides(); err != nil {
return conf, err
}
if conf.Gen.Go != nil {
for i := range conf.Gen.Go.Overrides {
if err := conf.Gen.Go.Overrides[i].Parse(); err != nil {
return conf, err
}
}
}
// TODO: Store built-in plugins somewhere else
builtins := map[string]struct{}{
"go": {},
"json": {},
"kotlin": {},
"python": {},
}
plugins := map[string]struct{}{}
for i := range conf.Plugins {
if conf.Plugins[i].Name == "" {
return conf, ErrPluginNoName
}
if _, ok := builtins[conf.Plugins[i].Name]; ok {
return conf, ErrPluginBuiltin
}
if _, ok := plugins[conf.Plugins[i].Name]; ok {
return conf, ErrPluginExists
}
if conf.Plugins[i].Process == nil && conf.Plugins[i].WASM == nil {
return conf, ErrPluginNoType
}
if conf.Plugins[i].Process != nil && conf.Plugins[i].WASM != nil {
return conf, ErrPluginBothTypes
}
if conf.Plugins[i].Process != nil {
if conf.Plugins[i].Process.Cmd == "" {
return conf, ErrPluginProcessNoCmd
}
}
plugins[conf.Plugins[i].Name] = struct{}{}
}
for j := range conf.SQL {
if conf.SQL[j].Engine == "" {
return conf, ErrMissingEngine
}
if conf.SQL[j].Gen.Go != nil {
if conf.SQL[j].Gen.Go.Out == "" {
return conf, ErrNoPackagePath
}
if conf.SQL[j].Gen.Go.Package == "" {
conf.SQL[j].Gen.Go.Package = filepath.Base(conf.SQL[j].Gen.Go.Out)
}
for i := range conf.SQL[j].Gen.Go.Overrides {
if err := conf.SQL[j].Gen.Go.Overrides[i].Parse(); err != nil {
return conf, err
}
}
}
if conf.SQL[j].Gen.Kotlin != nil {
fmt.Fprintf(os.Stderr, "WARNING: Built-in Kotlin support is deprecated.\n")
fmt.Fprintf(os.Stderr, " It will be removed in the next version (1.17.0).\n")
fmt.Fprintf(os.Stderr, " You will need to migrate to the sqlc-gen-kotlin plugin. See the step-by-step guide here:\n")
fmt.Fprintf(os.Stderr, " https://docs.sqlc.dev/en/latest/guides/migrating-to-sqlc-gen-kotlin.html\n")
if conf.SQL[j].Gen.Kotlin.Out == "" {
return conf, ErrNoOutPath
}
if conf.SQL[j].Gen.Kotlin.Package == "" {
return conf, ErrNoPackageName
}
}
if conf.SQL[j].Gen.Python != nil {
fmt.Fprintf(os.Stderr, "WARNING: Built-in Python support is deprecated.\n")
fmt.Fprintf(os.Stderr, " It will be removed in the next version (1.17.0).\n")
fmt.Fprintf(os.Stderr, " You will need to migrate to the sqlc-gen-python plugin. See the step-by-step guide here:\n")
fmt.Fprintf(os.Stderr, " https://docs.sqlc.dev/en/latest/guides/migrating-to-sqlc-gen-python.html\n")
if conf.SQL[j].Gen.Python.QueryParameterLimit != nil {
if *conf.SQL[j].Gen.Python.QueryParameterLimit < 0 {
return conf, ErrInvalidQueryParameterLimit
}
}
if conf.SQL[j].Gen.Python.Out == "" {
return conf, ErrNoOutPath
}
if conf.SQL[j].Gen.Python.Package == "" {
return conf, ErrNoPackageName
}
if !conf.SQL[j].Gen.Python.EmitSyncQuerier && !conf.SQL[j].Gen.Python.EmitAsyncQuerier {
return conf, ErrNoQuerierType
}
for i := range conf.SQL[j].Gen.Python.Overrides {
if err := conf.SQL[j].Gen.Python.Overrides[i].Parse(); err != nil {
return conf, err
}
}
}
if conf.SQL[j].Gen.JSON != nil {
if conf.SQL[j].Gen.JSON.Out == "" {
return conf, ErrNoOutPath
}
}
for _, cg := range conf.SQL[j].Codegen {
if cg.Plugin == "" {
return conf, ErrPluginNoName
}
if cg.Out == "" {
return conf, ErrNoOutPath
}
// TOOD: Allow the use of built-in codegen from here
if _, ok := plugins[cg.Plugin]; !ok {
return conf, ErrPluginNotFound
}
}
}
return conf, nil
}
func (c *Config) validateGlobalOverrides() error {
engines := map[Engine]struct{}{}
for _, pkg := range c.SQL {
if _, ok := engines[pkg.Engine]; !ok {
engines[pkg.Engine] = struct{}{}
}
}
if c.Gen.Go == nil {
return nil
}
usesMultipleEngines := len(engines) > 1
for _, oride := range c.Gen.Go.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
}