11package ast
22
33import (
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
2118func (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 {
0 commit comments