package hreflect
import "github.com/gohugoio/hugo/common/hreflect"
Package hreflect contains reflect helpers.
Index ¶
- func AsTime(v reflect.Value, loc *time.Location) (time.Time, bool)
- func CallMethodByName(cxt context.Context, name string, v reflect.Value) []reflect.Value
- func ConvertIfPossible(val reflect.Value, typ reflect.Type) (reflect.Value, bool)
- func GetMethodByName(v reflect.Value, name string) reflect.Value
- func GetMethodByNameForType(tp reflect.Type, name string) reflect.Method
- func GetMethodIndexByName(tp reflect.Type, name string) int
- func Indirect(v reflect.Value) (vv reflect.Value, isNil bool)
- func IndirectElem(v reflect.Value) (vv reflect.Value, isNil bool)
- func IsContextType(tp reflect.Type) bool
- func IsFloat(kind reflect.Kind) bool
- func IsInt(kind reflect.Kind) bool
- func IsInterfaceOrPointer(kind reflect.Kind) bool
- func IsMap(v any) bool
- func IsNil(v reflect.Value) bool
- func IsNumber(kind reflect.Kind) bool
- func IsSlice(v any) bool
- func IsString(kind reflect.Kind) bool
- func IsTime(tp reflect.Type) bool
- func IsTruthful(in any) bool
- func IsTruthfulValue(val reflect.Value) (truth bool)
- func IsUint(kind reflect.Kind) bool
- func IsValid(v reflect.Value) bool
- func ToFloat64(v reflect.Value) float64
- func ToFloat64E(v reflect.Value) (float64, error)
- func ToInt64(v reflect.Value) int64
- func ToInt64E(v reflect.Value) (int64, error)
- func ToSliceAny(v any) ([]any, bool)
- func ToString(v reflect.Value) string
- func ToStringE(v reflect.Value) (string, error)
- func ToStringValueE(v reflect.Value) (reflect.Value, error)
Functions ¶
func AsTime ¶
AsTime returns v as a time.Time if possible. The given location is only used if the value implements AsTimeProvider (e.g. go-toml local). A zero Time and false is returned if this isn't possible. Note that this function does not accept string dates.
func CallMethodByName ¶
CallMethodByName calls the method with the given name on v.
func ConvertIfPossible ¶
ConvertIfPossible tries to convert val to typ if possible. This is currently only implemented for int kinds, added to handle the move to a new YAML library which produces uint64 for unsigned integers. We can expand on this later if needed. This conversion is lossless. See Issue 14079.
func GetMethodByName ¶
GetMethodByName is the same as reflect.Value.MethodByName, but it caches the lookup.
func GetMethodByNameForType ¶
GetMethodByNameForType returns the method with the given name for the given type, or a zero Method if no such method exists. It panics if tp is an interface type. It caches the lookup.
func GetMethodIndexByName ¶
GetMethodIndexByName returns the index of the method with the given name, or -1 if no such method exists.
func Indirect ¶
Indirect unwraps interfaces and pointers until it finds a non-interface/pointer value. If a nil is encountered, the second return value is true. If a pointer to a struct is encountered, it is not unwrapped.
func IndirectElem ¶
IndirectElem is like Indirect, but if the final value is a pointer, it unwraps it.
func IsContextType ¶
IsContextType returns whether tp is a context.Context type.
func IsFloat ¶
IsFloat returns whether the given kind is a float.
func IsInt ¶
IsInt returns whether the given kind is an int.
func IsInterfaceOrPointer ¶
IsInterfaceOrPointer returns whether the given kind is an interface or a pointer.
func IsMap ¶
IsMap reports whether v is a map.
func IsNil ¶
IsNil reports whether v is nil. Based on reflect.Value.IsNil, but also considers invalid values as nil.
func IsNumber ¶
TODO(bep) replace the private versions in /tpl with these. IsNumber returns whether the given kind is a number.
func IsSlice ¶
IsSlice reports whether v is a slice.
func IsString ¶
IsString returns whether the given kind is a string.
func IsTime ¶
IsTime returns whether tp is a time.Time type or if it can be converted into one in ToTime.
func IsTruthful ¶
IsTruthful returns whether in represents a truthful value. See IsTruthfulValue
func IsTruthfulValue ¶
IsTruthfulValue returns whether the given value has a meaningful truth value. This is based on template.IsTrue in Go's stdlib, but also considers IsZero and any interface value will be unwrapped before it's considered for truthfulness.
func IsUint ¶
IsUint returns whether the given kind is an uint.
func IsValid ¶
IsValid returns whether v is not nil and a valid value.
func ToFloat64 ¶
ToFloat64 converts v to float64 if possible, panicking if not.
func ToFloat64E ¶
ToFloat64E converts v to float64 if possible, returning an error if not.
func ToInt64 ¶
ToInt64 converts v to int64 if possible. It panics if the conversion is not possible.
func ToInt64E ¶
ToInt64 converts v to int64 if possible, returning an error if not.
func ToSliceAny ¶
ToSliceAny converts the given value to a slice of any if possible.
func ToString ¶
ToString converts v to string if possible, panicking if not.
func ToStringE ¶
ToStringE converts v to string if possible, returning an error if not.
func ToStringValueE ¶
Source Files ¶
- Version
- v0.153.4 (latest)
- Published
- Dec 28, 2025
- Platform
- linux/amd64
- Imports
- 9 packages
- Last checked
- 4 months ago –
Tools for package owners.