package hreflect

import "github.com/gohugoio/hugo/common/hreflect"

Package hreflect contains reflect helpers.

Index

Functions

func AsTime

func AsTime(v reflect.Value, loc *time.Location) (time.Time, bool)

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

func CallMethodByName(cxt context.Context, name string, v reflect.Value) []reflect.Value

CallMethodByName calls the method with the given name on v.

func ConvertIfPossible

func ConvertIfPossible(val reflect.Value, typ reflect.Type) (reflect.Value, bool)

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

func GetMethodByName(v reflect.Value, name string) reflect.Value

GetMethodByName is the same as reflect.Value.MethodByName, but it caches the lookup.

func GetMethodByNameForType

func GetMethodByNameForType(tp reflect.Type, name string) reflect.Method

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

func GetMethodIndexByName(tp reflect.Type, name string) int

GetMethodIndexByName returns the index of the method with the given name, or -1 if no such method exists.

func Indirect

func Indirect(v reflect.Value) (vv reflect.Value, isNil bool)

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

func IndirectElem(v reflect.Value) (vv reflect.Value, isNil bool)

IndirectElem is like Indirect, but if the final value is a pointer, it unwraps it.

func IsContextType

func IsContextType(tp reflect.Type) bool

IsContextType returns whether tp is a context.Context type.

func IsFloat

func IsFloat(kind reflect.Kind) bool

IsFloat returns whether the given kind is a float.

func IsInt

func IsInt(kind reflect.Kind) bool

IsInt returns whether the given kind is an int.

func IsInterfaceOrPointer

func IsInterfaceOrPointer(kind reflect.Kind) bool

IsInterfaceOrPointer returns whether the given kind is an interface or a pointer.

func IsMap

func IsMap(v any) bool

IsMap reports whether v is a map.

func IsNil

func IsNil(v reflect.Value) bool

IsNil reports whether v is nil. Based on reflect.Value.IsNil, but also considers invalid values as nil.

func IsNumber

func IsNumber(kind reflect.Kind) bool

TODO(bep) replace the private versions in /tpl with these. IsNumber returns whether the given kind is a number.

func IsSlice

func IsSlice(v any) bool

IsSlice reports whether v is a slice.

func IsString

func IsString(kind reflect.Kind) bool

IsString returns whether the given kind is a string.

func IsTime

func IsTime(tp reflect.Type) bool

IsTime returns whether tp is a time.Time type or if it can be converted into one in ToTime.

func IsTruthful

func IsTruthful(in any) bool

IsTruthful returns whether in represents a truthful value. See IsTruthfulValue

func IsTruthfulValue

func IsTruthfulValue(val reflect.Value) (truth bool)

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.

Based on: https://github.com/golang/go/blob/178a2c42254166cffed1b25fb1d3c7a5727cada6/src/text/template/exec.go#L306

func IsUint

func IsUint(kind reflect.Kind) bool

IsUint returns whether the given kind is an uint.

func IsValid

func IsValid(v reflect.Value) bool

IsValid returns whether v is not nil and a valid value.

func ToFloat64

func ToFloat64(v reflect.Value) float64

ToFloat64 converts v to float64 if possible, panicking if not.

func ToFloat64E

func ToFloat64E(v reflect.Value) (float64, error)

ToFloat64E converts v to float64 if possible, returning an error if not.

func ToInt64

func ToInt64(v reflect.Value) int64

ToInt64 converts v to int64 if possible. It panics if the conversion is not possible.

func ToInt64E

func ToInt64E(v reflect.Value) (int64, error)

ToInt64 converts v to int64 if possible, returning an error if not.

func ToSliceAny

func ToSliceAny(v any) ([]any, bool)

ToSliceAny converts the given value to a slice of any if possible.

func ToString

func ToString(v reflect.Value) string

ToString converts v to string if possible, panicking if not.

func ToStringE

func ToStringE(v reflect.Value) (string, error)

ToStringE converts v to string if possible, returning an error if not.

func ToStringValueE

func ToStringValueE(v reflect.Value) (reflect.Value, error)

Source Files

convert.go helpers.go

Version
v0.153.4 (latest)
Published
Dec 28, 2025
Platform
linux/amd64
Imports
9 packages
Last checked
4 months ago

Tools for package owners.