2025-02-08 23:02:51 +00:00
|
|
|
package experiments
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"slices"
|
2025-02-23 10:51:59 +01:00
|
|
|
"strconv"
|
2025-04-19 12:20:33 +01:00
|
|
|
|
|
|
|
"github.com/go-task/task/v3/taskrc/ast"
|
2025-02-08 23:02:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Experiment struct {
|
2025-02-23 10:51:59 +01:00
|
|
|
Name string // The name of the experiment.
|
|
|
|
AllowedValues []int // The values that can enable this experiment.
|
|
|
|
Value int // The version of the experiment that is enabled.
|
2025-02-08 23:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new experiment with the given name and sets the values that can
|
|
|
|
// enable it.
|
2025-04-19 12:20:33 +01:00
|
|
|
func New(xName string, config *ast.TaskRC, allowedValues ...int) Experiment {
|
|
|
|
var value int
|
|
|
|
if config != nil {
|
|
|
|
value = config.Experiments[xName]
|
|
|
|
}
|
2025-02-23 10:51:59 +01:00
|
|
|
|
|
|
|
if value == 0 {
|
|
|
|
value, _ = strconv.Atoi(getEnv(xName))
|
|
|
|
}
|
|
|
|
|
2025-02-08 23:02:51 +00:00
|
|
|
x := Experiment{
|
|
|
|
Name: xName,
|
|
|
|
AllowedValues: allowedValues,
|
|
|
|
Value: value,
|
|
|
|
}
|
|
|
|
xList = append(xList, x)
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
2025-02-23 10:51:59 +01:00
|
|
|
func (x Experiment) Enabled() bool {
|
2025-02-08 23:02:51 +00:00
|
|
|
return slices.Contains(x.AllowedValues, x.Value)
|
|
|
|
}
|
|
|
|
|
2025-02-23 10:51:59 +01:00
|
|
|
func (x Experiment) Active() bool {
|
2025-02-08 23:02:51 +00:00
|
|
|
return len(x.AllowedValues) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x Experiment) Valid() error {
|
2025-02-23 10:51:59 +01:00
|
|
|
if !x.Active() && x.Value != 0 {
|
2025-02-08 23:02:51 +00:00
|
|
|
return &InactiveError{
|
|
|
|
Name: x.Name,
|
|
|
|
}
|
|
|
|
}
|
2025-02-23 10:51:59 +01:00
|
|
|
if !x.Enabled() && x.Value != 0 {
|
2025-02-08 23:02:51 +00:00
|
|
|
return &InvalidValueError{
|
|
|
|
Name: x.Name,
|
|
|
|
AllowedValues: x.AllowedValues,
|
|
|
|
Value: x.Value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x Experiment) String() string {
|
|
|
|
if x.Enabled() {
|
2025-02-23 10:51:59 +01:00
|
|
|
return fmt.Sprintf("on (%d)", x.Value)
|
2025-02-08 23:02:51 +00:00
|
|
|
}
|
|
|
|
return "off"
|
|
|
|
}
|