-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathexperiment.go
More file actions
105 lines (92 loc) · 2.89 KB
/
experiment.go
File metadata and controls
105 lines (92 loc) · 2.89 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
package opts
import (
"os"
"strings"
)
// The SQLCEXPERIMENT variable controls experimental features within sqlc. It
// is a comma-separated list of experiment names. Experiment names can be
// prefixed with "no" to explicitly disable them.
//
// This is modeled after Go's GOEXPERIMENT environment variable. For more
// information, see https://pkg.go.dev/internal/goexperiment
//
// Available experiments:
//
// analyzerv2 - enables database-only analyzer mode
//
// Example usage:
//
// SQLCEXPERIMENT=foo,bar # enable foo and bar experiments
// SQLCEXPERIMENT=nofoo # explicitly disable foo experiment
// SQLCEXPERIMENT=foo,nobar # enable foo, disable bar
// Experiment holds the state of all experimental features.
// Add new experiments as boolean fields to this struct.
type Experiment struct {
// AnalyzerV2 enables the database-only analyzer mode (analyzer.database: only)
// which uses the database for all type resolution instead of parsing schema files.
AnalyzerV2 bool
}
// ExperimentFromEnv returns an Experiment initialized from the SQLCEXPERIMENT
// environment variable.
func ExperimentFromEnv() Experiment {
return ExperimentFromString(os.Getenv("SQLCEXPERIMENT"))
}
// ExperimentFromString parses a comma-separated list of experiment names
// and returns an Experiment with the appropriate flags set.
//
// Experiment names can be prefixed with "no" to explicitly disable them.
// Unknown experiment names are silently ignored.
func ExperimentFromString(val string) Experiment {
e := Experiment{}
if val == "" {
return e
}
for name := range strings.SplitSeq(val, ",") {
name = strings.TrimSpace(name)
if name == "" {
continue
}
// Check if this is a negation (noFoo)
enabled := true
if strings.HasPrefix(strings.ToLower(name), "no") && len(name) > 2 {
// Could be a negation, check if the rest is a valid experiment
possibleExp := name[2:]
if isKnownExperiment(possibleExp) {
name = possibleExp
enabled = false
}
// If not a known experiment, treat "no..." as a potential experiment name itself
}
setExperiment(&e, name, enabled)
}
return e
}
// isKnownExperiment returns true if the given name (case-insensitive) is a
// known experiment.
func isKnownExperiment(name string) bool {
switch strings.ToLower(name) {
case "analyzerv2":
return true
default:
return false
}
}
// setExperiment sets the experiment flag with the given name to the given value.
func setExperiment(e *Experiment, name string, enabled bool) {
switch strings.ToLower(name) {
case "analyzerv2":
e.AnalyzerV2 = enabled
}
}
// Enabled returns a slice of all enabled experiment names.
func (e Experiment) Enabled() []string {
var enabled []string
if e.AnalyzerV2 {
enabled = append(enabled, "analyzerv2")
}
return enabled
}
// String returns a comma-separated list of enabled experiments.
func (e Experiment) String() string {
return strings.Join(e.Enabled(), ",")
}