IgnoreMapEntries returns an [cmp.Option] that ignores entries of map[K]V. The discard function must be of the form "func(T, R) bool" which is used to ignore map entries of type K and V, where K and V are assignable to T and R. Entries are ignored if the function reports true.
(discardFunc interface{})
| 181 | // ignore map entries of type K and V, where K and V are assignable to T and R. |
| 182 | // Entries are ignored if the function reports true. |
| 183 | func IgnoreMapEntries(discardFunc interface{}) cmp.Option { |
| 184 | vf := reflect.ValueOf(discardFunc) |
| 185 | if !function.IsType(vf.Type(), function.KeyValuePredicate) || vf.IsNil() { |
| 186 | panic(fmt.Sprintf("invalid discard function: %T", discardFunc)) |
| 187 | } |
| 188 | return cmp.FilterPath(func(p cmp.Path) bool { |
| 189 | mi, ok := p.Index(-1).(cmp.MapIndex) |
| 190 | if !ok { |
| 191 | return false |
| 192 | } |
| 193 | if !mi.Key().Type().AssignableTo(vf.Type().In(0)) || !mi.Type().AssignableTo(vf.Type().In(1)) { |
| 194 | return false |
| 195 | } |
| 196 | k := mi.Key() |
| 197 | vx, vy := mi.Values() |
| 198 | if vx.IsValid() && vf.Call([]reflect.Value{k, vx})[0].Bool() { |
| 199 | return true |
| 200 | } |
| 201 | if vy.IsValid() && vf.Call([]reflect.Value{k, vy})[0].Bool() { |
| 202 | return true |
| 203 | } |
| 204 | return false |
| 205 | }, cmp.Ignore()) |
| 206 | } |