1
0
mirror of https://github.com/go-task/task.git synced 2025-03-19 21:17:46 +02:00

78 lines
1.7 KiB
Go
Raw Normal View History

package ast
2023-06-15 15:04:03 +00:00
import (
"gopkg.in/yaml.v3"
"github.com/go-task/task/v3/errors"
2023-06-15 15:04:03 +00:00
"github.com/go-task/task/v3/internal/deepcopy"
"github.com/go-task/task/v3/internal/omap"
2023-06-15 15:04:03 +00:00
)
type For struct {
2024-09-02 20:29:00 +01:00
From string
List []any
Matrix omap.OrderedMap[string, []any]
2024-09-02 20:29:00 +01:00
Var string
Split string
As string
2023-06-15 15:04:03 +00:00
}
func (f *For) UnmarshalYAML(node *yaml.Node) error {
switch node.Kind {
case yaml.ScalarNode:
var from string
if err := node.Decode(&from); err != nil {
return errors.NewTaskfileDecodeError(err, node)
2023-06-15 15:04:03 +00:00
}
f.From = from
return nil
case yaml.SequenceNode:
var list []any
2023-06-15 15:04:03 +00:00
if err := node.Decode(&list); err != nil {
return errors.NewTaskfileDecodeError(err, node)
2023-06-15 15:04:03 +00:00
}
f.List = list
return nil
case yaml.MappingNode:
var forStruct struct {
Matrix omap.OrderedMap[string, []any]
2024-09-02 20:29:00 +01:00
Var string
Split string
As string
2023-06-15 15:04:03 +00:00
}
if err := node.Decode(&forStruct); err != nil {
return errors.NewTaskfileDecodeError(err, node)
2023-06-15 15:04:03 +00:00
}
if forStruct.Var == "" && forStruct.Matrix.Len() == 0 {
return errors.NewTaskfileDecodeError(nil, node).WithMessage("invalid keys in for")
}
if forStruct.Var != "" && forStruct.Matrix.Len() != 0 {
2024-09-02 20:29:00 +01:00
return errors.NewTaskfileDecodeError(nil, node).WithMessage("cannot use both var and matrix in for")
}
f.Matrix = forStruct.Matrix
f.Var = forStruct.Var
f.Split = forStruct.Split
f.As = forStruct.As
return nil
2023-06-15 15:04:03 +00:00
}
return errors.NewTaskfileDecodeError(nil, node).WithTypeMessage("for")
2023-06-15 15:04:03 +00:00
}
func (f *For) DeepCopy() *For {
if f == nil {
return nil
}
return &For{
2024-09-02 20:29:00 +01:00
From: f.From,
List: deepcopy.Slice(f.List),
Matrix: f.Matrix.DeepCopy(),
2024-09-02 20:29:00 +01:00
Var: f.Var,
Split: f.Split,
As: f.As,
2023-06-15 15:04:03 +00:00
}
}