reflectutils

package
v1.30.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2024 License: Apache-2.0 Imports: 15 Imported by: 102

Documentation

Index

Constants

View Source
const (
	FieldPathElementTypeField = iota
	FieldPathElementTypeMapKey
	FieldPathElementTypeArrayIndex
	FieldPathElementTypeWildcardIndex
)

Variables

View Source
var SkipReflection = errors.New("skip this value")

Functions

func BuildTypeName

func BuildTypeName(t reflect.Type) string

func FormatValue

func FormatValue(value interface{}) string

FormatValue returns a string representing the value

func InvokeMethod

func InvokeMethod(target interface{}, name string, args ...interface{}) ([]reflect.Value, error)

InvokeMethod calls the specified method by reflection

func IsMethodNotFound

func IsMethodNotFound(err error) bool

func IsPrimitiveValue

func IsPrimitiveValue(v reflect.Value) bool

IsPrimitiveValue returns true if passed a value of primitive type: int, bool, etc Note that string (like []byte) is not treated as a primitive type

func JSONMergeStruct added in v1.19.0

func JSONMergeStruct(dest, src interface{})

JSONMergeStruct merges src into dest It uses a JSON marshal & unmarshal, so only fields that are JSON-visible will be copied If both source and destination has a value for a field, source takes presedence

func ReflectRecursive

func ReflectRecursive(v reflect.Value, visitor visitorFunc, options *ReflectOptions) error

ReflectRecursive calls visitor with v and every recursive sub-value, skipping subtrees if SkipReflection is returned

func RegisterPrinter

func RegisterPrinter(p Printer)

RegisterPrinter adds a custom printer function

func SetString added in v1.19.0

func SetString(target interface{}, targetPath string, newValue string) error

func Unset added in v1.21.3

func Unset(target interface{}, targetPath string) error

func ValueAsString

func ValueAsString(value reflect.Value) string

ValueAsString returns a human-readable string representation of the passed value

Types

type FieldPath added in v1.19.0

type FieldPath struct {
	// contains filtered or unexported fields
}

func ParseFieldPath added in v1.19.0

func ParseFieldPath(s string) (*FieldPath, error)

func (*FieldPath) Extend added in v1.19.0

func (f *FieldPath) Extend(el FieldPathElement) *FieldPath

func (*FieldPath) HasPrefixMatch added in v1.19.0

func (p *FieldPath) HasPrefixMatch(r *FieldPath) bool

func (*FieldPath) IsEmpty added in v1.19.0

func (p *FieldPath) IsEmpty() bool

func (*FieldPath) Matches added in v1.19.0

func (p *FieldPath) Matches(r *FieldPath) bool

func (*FieldPath) String added in v1.19.0

func (f *FieldPath) String() string

type FieldPathElement added in v1.19.0

type FieldPathElement struct {
	Type FieldPathElementType
	// contains filtered or unexported fields
}

type FieldPathElementType added in v1.19.0

type FieldPathElementType int

type MethodNotFoundError

type MethodNotFoundError struct {
	Name   string
	Target interface{}
}

func (*MethodNotFoundError) Error

func (e *MethodNotFoundError) Error() string

type Printer

type Printer func(o interface{}) (string, bool)

Printer is a custom printer function, so we can add special display for objects (without introducing a package dependency)

type ReflectOptions added in v1.19.0

type ReflectOptions struct {
	// JSONNames means the elements of the FieldPath will be the JSON field names (instead of the go field names)
	JSONNames bool

	// DeprecatedDoubleVisit activates the compatibility mode of the walker, where we end up visiting every struct field twice.
	// Ideally we can replace these instances over time.
	DeprecatedDoubleVisit bool
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL