Skip to content

Commit c6f1b3a

Browse files
authored
feat: make map variables experiment (prop 2) generally available (#2081)
* feat: make map variables experiment (prop 2) generally available * docs: remove map variables experiment page and update usage to include map variable info
1 parent cb14a4f commit c6f1b3a

File tree

5 files changed

+51
-348
lines changed

5 files changed

+51
-348
lines changed

internal/experiments/experiments.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func init() {
4444
GentleForce = New("GENTLE_FORCE", 1)
4545
RemoteTaskfiles = New("REMOTE_TASKFILES", 1)
4646
AnyVariables = New("ANY_VARIABLES")
47-
MapVariables = New("MAP_VARIABLES", 1, 2)
47+
MapVariables = New("MAP_VARIABLES")
4848
EnvPrecedence = New("ENV_PRECEDENCE", 1)
4949
}
5050

taskfile/ast/var.go

Lines changed: 4 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package ast
22

33
import (
4-
"strings"
5-
64
"gopkg.in/yaml.v3"
75

86
"github.com/go-task/task/v3/errors"
9-
"github.com/go-task/task/v3/internal/experiments"
107
)
118

129
// Var represents either a static or dynamic variable.
@@ -19,82 +16,26 @@ type Var struct {
1916
}
2017

2118
func (v *Var) UnmarshalYAML(node *yaml.Node) error {
22-
if experiments.MapVariables.Enabled() {
23-
24-
// This implementation is not backwards-compatible and replaces the 'sh' key with map variables
25-
if experiments.MapVariables.Value == 1 {
26-
var value any
27-
if err := node.Decode(&value); err != nil {
28-
return errors.NewTaskfileDecodeError(err, node)
29-
}
30-
// If the value is a string and it starts with $, then it's a shell command
31-
if str, ok := value.(string); ok {
32-
if str, ok = strings.CutPrefix(str, "$"); ok {
33-
v.Sh = &str
34-
return nil
35-
}
36-
if str, ok = strings.CutPrefix(str, "#"); ok {
37-
v.Ref = str
38-
return nil
39-
}
40-
}
41-
v.Value = value
42-
return nil
43-
}
44-
45-
// This implementation IS backwards-compatible and keeps the 'sh' key and allows map variables to be added under the `map` key
46-
if experiments.MapVariables.Value == 2 {
47-
switch node.Kind {
48-
case yaml.MappingNode:
49-
key := node.Content[0].Value
50-
switch key {
51-
case "sh", "ref", "map":
52-
var m struct {
53-
Sh *string
54-
Ref string
55-
Map any
56-
}
57-
if err := node.Decode(&m); err != nil {
58-
return errors.NewTaskfileDecodeError(err, node)
59-
}
60-
v.Sh = m.Sh
61-
v.Ref = m.Ref
62-
v.Value = m.Map
63-
return nil
64-
default:
65-
return errors.NewTaskfileDecodeError(nil, node).WithMessage(`%q is not a valid variable type. Try "sh", "ref", "map" or using a scalar value`, key)
66-
}
67-
default:
68-
var value any
69-
if err := node.Decode(&value); err != nil {
70-
return errors.NewTaskfileDecodeError(err, node)
71-
}
72-
v.Value = value
73-
return nil
74-
}
75-
}
76-
}
77-
7819
switch node.Kind {
79-
8020
case yaml.MappingNode:
8121
key := node.Content[0].Value
8222
switch key {
83-
case "sh", "ref":
23+
case "sh", "ref", "map":
8424
var m struct {
8525
Sh *string
8626
Ref string
27+
Map any
8728
}
8829
if err := node.Decode(&m); err != nil {
8930
return errors.NewTaskfileDecodeError(err, node)
9031
}
9132
v.Sh = m.Sh
9233
v.Ref = m.Ref
34+
v.Value = m.Map
9335
return nil
9436
default:
95-
return errors.NewTaskfileDecodeError(nil, node).WithMessage("maps cannot be assigned to variables")
37+
return errors.NewTaskfileDecodeError(nil, node).WithMessage(`%q is not a valid variable type. Try "sh", "ref", "map" or using a scalar value`, key)
9638
}
97-
9839
default:
9940
var value any
10041
if err := node.Decode(&value); err != nil {

website/docs/experiments/map_variables.mdx

Lines changed: 0 additions & 245 deletions
This file was deleted.

0 commit comments

Comments
 (0)