Repository navigation
Expand file tree
/
Copy pathoptions.go
More file actions
82 lines (77 loc) · 2.58 KB
/
Copy pathoptions.go
File metadata and controls
82 lines (77 loc) · 2.58 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
package wicked
import (
"encoding/json"
"fmt"
"strings"
"github.com/sqlc-dev/sqlc/internal/codegen/golang/opts"
"github.com/sqlc-dev/sqlc/internal/plugin"
)
var wickedReservedNames = map[string]bool{"load": true, "dump": true, "check": true}
// Reuse upstream's override and rename parsing without teaching the standard Go
// generator about wpgx. Only this private copy is normalized to its pgx driver.
func parseOptions(req *plugin.GenerateRequest) (*opts.Options, error) {
var raw map[string]json.RawMessage
if err := json.Unmarshal(req.PluginOptions, &raw); err != nil {
return nil, fmt.Errorf("wicked options: %w", err)
}
if raw == nil {
return nil, fmt.Errorf("wicked: missing Go options")
}
raw["sql_package"] = json.RawMessage(`"pgx/v5"`)
blob, err := json.Marshal(raw)
if err != nil {
return nil, err
}
copy := &plugin.GenerateRequest{Settings: req.Settings, Catalog: req.Catalog, PluginOptions: blob, GlobalOptions: req.GlobalOptions}
options, err := opts.Parse(copy)
if err != nil {
return nil, err
}
// Legacy wicked configurations use local rename entries over global ones.
// Upstream opts.Parse currently applies them in the opposite order.
if rename, present := raw["rename"]; present {
var local map[string]string
if err := json.Unmarshal(rename, &local); err != nil {
return nil, err
}
if options.Rename == nil {
options.Rename = make(map[string]string)
}
for key, value := range local {
options.Rename[key] = value
}
}
return options, nil
}
func parseQueryOptions(comments []string, isSelect bool, names map[string]bool) (WPgxOption, error) {
values := make(map[string]string)
for _, comment := range comments {
body := strings.TrimSpace(comment)
if !strings.HasPrefix(body, "--") {
continue
}
key, value, ok := strings.Cut(body[2:], ":")
if !ok {
return WPgxOption{}, fmt.Errorf("invalid query option: %s", comment)
}
values[strings.TrimSpace(key)] = strings.TrimSpace(value)
}
for _, key := range []string{WpgxOptionKeyCountIntent, WpgxOptionKeyAllowReplica} {
value, present := values[key]
if !present {
if isSelect {
values[key] = "true"
} else {
values[key] = "false"
}
} else if value != "true" && value != "false" {
return WPgxOption{}, fmt.Errorf("invalid %s value: %s", key, value)
} else if !isSelect && value == "true" {
return WPgxOption{}, fmt.Errorf("%s requires a SELECT statement", key)
}
}
if _, present := values[WPgxOptionKeyInvalidate]; isSelect && present {
return WPgxOption{}, fmt.Errorf("invalidate cannot be used on a SELECT statement")
}
return parseOption(values, names)
}