-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
variables.go
94 lines (81 loc) · 2.69 KB
/
variables.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package pipeline
import (
"regexp"
"strconv"
"strings"
"github.com/pkg/errors"
)
var (
ErrKeypathNotFound = errors.New("keypath not found")
ErrVarsRoot = errors.New("cannot get/set the root of a pipeline.Vars")
ErrVarsSetNested = errors.New("cannot set a nested key of a pipeline.Vars")
variableRegexp = regexp.MustCompile(`\$\(\s*([a-zA-Z0-9_\.]+)\s*\)`)
)
type Vars struct {
vars map[string]interface{}
}
// NewVarsFrom creates new Vars from the given map.
// If the map is nil, a new map instance will be created.
func NewVarsFrom(m map[string]interface{}) Vars {
if m == nil {
m = make(map[string]interface{})
}
return Vars{vars: m}
}
// Get returns the value for the given keypath or error.
// The keypath can consist of one or more parts, e.g. "foo" or "foo.6.a.b".
// Every part except for the first one can be an index of a slice.
func (vars Vars) Get(keypathStr string) (interface{}, error) {
keypathStr = strings.TrimSpace(keypathStr)
keypath, err := NewKeypathFromString(keypathStr)
if err != nil {
return nil, err
}
if len(keypath.Parts) == 0 {
return nil, ErrVarsRoot
}
var exists bool
var currVal interface{} = vars.vars
for i, part := range keypath.Parts {
switch v := currVal.(type) {
case map[string]interface{}:
currVal, exists = v[part]
if !exists {
return nil, errors.Wrapf(ErrKeypathNotFound, "key %v (segment %v in keypath %v)", part, i, keypathStr)
}
case []interface{}:
idx, err := strconv.ParseInt(part, 10, 64)
if err != nil {
return nil, errors.Wrapf(ErrKeypathNotFound, "could not parse key as integer: %v", err)
} else if idx < 0 || idx > int64(len(v)-1) {
return nil, errors.Wrapf(ErrIndexOutOfRange, "index %v out of range (segment %v of length %v in keypath %v)", idx, i, len(v), keypathStr)
}
currVal = v[idx]
default:
return nil, errors.Wrapf(ErrKeypathNotFound, "value at key '%v' is a %T, not a map or slice", part, currVal)
}
}
return currVal, nil
}
// Set sets a top-level variable specified by dotID.
// Returns error if either dotID is empty or it is a compound keypath.
func (vars Vars) Set(dotID string, value interface{}) error {
dotID = strings.TrimSpace(dotID)
if len(dotID) == 0 {
return ErrVarsRoot
} else if strings.Contains(dotID, KeypathSeparator) {
return errors.Wrapf(ErrVarsSetNested, "%s", dotID)
}
vars.vars[dotID] = value
return nil
}
// Copy makes a copy of Vars by copying the underlying map.
// Used by scheduler for new tasks to avoid data races.
func (vars Vars) Copy() Vars {
newVars := make(map[string]interface{})
// No need to copy recursively, because only the top-level map is mutable (see Set()).
for k, v := range vars.vars {
newVars[k] = v
}
return NewVarsFrom(newVars)
}