-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathv_two.go
More file actions
112 lines (108 loc) · 2.66 KB
/
v_two.go
File metadata and controls
112 lines (108 loc) · 2.66 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
package config
import (
"fmt"
"io"
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
}
// TODO: Store built-in plugins somewhere else
builtins := map[string]struct{}{
"go": {},
"json": {},
}
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 r := conf.Plugins[i].Process; r != nil {
switch {
case r.Cmd != "" && r.GoPkg != "":
return conf, ErrPluginProcessTooManyCmd
case r.Cmd == "" && r.GoPkg == "":
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.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
}
// TODO: Allow the use of built-in codegen from here
if _, ok := plugins[cg.Plugin]; !ok {
return conf, ErrPluginNotFound
}
}
if conf.SQL[j].StrictOrderBy == nil {
defaultValidate := true
conf.SQL[j].StrictOrderBy = &defaultValidate
}
}
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.Overrides.Go == nil {
return nil
}
usesMultipleEngines := len(engines) > 1
for _, oride := range c.Overrides.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
}