reflection

package
v1.216.0 Latest Latest
Warning

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

Go to latest
Published: May 17, 2024 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloneType

func CloneType(t reflect.Type, replace map[string]reflect.Type) (reflect.Type, error)

CloneType() clones a type into a new type, replacing some elements of it Paramters:

t is the existing type to be cloned
replace is a list of items to replace with their new types
		use jq key notation
		if this is empty map/nil, it will clone the type without changes

Return:

cloned type or error

Example:

newType,err := reflection.CloneType(
	reflect.TypeOf(myStruct{}),
	map[string]reflect.Type{
		".field1": reflect.TypeOf(float64(0)),
		".list[].name": reflect.TypeOf(int64(0)),
	},
)

This is not a function you will use everyday, but very useful when needed See example usage in search to read OpenSearch responses with the correct struct type used to parse ".hits.hits[]._source" so that we do not have to unmarshal the JSON, then marshal each _source from map[string]interface{} back to json then unmarshal again into the correct type! It saves a lot of overhead CPU doing it all at once using the correct type nested deep into the response body type.

this function was written for above case... it will likely need extension if we want to replace all kinds of other fields, but it meets the current requirements.

After parsing, use reflection.Get() with the same key notation to get the result from the nested document.

Note current shortcoming: partial matching will apply if you have two similarly named fields, e.g. "name" and "name2", then replace instruction on "name" may partial match name2. To fix this, we need better function than strings.HasPrefix() to check for delimiter/end of name to do full word matches, and we need to extend the test to illustrate this.

func Get

func Get(v reflect.Value, key string) (reflect.Value, error)

Get() a jq-named element from a value e.g. get(reflect.ValueOf(myDoc), ".hits.hits[]._source") the result is an array of _source items which may be just one field inside the hits object in the list

see usage in search.TimeSeries.Search() to get documents from the OpenSearch response structure that is very nested and parse using a runtime-created reflect Type, i.e. one cannot get it simply by iterating over res.Hits.[]Hits...

func GetInt64Value

func GetInt64Value(field reflect.Value) int64

func GetStringValue

func GetStringValue(field reflect.Value) string

func SetInt64Field added in v1.136.0

func SetInt64Field(field reflect.Value, value int64)

func SetStringField added in v1.136.0

func SetStringField(field reflect.Value, value string)

func SetTimeField added in v1.136.0

func SetTimeField(field reflect.Value, value time.Time)

func SetValue

func SetValue(tgt reflect.Value, src interface{}) error

Types

This section is empty.

Jump to

Keyboard shortcuts

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