MCPcopy
hub / github.com/google/go-cmp / IgnoreMapEntries

Function IgnoreMapEntries

cmp/cmpopts/ignore.go:183–206  ·  view source on GitHub ↗

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{})

Source from the content-addressed store, hash-verified

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.
183func 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}

Callers 1

TestOptionsFunction · 0.85

Calls 8

IsTypeFunction · 0.92
FilterPathFunction · 0.92
IgnoreFunction · 0.92
IsNilMethod · 0.80
TypeMethod · 0.65
ValuesMethod · 0.65
IndexMethod · 0.45
KeyMethod · 0.45

Tested by 1

TestOptionsFunction · 0.68