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

Function IgnoreSliceElements

cmp/cmpopts/ignore.go:155–177  ·  view source on GitHub ↗

IgnoreSliceElements returns an [cmp.Option] that ignores elements of []V. The discard function must be of the form "func(T) bool" which is used to ignore slice elements of type V, where V is assignable to T. Elements are ignored if the function reports true.

(discardFunc interface{})

Source from the content-addressed store, hash-verified

153// ignore slice elements of type V, where V is assignable to T.
154// Elements are ignored if the function reports true.
155func IgnoreSliceElements(discardFunc interface{}) cmp.Option {
156 vf := reflect.ValueOf(discardFunc)
157 if !function.IsType(vf.Type(), function.ValuePredicate) || vf.IsNil() {
158 panic(fmt.Sprintf("invalid discard function: %T", discardFunc))
159 }
160 return cmp.FilterPath(func(p cmp.Path) bool {
161 si, ok := p.Index(-1).(cmp.SliceIndex)
162 if !ok {
163 return false
164 }
165 if !si.Type().AssignableTo(vf.Type().In(0)) {
166 return false
167 }
168 vx, vy := si.Values()
169 if vx.IsValid() && vf.Call([]reflect.Value{vx})[0].Bool() {
170 return true
171 }
172 if vy.IsValid() && vf.Call([]reflect.Value{vy})[0].Bool() {
173 return true
174 }
175 return false
176 }, cmp.Ignore())
177}
178
179// IgnoreMapEntries returns an [cmp.Option] that ignores entries of map[K]V.
180// The discard function must be of the form "func(T, R) bool" which is used to

Callers 1

TestOptionsFunction · 0.85

Calls 7

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

Tested by 1

TestOptionsFunction · 0.68