reflect: Add FieldByNameFunc

This adds FieldByNameFunc, which some libraries like reflect2 need.

For my usecase I could also just stub FieldByNameFunc to panic, but
figured that it would work OK to just make it work. I'm not sure if
the overhead to FieldByName using a closure is acceptable.

Signed-off-by: Tyler Rockwood <rockwood@redpanda.com>
This commit is contained in:
Tyler Rockwood
2023-06-22 15:23:19 -05:00
committed by Ron Evans
parent dd4e9e86e7
commit fdc4bbbfad
3 changed files with 48 additions and 5 deletions
+11
View File
@@ -1846,6 +1846,17 @@ func (v Value) FieldByName(name string) Value {
return Value{}
}
func (v Value) FieldByNameFunc(match func(string) bool) Value {
if v.Kind() != Struct {
panic(&ValueError{"FieldByName", v.Kind()})
}
if field, ok := v.typecode.FieldByNameFunc(match); ok {
return v.FieldByIndex(field.Index)
}
return Value{}
}
//go:linkname hashmapMake runtime.hashmapMakeUnsafePointer
func hashmapMake(keySize, valueSize uintptr, sizeHint uintptr, alg uint8) unsafe.Pointer