goref/keys.go

28 lines
522 B
Go

package goref
import "reflect"
func ExistMapKey(val any, key string) bool {
vo := reflect.ValueOf(val)
if vo.Kind() == reflect.Ptr {
vo = vo.Elem()
}
if vo.Kind() != reflect.Map {
return false
}
getter := vo.MapIndex(reflect.ValueOf(key))
return getter.IsValid()
}
func ExistStructField(val any, field string) bool {
vo := reflect.ValueOf(val)
if vo.Kind() == reflect.Ptr {
vo = vo.Elem()
}
if vo.Kind() != reflect.Struct {
return false
}
getter := vo.FieldByName(field)
return getter.IsValid()
}