-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathexperiment.go
More file actions
111 lines (98 loc) · 3.06 KB
/
experiment.go
File metadata and controls
111 lines (98 loc) · 3.06 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
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:
//
// (none currently defined - add experiments here as they are introduced)
//
// 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 {
// Add experimental feature flags here as they are introduced.
// Example:
// NewParser bool // Enable new SQL parser
}
// 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.Split(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) {
// Add experiment names here as they are introduced.
// Example:
// case "newparser":
// 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) {
// Add experiment cases here as they are introduced.
// Example:
// case "newparser":
// e.NewParser = enabled
}
}
// Enabled returns a slice of all enabled experiment names.
func (e Experiment) Enabled() []string {
var enabled []string
// Add enabled experiments here as they are introduced.
// Example:
// if e.NewParser {
// enabled = append(enabled, "newparser")
// }
return enabled
}
// String returns a comma-separated list of enabled experiments.
func (e Experiment) String() string {
return strings.Join(e.Enabled(), ",")
}