@@ -19,6 +19,7 @@ func main() {
19
19
"cases_with_duration" : func (op string ) string {
20
20
return cases (op , uints , ints , floats , []string {"time.Duration" })
21
21
},
22
+ "array_equal_cases" : func () string { return arrayEqualCases ([]string {"string" }, uints , ints , floats ) },
22
23
}).
23
24
Parse (helpers ),
24
25
).Execute (& b , nil )
@@ -89,6 +90,45 @@ func cases(op string, xs ...[]string) string {
89
90
return strings .TrimRight (out , "\n " )
90
91
}
91
92
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
+
92
132
func isFloat (t string ) bool {
93
133
return strings .HasPrefix (t , "float" )
94
134
}
@@ -110,6 +150,7 @@ import (
110
150
func Equal(a, b interface{}) bool {
111
151
switch x := a.(type) {
112
152
{{ cases "==" }}
153
+ {{ array_equal_cases }}
113
154
case string:
114
155
switch y := b.(type) {
115
156
case string:
0 commit comments