structSliceIndexByValue searches for first index that contains given value in field of given name.
(structSlice any, fieldName string, fieldValue any)
| 517 | // structSliceIndexByValue searches for first index that contains given value in field of |
| 518 | // given name. |
| 519 | func 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 | |
| 543 | func (tb *Table) editIndex(idx int) { |
| 544 | if idx < 0 || idx >= tb.sliceUnderlying.Len() { |
no test coverage detected