| 1 |
// Copyright (c) 2020-2023, Maxime Soulé |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// This source code is licensed under the BSD-style license found in the |
| 5 |
// LICENSE file in the root directory of this source tree. |
| 6 |
|
| 7 |
package td |
| 8 |
|
| 9 |
import ( |
| 10 |
"reflect" |
| 11 |
"strings" |
| 12 |
|
| 13 |
"github.com/maxatome/go-testdeep/internal/color" |
| 14 |
"github.com/maxatome/go-testdeep/internal/flat" |
| 15 |
"github.com/maxatome/go-testdeep/internal/types" |
| 16 |
) |
| 17 |
|
| 18 |
func flattenFuncIsValid(typ reflect.Type) bool { |
| 19 |
return typ.Kind() == reflect.Func && |
| 20 |
(typ.NumIn() == 1 && !typ.IsVariadic() || |
| 21 |
typ.NumIn() == 2 && typ.IsVariadic()) && |
| 22 |
(typ.NumOut() == 1 || typ.NumOut() == 2 && typ.Out(1) == types.Bool) |
| 23 |
} |
| 24 |
|
| 25 |
// Flatten allows to flatten any slice, array, map or int range in |
| 26 |
// parameters of operators expecting ...any. fn parameter allows to |
| 27 |
// filter and/or transform items before flattening and is described |
| 28 |
// below. |
| 29 |
// |
| 30 |
// For example the [Set] operator is defined as: |
| 31 |
// |
| 32 |
// func Set(expectedItems ...any) TestDeep |
| 33 |
// |
| 34 |
// so when comparing to a []int slice, we usually do: |
| 35 |
// |
| 36 |
// got := []int{42, 66, 22} |
| 37 |
// td.Cmp(t, got, td.Set(22, 42, 66)) |
| 38 |
// |
| 39 |
// it works but if the expected items are already in a []int, we have |
| 40 |
// to copy them in a []any as it can not be flattened directly |
| 41 |
// in [Set] parameters: |
| 42 |
// |
| 43 |
// expected := []int{22, 42, 66} |
| 44 |
// expectedIf := make([]any, len(expected)) |
| 45 |
// for i, item := range expected { |
| 46 |
// expectedIf[i] = item |
| 47 |
// } |
| 48 |
// td.Cmp(t, got, td.Set(expectedIf...)) |
| 49 |
// |
| 50 |
// but it is a bit boring and less efficient, as [Set] does not keep |
| 51 |
// the []any behind the scene. |
| 52 |
// |
| 53 |
// The same with Flatten follows: |
| 54 |
// |
| 55 |
// expected := []int{22, 42, 66} |
| 56 |
// td.Cmp(t, got, td.Set(td.Flatten(expected))) |
| 57 |
// |
| 58 |
// Several Flatten calls can be passed, and even combined with normal |
| 59 |
// parameters: |
| 60 |
// |
| 61 |
// expectedPart1 := []int{11, 22, 33} |
| 62 |
// expectedPart2 := []int{55, 66, 77} |
| 63 |
// expectedPart3 := []int{99} |
| 64 |
// td.Cmp(t, got, |
| 65 |
// td.Set( |
| 66 |
// td.Flatten(expectedPart1), |
| 67 |
// 44, |
| 68 |
// td.Flatten(expectedPart2), |
| 69 |
// 88, |
| 70 |
// td.Flatten(expectedPart3), |
| 71 |
// )) |
| 72 |
// |
| 73 |
// is exactly the same as: |
| 74 |
// |
| 75 |
// td.Cmp(t, got, td.Set(11, 22, 33, 44, 55, 66, 77, 88, 99)) |
| 76 |
// |
| 77 |
// Note that Flatten calls can even be nested: |
| 78 |
// |
| 79 |
// td.Cmp(t, got, |
| 80 |
// td.Set( |
| 81 |
// td.Flatten([]any{ |
| 82 |
// td.Flatten(5), |
| 83 |
// 11, |
| 84 |
// td.Flatten([]int{22, 33}), |
| 85 |
// td.Flatten([]int{44, 55, 66}), |
| 86 |
// }), |
| 87 |
// 77, |
| 88 |
// )) |
| 89 |
// |
| 90 |
// is exactly the same as: |
| 91 |
// |
| 92 |
// td.Cmp(t, got, td.Set(0, 1, 2, 3, 4, 11, 22, 33, 44, 55, 66, 77)) |
| 93 |
// |
| 94 |
// Maps can be flattened too, keeping in mind there is no particular order: |
| 95 |
// |
| 96 |
// td.Flatten(map[int]int{1: 2, 3: 4}) |
| 97 |
// |
| 98 |
// is flattened as 1, 2, 3, 4 or 3, 4, 1, 2. |
| 99 |
// |
| 100 |
// As seen in the example above, int range: |
| 101 |
// |
| 102 |
// td.Flatten(5) |
| 103 |
// |
| 104 |
// is flattened as 0, 1, 2, 3, 4. |
| 105 |
// |
| 106 |
// Optional fn parameter can be used to filter and/or transform items |
| 107 |
// before flattening. If passed, it has to be one element length and |
| 108 |
// this single element can be: |
| 109 |
// |
| 110 |
// - untyped nil: it is a no-op, as if it was not passed |
| 111 |
// - a function |
| 112 |
// - a string shortcut |
| 113 |
// |
| 114 |
// If it is a function, it must be a non-nil function with a signature like: |
| 115 |
// |
| 116 |
// func(T) V |
| 117 |
// func(T) (V, bool) |
| 118 |
// func(T, X...) V |
| 119 |
// func(T, X...) (V, bool) |
| 120 |
// |
| 121 |
// T can be the same as V, but it is not mandatory. The (V, bool) |
| 122 |
// returned cases allow to exclude some items when returning |
| 123 |
// false. For the variadic cases, X does not matter as the function is |
| 124 |
// always called without any variadic argument. |
| 125 |
// |
| 126 |
// If the function signature does not match these cases, Flatten panics. |
| 127 |
// |
| 128 |
// If the type of an item of sliceOrMapOrInt is not convertible to T, |
| 129 |
// the item is dropped silently, as if fn returns false. |
| 130 |
// |
| 131 |
// This single element can also be a string among: |
| 132 |
// |
| 133 |
// "Smuggle:FIELD" |
| 134 |
// "JSONPointer:/PATH" |
| 135 |
// |
| 136 |
// that are shortcuts for respectively: |
| 137 |
// |
| 138 |
// func(in any) any { return td.Smuggle("FIELD", in) } |
| 139 |
// func(in any) any { return td.JSONPointer("/PATH", in) } |
| 140 |
// |
| 141 |
// See [Smuggle] and [JSONPointer] for a description of what "FIELD" |
| 142 |
// and "/PATH" can really be. |
| 143 |
// |
| 144 |
// Flatten with an fn can be useful when testing some fields of |
| 145 |
// structs in a slice with [Bag] or [Set] operators families as well |
| 146 |
// as [List]. As an example, here we test only "Name" field for each |
| 147 |
// item of a person slice: |
| 148 |
// |
| 149 |
// type person struct { |
| 150 |
// Name string `json:"name"` |
| 151 |
// Age int `json:"age"` |
| 152 |
// } |
| 153 |
// got := []person{{"alice", 22}, {"bob", 18}, {"brian", 34}, {"britt", 32}} |
| 154 |
// |
| 155 |
// td.Cmp(t, got, |
| 156 |
// td.Bag(td.Flatten( |
| 157 |
// []string{"alice", "britt", "brian", "bob"}, |
| 158 |
// func(name string) any { return td.Smuggle("Name", name) }))) |
| 159 |
// // distributes td.Smuggle for each Name, so is equivalent of: |
| 160 |
// td.Cmp(t, got, td.Bag( |
| 161 |
// td.Smuggle("Name", "alice"), |
| 162 |
// td.Smuggle("Name", "britt"), |
| 163 |
// td.Smuggle("Name", "brian"), |
| 164 |
// td.Smuggle("Name", "bob"), |
| 165 |
// )) |
| 166 |
// |
| 167 |
// // Same here using Smuggle string shortcut |
| 168 |
// td.Cmp(t, got, |
| 169 |
// td.Bag(td.Flatten( |
| 170 |
// []string{"alice", "britt", "brian", "bob"}, "Smuggle:Name"))) |
| 171 |
// |
| 172 |
// // Same here, but using JSONPointer operator |
| 173 |
// td.Cmp(t, got, |
| 174 |
// td.Bag(td.Flatten( |
| 175 |
// []string{"alice", "britt", "brian", "bob"}, |
| 176 |
// func(name string) any { return td.JSONPointer("/name", name) }))) |
| 177 |
// |
| 178 |
// // Same here using JSONPointer string shortcut |
| 179 |
// td.Cmp(t, got, |
| 180 |
// td.Bag(td.Flatten( |
| 181 |
// []string{"alice", "britt", "brian", "bob"}, "JSONPointer:/name"))) |
| 182 |
// |
| 183 |
// // Same here, but using SuperJSONOf operator |
| 184 |
// td.Cmp(t, got, |
| 185 |
// td.Bag(td.Flatten( |
| 186 |
// []string{"alice", "britt", "brian", "bob"}, |
| 187 |
// func(name string) any { return td.SuperJSONOf(`{"name":$1}`, name) }))) |
| 188 |
// |
| 189 |
// // Same here, but using Struct operator |
| 190 |
// td.Cmp(t, got, |
| 191 |
// td.Bag(td.Flatten( |
| 192 |
// []string{"alice", "britt", "brian", "bob"}, |
| 193 |
// func(name string) any { return td.Struct(person{Name: name}) }))) |
| 194 |
// |
| 195 |
// See also [Grep]. |
| 196 |
func Flatten(sliceOrMapOrInt any, fn ...any) flat.Slice { |
| 197 |
const ( |
| 198 |
smugglePrefix = "Smuggle:" |
| 199 |
jsonPointerPrefix = "JSONPointer:" |
| 200 |
usage = "Flatten(SLICE|ARRAY|MAP|int[, FUNC])" |
| 201 |
usageFunc = usage + `, FUNC should be non-nil func(T) V or func(T) (V, bool) or a string "` + smugglePrefix + `…" or "` + jsonPointerPrefix + `…"` |
| 202 |
) |
| 203 |
|
| 204 |
if _, isInt := sliceOrMapOrInt.(int); !isInt { |
| 205 |
switch reflect.ValueOf(sliceOrMapOrInt).Kind() { |
| 206 |
case reflect.Slice, reflect.Array, reflect.Map: |
| 207 |
default: |
| 208 |
panic(color.BadUsage(usage, sliceOrMapOrInt, 1, true)) |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
switch len(fn) { |
| 213 |
case 1: |
| 214 |
if fn[0] != nil { |
| 215 |
break |
| 216 |
} |
| 217 |
fallthrough |
| 218 |
case 0: |
| 219 |
return flat.Slice{Slice: sliceOrMapOrInt} |
| 220 |
default: |
| 221 |
panic(color.TooManyParams(usage)) |
| 222 |
} |
| 223 |
|
| 224 |
f := fn[0] |
| 225 |
|
| 226 |
// Smuggle & JSONPointer specific shortcuts |
| 227 |
if s, ok := f.(string); ok { |
| 228 |
switch { |
| 229 |
case strings.HasPrefix(s, smugglePrefix): |
| 230 |
f = func(in any) any { |
| 231 |
return Smuggle(s[len(smugglePrefix):], in) |
| 232 |
} |
| 233 |
case strings.HasPrefix(s, jsonPointerPrefix): |
| 234 |
f = func(in any) any { |
| 235 |
return JSONPointer(s[len(jsonPointerPrefix):], in) |
| 236 |
} |
| 237 |
default: |
| 238 |
panic(color.Bad("usage: "+usageFunc+", but received %q as 2nd parameter", s)) |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
fnType := reflect.TypeOf(f) |
| 243 |
vfn := reflect.ValueOf(f) |
| 244 |
|
| 245 |
if !flattenFuncIsValid(fnType) { |
| 246 |
panic(color.BadUsage(usageFunc, f, 2, false)) |
| 247 |
} |
| 248 |
if vfn.IsNil() { |
| 249 |
panic(color.Bad("usage: " + usageFunc)) |
| 250 |
} |
| 251 |
|
| 252 |
inType := fnType.In(0) |
| 253 |
|
| 254 |
var final []any |
| 255 |
for _, v := range flat.Values([]any{flat.Slice{Slice: sliceOrMapOrInt}}) { |
| 256 |
if v.Type() != inType { |
| 257 |
if !v.Type().ConvertibleTo(inType) { |
| 258 |
continue |
| 259 |
} |
| 260 |
v = v.Convert(inType) |
| 261 |
} |
| 262 |
|
| 263 |
ret := vfn.Call([]reflect.Value{v}) |
| 264 |
if len(ret) == 1 || ret[1].Bool() { |
| 265 |
final = append(final, ret[0].Interface()) |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
return flat.Slice{Slice: final} |
| 270 |
} |