Skip to content

Commit d66ffcd

Browse files
committed
Merge compare-arrays
2 parents c0ad2d4 + 524efca commit d66ffcd

File tree

4 files changed

+462
-0
lines changed

4 files changed

+462
-0
lines changed

Diff for: expr_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -2713,3 +2713,29 @@ func TestPredicateCombination(t *testing.T) {
27132713
})
27142714
}
27152715
}
2716+
2717+
func TestArrayComparison(t *testing.T) {
2718+
tests := []struct {
2719+
env any
2720+
code string
2721+
}{
2722+
{[]string{"A", "B"}, "foo == ['A', 'B']"},
2723+
{[]int{1, 2}, "foo == [1, 2]"},
2724+
{[]uint8{1, 2}, "foo == [1, 2]"},
2725+
{[]float64{1.1, 2.2}, "foo == [1.1, 2.2]"},
2726+
{[]any{"A", 1, 1.1, true}, "foo == ['A', 1, 1.1, true]"},
2727+
{[]string{"A", "B"}, "foo != [1, 2]"},
2728+
}
2729+
2730+
for _, tt := range tests {
2731+
t.Run(tt.code, func(t *testing.T) {
2732+
env := map[string]any{"foo": tt.env}
2733+
program, err := expr.Compile(tt.code, expr.Env(env))
2734+
require.NoError(t, err)
2735+
2736+
out, err := expr.Run(program, env)
2737+
require.NoError(t, err)
2738+
require.Equal(t, true, out)
2739+
})
2740+
}
2741+
}

Diff for: vm/runtime/helpers/main.go

+41
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func main() {
1919
"cases_with_duration": func(op string) string {
2020
return cases(op, uints, ints, floats, []string{"time.Duration"})
2121
},
22+
"array_equal_cases": func() string { return arrayEqualCases([]string{"string"}, uints, ints, floats) },
2223
}).
2324
Parse(helpers),
2425
).Execute(&b, nil)
@@ -89,6 +90,45 @@ func cases(op string, xs ...[]string) string {
8990
return strings.TrimRight(out, "\n")
9091
}
9192

93+
func arrayEqualCases(xs ...[]string) string {
94+
var types []string
95+
for _, x := range xs {
96+
types = append(types, x...)
97+
}
98+
99+
_, _ = fmt.Fprintf(os.Stderr, "Generating array equal cases for %v\n", types)
100+
101+
var out string
102+
echo := func(s string, xs ...any) {
103+
out += fmt.Sprintf(s, xs...) + "\n"
104+
}
105+
echo(`case []any:`)
106+
echo(`switch y := b.(type) {`)
107+
for _, a := range append(types, "any") {
108+
echo(`case []%v:`, a)
109+
echo(`if len(x) != len(y) { return false }`)
110+
echo(`for i := range x {`)
111+
echo(`if !Equal(x[i], y[i]) { return false }`)
112+
echo(`}`)
113+
echo("return true")
114+
}
115+
echo(`}`)
116+
for _, a := range types {
117+
echo(`case []%v:`, a)
118+
echo(`switch y := b.(type) {`)
119+
echo(`case []any:`)
120+
echo(`return Equal(y, x)`)
121+
echo(`case []%v:`, a)
122+
echo(`if len(x) != len(y) { return false }`)
123+
echo(`for i := range x {`)
124+
echo(`if x[i] != y[i] { return false }`)
125+
echo(`}`)
126+
echo("return true")
127+
echo(`}`)
128+
}
129+
return strings.TrimRight(out, "\n")
130+
}
131+
92132
func isFloat(t string) bool {
93133
return strings.HasPrefix(t, "float")
94134
}
@@ -110,6 +150,7 @@ import (
110150
func Equal(a, b interface{}) bool {
111151
switch x := a.(type) {
112152
{{ cases "==" }}
153+
{{ array_equal_cases }}
113154
case string:
114155
switch y := b.(type) {
115156
case string:

0 commit comments

Comments
 (0)