snapshot of https://github.com/sqlc-dev/sqlc.git (main, 2026-09-16; blobs > 131072B omitted)
Some checks failed
kotlin / test (push) Has been skipped
python / test (push) Has been skipped
typescript / test (push) Has been skipped
go / build darwin/amd64 (push) Has been cancelled
go / build linux/amd64 (push) Has been cancelled
go / build windows/amd64 (push) Has been cancelled
go / build darwin/arm64 (push) Has been cancelled
go / build linux/arm64 (push) Has been cancelled
go / build windows/arm64 (push) Has been cancelled
go / test (push) Has been cancelled
go / vuln_check (push) Has been cancelled
Some checks failed
kotlin / test (push) Has been skipped
python / test (push) Has been skipped
typescript / test (push) Has been skipped
go / build darwin/amd64 (push) Has been cancelled
go / build linux/amd64 (push) Has been cancelled
go / build windows/amd64 (push) Has been cancelled
go / build darwin/arm64 (push) Has been cancelled
go / build linux/arm64 (push) Has been cancelled
go / build windows/arm64 (push) Has been cancelled
go / test (push) Has been cancelled
go / vuln_check (push) Has been cancelled
This commit is contained in:
105
internal/opts/experiment.go
Normal file
105
internal/opts/experiment.go
Normal file
@@ -0,0 +1,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:
|
||||
//
|
||||
// coreanalyzer - routes generate through the core catalog and analyzer
|
||||
//
|
||||
// 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 {
|
||||
// CoreAnalyzer routes generate through the core catalog and analyzer
|
||||
// instead of each engine's own analysis path.
|
||||
CoreAnalyzer 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 "coreanalyzer":
|
||||
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 "coreanalyzer":
|
||||
e.CoreAnalyzer = enabled
|
||||
}
|
||||
}
|
||||
|
||||
// Enabled returns a slice of all enabled experiment names.
|
||||
func (e Experiment) Enabled() []string {
|
||||
var enabled []string
|
||||
if e.CoreAnalyzer {
|
||||
enabled = append(enabled, "coreanalyzer")
|
||||
}
|
||||
return enabled
|
||||
}
|
||||
|
||||
// String returns a comma-separated list of enabled experiments.
|
||||
func (e Experiment) String() string {
|
||||
return strings.Join(e.Enabled(), ",")
|
||||
}
|
||||
155
internal/opts/experiment_test.go
Normal file
155
internal/opts/experiment_test.go
Normal file
@@ -0,0 +1,155 @@
|
||||
package opts
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestExperimentFromString(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want Experiment
|
||||
}{
|
||||
{
|
||||
name: "empty string",
|
||||
input: "",
|
||||
want: Experiment{},
|
||||
},
|
||||
{
|
||||
name: "whitespace only",
|
||||
input: " ",
|
||||
want: Experiment{},
|
||||
},
|
||||
{
|
||||
name: "unknown experiment",
|
||||
input: "unknownexperiment",
|
||||
want: Experiment{},
|
||||
},
|
||||
{
|
||||
name: "multiple unknown experiments",
|
||||
input: "foo,bar,baz",
|
||||
want: Experiment{},
|
||||
},
|
||||
{
|
||||
name: "unknown with no prefix",
|
||||
input: "nounknown",
|
||||
want: Experiment{},
|
||||
},
|
||||
{
|
||||
name: "whitespace around experiments",
|
||||
input: " foo , bar , baz ",
|
||||
want: Experiment{},
|
||||
},
|
||||
{
|
||||
name: "empty items in list",
|
||||
input: "foo,,bar",
|
||||
want: Experiment{},
|
||||
},
|
||||
{
|
||||
name: "enable coreanalyzer",
|
||||
input: "coreanalyzer",
|
||||
want: Experiment{CoreAnalyzer: true},
|
||||
},
|
||||
{
|
||||
name: "disable coreanalyzer",
|
||||
input: "nocoreanalyzer",
|
||||
want: Experiment{CoreAnalyzer: false},
|
||||
},
|
||||
{
|
||||
name: "enable then disable coreanalyzer",
|
||||
input: "coreanalyzer,nocoreanalyzer",
|
||||
want: Experiment{CoreAnalyzer: false},
|
||||
},
|
||||
{
|
||||
name: "coreanalyzer case insensitive",
|
||||
input: "CoreAnalyzer",
|
||||
want: Experiment{CoreAnalyzer: true},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := ExperimentFromString(tt.input)
|
||||
if got != tt.want {
|
||||
t.Errorf("ExperimentFromString(%q) = %+v, want %+v", tt.input, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExperimentEnabled(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
exp Experiment
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "no experiments enabled",
|
||||
exp: Experiment{},
|
||||
want: nil,
|
||||
},
|
||||
{
|
||||
name: "coreanalyzer enabled",
|
||||
exp: Experiment{CoreAnalyzer: true},
|
||||
want: []string{"coreanalyzer"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := tt.exp.Enabled()
|
||||
if len(got) != len(tt.want) {
|
||||
t.Errorf("Experiment.Enabled() = %v, want %v", got, tt.want)
|
||||
return
|
||||
}
|
||||
for i := range got {
|
||||
if got[i] != tt.want[i] {
|
||||
t.Errorf("Experiment.Enabled()[%d] = %q, want %q", i, got[i], tt.want[i])
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExperimentString(t *testing.T) {
|
||||
exp := Experiment{}
|
||||
if got := exp.String(); got != "" {
|
||||
t.Errorf("Experiment.String() = %q, want %q", got, "")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsKnownExperiment(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "unknown experiment",
|
||||
input: "unknown",
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "empty string",
|
||||
input: "",
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "coreanalyzer lowercase",
|
||||
input: "coreanalyzer",
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "coreanalyzer mixed case",
|
||||
input: "CoreAnalyzer",
|
||||
want: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := isKnownExperiment(tt.input)
|
||||
if got != tt.want {
|
||||
t.Errorf("isKnownExperiment(%q) = %v, want %v", tt.input, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
5
internal/opts/parser.go
Normal file
5
internal/opts/parser.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package opts
|
||||
|
||||
type Parser struct {
|
||||
Experiment Experiment
|
||||
}
|
||||
Reference in New Issue
Block a user