@@ -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+
92132func isFloat (t string ) bool {
93133 return strings .HasPrefix (t , "float" )
94134}
@@ -110,6 +150,7 @@ import (
110150func 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