Documentation ¶
Index ¶
- func CloneType(t reflect.Type, replace map[string]reflect.Type) (reflect.Type, error)
- func Get(v reflect.Value, key string) (reflect.Value, error)
- func GetInt64Value(field reflect.Value) int64
- func GetStringValue(field reflect.Value) string
- func SetInt64(field reflect.Value, value int64)
- func SetPointerInt64(field reflect.Value, value *int64)
- func SetPointerString(field reflect.Value, value *string)
- func SetPointerTime(field reflect.Value, value *time.Time)
- func SetString(field reflect.Value, value string)
- func SetValue(tgt reflect.Value, src interface{}) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CloneType ¶
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 ¶
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 GetStringValue ¶
func SetPointerInt64 ¶
func SetPointerString ¶
Types ¶
This section is empty.