Skip to content

Commit

Permalink
convert to int
Browse files Browse the repository at this point in the history
  • Loading branch information
vmaerten committed Jan 5, 2025
1 parent dda6a5a commit 5bf8fa9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
19 changes: 10 additions & 9 deletions internal/experiments/experiments.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"slices"
"strconv"
"strings"

"gopkg.in/yaml.v3"
Expand All @@ -25,13 +26,13 @@ var defaultConfigFilenames = []string{
}

type experimentConfigFile struct {
Experiments map[string]string `yaml:"experiments"`
Experiments map[string]int `yaml:"experiments"`
}

type Experiment struct {
Name string
Enabled bool
Value string
Value int
}

// A list of experiments.
Expand All @@ -50,20 +51,20 @@ func init() {
experimentConfig = readConfig()
GentleForce = New("GENTLE_FORCE")
RemoteTaskfiles = New("REMOTE_TASKFILES")
AnyVariables = New("ANY_VARIABLES", "1", "2")
MapVariables = New("MAP_VARIABLES", "1", "2")
AnyVariables = New("ANY_VARIABLES", 1, 2)
MapVariables = New("MAP_VARIABLES", 1, 2)
EnvPrecedence = New("ENV_PRECEDENCE")
}

func New(xName string, enabledValues ...string) Experiment {
func New(xName string, enabledValues ...int) Experiment {
if len(enabledValues) == 0 {
enabledValues = []string{"1"}
enabledValues = []int{1}
}

value := experimentConfig.Experiments[xName]

if value == "" {
value = getEnv(xName)
if value == 0 {
value, _ = strconv.Atoi(getEnv(xName))
}

return Experiment{
Expand All @@ -75,7 +76,7 @@ func New(xName string, enabledValues ...string) Experiment {

func (x Experiment) String() string {
if x.Enabled {
return fmt.Sprintf("on (%s)", x.Value)
return fmt.Sprintf("on (%d)", x.Value)
}
return "off"
}
Expand Down
6 changes: 3 additions & 3 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,7 @@ func TestIncludesMultiLevel(t *testing.T) {
}

func TestIncludesRemote(t *testing.T) {
enableExperimentForTest(t, &experiments.RemoteTaskfiles, "1")
enableExperimentForTest(t, &experiments.RemoteTaskfiles, 1)

dir := "testdata/includes_remote"

Expand Down Expand Up @@ -1338,7 +1338,7 @@ func TestIncludesEmptyMain(t *testing.T) {
}

func TestIncludesHttp(t *testing.T) {
enableExperimentForTest(t, &experiments.RemoteTaskfiles, "1")
enableExperimentForTest(t, &experiments.RemoteTaskfiles, 1)

dir, err := filepath.Abs("testdata/includes_http")
require.NoError(t, err)
Expand Down Expand Up @@ -3191,7 +3191,7 @@ func TestReference(t *testing.T) {
//
// Typically experiments are controlled via TASK_X_ env vars, but we cannot use those in tests
// because the experiment settings are parsed during experiments.init(), before any tests run.
func enableExperimentForTest(t *testing.T, e *experiments.Experiment, val string) {
func enableExperimentForTest(t *testing.T, e *experiments.Experiment, val int) {
t.Helper()

prev := *e
Expand Down
4 changes: 2 additions & 2 deletions taskfile/ast/var.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (v *Var) UnmarshalYAML(node *yaml.Node) error {
if experiments.MapVariables.Enabled {

// This implementation is not backwards-compatible and replaces the 'sh' key with map variables
if experiments.MapVariables.Value == "1" {
if experiments.MapVariables.Value == 1 {
var value any
if err := node.Decode(&value); err != nil {
return errors.NewTaskfileDecodeError(err, node)
Expand All @@ -199,7 +199,7 @@ func (v *Var) UnmarshalYAML(node *yaml.Node) error {
}

// This implementation IS backwards-compatible and keeps the 'sh' key and allows map variables to be added under the `map` key
if experiments.MapVariables.Value == "2" {
if experiments.MapVariables.Value == 2 {
switch node.Kind {
case yaml.MappingNode:
key := node.Content[0].Value
Expand Down

0 comments on commit 5bf8fa9

Please sign in to comment.