reflectext

package
v0.0.479 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrAccessStructArrayAccess = errors.New("trying to access array")
View Source
var ErrAccessStructFieldInPathWasNil = errors.New("a field in the path was nil")
View Source
var ErrAccessStructInvalidArrayIndex = errors.New("invalid array index")
View Source
var ErrAccessStructInvalidFieldType = errors.New("invalid field type")
View Source
var ErrAccessStructInvalidMapKey = errors.New("invalid map key")
View Source
var ErrAccessStructMapAccess = errors.New("trying to access map")
View Source
var ErrAccessStructMissingField = errors.New("missing field")

Functions

func AccessJSONStruct added in v0.0.479

func AccessJSONStruct[TResult any](v any, path string) (TResult, error)

func AccessStruct added in v0.0.479

func AccessStruct[TResult any](v any, path string) (TResult, error)

func AccessStructByArrayPath added in v0.0.479

func AccessStructByArrayPath[TResult any](v any, path []string, opts ...AccessStructOpt) (TResult, error)

func AccessStructByStringPath added in v0.0.479

func AccessStructByStringPath[TResult any](v any, path string, opts ...AccessStructOpt) (TResult, error)

func ConvertStructToMap added in v0.0.381

func ConvertStructToMap(v any, opts ...ConvertStructToMapOpt) map[string]any

func GetMapField added in v0.0.367

func GetMapField[TData any, TKey comparable](mapval any, key TKey) (TData, bool)

GetMapField gets the value of a map, without knowing the actual types (mapval is any) eg: x := langext.H{"K1": 665} GetMapPath[int](x, "K1") == 665

works with aliased types and autom. dereferences pointes

func GetMapPath added in v0.0.367

func GetMapPath[TData any](mapval any, path string) (TData, bool)

GetMapPath returns the value deep inside a hierahically nested map structure eg: x := langext.H{"K1": langext.H{"K2": 665}} GetMapPath[int](x, "K1.K2") == 665

func TryCast

func TryCast[T any](v any) (T, bool)

TryCast works similar to `v2, ok := v.(T)` Except it works through type alias'

func TryCastType

func TryCastType(v any, dest reflect.Type) (any, bool)

func Underlying

func Underlying(t reflect.Type) (ret reflect.Type)

Underlying returns the underlying type of t (without type alias)

https://github.com/golang/go/issues/39574#issuecomment-655664772

Types

type AccessStructOpt added in v0.0.479

type AccessStructOpt struct {
	ReturnNilOnMissingFields          bool    // return nil (instead of error) when a field in the path is missing (aka the supplied path is wrong)
	ReturnNilOnNilPtrFields           bool    // return nil (instead of error) when a field in the path is nil
	ReturnNilOnWrongFinalFieldType    bool    // return nil (instead of error) when the (final) field is not of the requested generic type
	ReturnNilOnWrongIntermedFieldType bool    // return nil (instead of error) when the intermediate field has an invalid type
	ReturnNilOnInvalidArrayIndizes    bool    // return nil (instead of error) when trying to acces an array with an invalid index (not a number or out of range)
	ReturnNilOnMissingMapKeys         bool    // return nil (instead of error) when trying to access a map with a missing key
	UsedTagForKeys                    *string // Use this tag for key names in the struct (instead of the StructField.Name)
	PreventArrayAccess                bool    // do not access array indizes - throw an error instead
	PreventMapAccess                  bool    // do not access maps - throw an error instead
}

type ConvertStructToMapOpt added in v0.0.416

type ConvertStructToMapOpt struct {
	KeepJsonMarshalTypes bool
	MaxDepth             *int
}

type PrimitiveStringSerializer

type PrimitiveStringSerializer struct{}

PrimitiveStringSerializer is used to serialize primitive types (and a few more) from and to string This is not really intended to be user facing, and more as a simple building block for other mechanisms supports:

  • golang primitives (ints, uints, floats, bool, string)
  • type aliases
  • time.Time
  • primitive.ObjectID

func (PrimitiveStringSerializer) ValueFromString

func (pss PrimitiveStringSerializer) ValueFromString(str string, outType reflect.Type) (any, error)

func (PrimitiveStringSerializer) ValueToString

func (pss PrimitiveStringSerializer) ValueToString(v any) (string, error)

Jump to

Keyboard shortcuts

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