MCPcopy Create free account
hub / github.com/cogentcore/core / structSliceIndexByValue

Function structSliceIndexByValue

core/table.go:519–541  ·  view source on GitHub ↗

structSliceIndexByValue searches for first index that contains given value in field of given name.

(structSlice any, fieldName string, fieldValue any)

Source from the content-addressed store, hash-verified

517// structSliceIndexByValue searches for first index that contains given value in field of
518// given name.
519func structSliceIndexByValue(structSlice any, fieldName string, fieldValue any) (int, error) {
520 svnp := reflectx.NonPointerValue(reflect.ValueOf(structSlice))
521 sz := svnp.Len()
522 struTyp := reflectx.NonPointerType(reflect.TypeOf(structSlice).Elem().Elem())
523 fld, ok := struTyp.FieldByName(fieldName)
524 if !ok {
525 err := fmt.Errorf("StructSliceRowByValue: field name: %v not found", fieldName)
526 slog.Error(err.Error())
527 return -1, err
528 }
529 fldIndex := fld.Index
530 for idx := 0; idx < sz; idx++ {
531 rval := reflectx.UnderlyingPointer(svnp.Index(idx))
532 fval := rval.Elem().FieldByIndex(fldIndex)
533 if !fval.IsValid() {
534 continue
535 }
536 if fval.Interface() == fieldValue {
537 return idx, nil
538 }
539 }
540 return -1, nil
541}
542
543func (tb *Table) editIndex(idx int) {
544 if idx < 0 || idx >= tb.sliceUnderlying.Len() {

Callers 2

InitMethod · 0.85
selectFieldValueMethod · 0.85

Calls 11

NonPointerValueFunction · 0.92
NonPointerTypeFunction · 0.92
UnderlyingPointerFunction · 0.92
ElemMethod · 0.80
FieldByNameMethod · 0.80
FieldByIndexMethod · 0.80
LenMethod · 0.65
ErrorfMethod · 0.65
ErrorMethod · 0.65
IndexMethod · 0.45
IsValidMethod · 0.45

Tested by

no test coverage detected