forked from geofffranks/spruce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathop_cartesian_product.go
165 lines (133 loc) · 4.26 KB
/
op_cartesian_product.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package spruce
import (
"fmt"
"github.com/starkandwayne/goutils/ansi"
"github.com/starkandwayne/goutils/tree"
. "github.com/geofffranks/spruce/log"
)
// CartesianProductOperator ...
type CartesianProductOperator struct{}
// Setup ...
func (CartesianProductOperator) Setup() error {
return nil
}
// Phase ...
func (CartesianProductOperator) Phase() OperatorPhase {
return EvalPhase
}
// Dependencies ...
func (CartesianProductOperator) Dependencies(_ *Evaluator, args []*Expr, locs []*tree.Cursor, auto []*tree.Cursor) []*tree.Cursor {
l := []*tree.Cursor{}
for _, arg := range args {
if arg.Type != Reference {
continue
}
for _, other := range locs {
if other.Under(arg.Reference) {
l = append(l, other)
}
}
}
//append autogenerated dependencies (operator reference-type arguments)
for _, dep := range auto {
l = append(l, dep)
}
return l
}
// Run ...
func (CartesianProductOperator) Run(ev *Evaluator, args []*Expr) (*Response, error) {
DEBUG("running (( cartesian-product ... )) operation at $.%s", ev.Here)
defer DEBUG("done with (( cartesian-product ... )) operation at $%s\n", ev.Here)
var vals [][]string
for i, arg := range args {
v, err := arg.Resolve(ev.Tree)
if err != nil {
DEBUG(" [%d]: resolution failed\n error: %s", i, err)
return nil, err
}
switch v.Type {
case Literal:
DEBUG(" arg[%d]: found string literal '%s'", i, v.Literal)
vals = append(vals, []string{v.Literal.(string)})
case Reference:
DEBUG(" arg[%d]: trying to resolve reference $.%s", i, v.Reference)
s, err := v.Reference.Resolve(ev.Tree)
if err != nil {
DEBUG(" [%d]: resolution failed\n error: %s", i, err)
return nil, ansi.Errorf("Unable to resolve `@m{%s}`: %s", v.Reference, err)
}
switch s.(type) {
case []interface{}:
var strs []string
DEBUG(" [%d]: resolved to a list; verifying", i)
for j, sub := range s.([]interface{}) {
if _, ok := sub.([]interface{}); ok {
DEBUG(" list[%d]: list item is itself a list; error!", j)
return nil, fmt.Errorf("cartesian-product operator can only operate on lists of scalar values")
} else if _, ok := sub.(map[interface{}]interface{}); ok {
DEBUG(" list[%d]: list item is a map; error!", j)
return nil, fmt.Errorf("cartesian-product operator can only operate on lists of scalar values")
}
DEBUG(" list[%d]: list item is a scalar: %v", j, sub)
strs = append(strs, fmt.Sprintf("%v", sub))
}
vals = append(vals, strs)
case map[interface{}]interface{}:
DEBUG(" [%d]: resolved to a map; error!", i)
return nil, fmt.Errorf("cartesian-product operator only accepts arrays and string values")
default:
DEBUG(" [%d]: resolved to a scalar; appending", i)
vals = append(vals, []string{fmt.Sprintf("%v", s)})
}
default:
DEBUG(" arg[%d]: I don't know what to do with '%v'", i, arg)
return nil, fmt.Errorf("cartesian-product operator only accepts key reference arguments")
}
DEBUG("")
}
switch len(args) {
case 0:
DEBUG(" no arguments supplied to (( cartesian-product ... )) operation. oops.")
return nil, ansi.Errorf("no arguments specified to @c{(( cartesian-product ... ))}")
case 1:
DEBUG(" called with only one argument; returning value as-is")
return &Response{
Type: Replace,
Value: vals[0],
}, nil
default:
DEBUG(" called with more than one arguments; combining into a single list of strings")
lst := []interface{}{}
//Bootstrap the return list, making a list with interfaces, not strings
for _, v := range vals[0] {
lst = append(lst, v)
}
for _, l := range vals[1:] {
lst = cartesian(lst, l)
}
return &Response{
Type: Replace,
Value: lst,
}, nil
}
}
// input 'a' and the output are always a list of strings, but we need it to be
// a list of interfaces in the Go type system in order to be consistent with the
// way that the rest of spruce handles yaml lists
func cartesian(a []interface{}, b []string) []interface{} {
if len(a) == 0 || len(b) == 0 {
return []interface{}{}
}
l := make([]interface{}, len(a)*len(b))
n := 0
for _, x := range a {
for _, y := range b {
l[n] = fmt.Sprintf("%s%s", x, y)
n++
}
}
return l
}
func init() {
RegisterOp("cartesian-product", CartesianProductOperator{})
}