Documentation
¶
Overview ¶
* Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. * SPDX-License-Identifier: Apache-2.0
Index ¶
- func GetStructField(structure any, fieldName string) (any, bool)
- func GetStructureField(field reflect.Value) any
- func GetUnexportedStructureField(structure any, fieldName string) any
- func InheritsFrom(object any, parentType reflect.Type) bool
- func IsEmpty(value any) bool
- func IsNilInterface(i any) bool
- func IsNotEmpty(value any) bool
- func MapLookupKey(keyType reflect.Type, key string) (reflect.Value, bool)
- func MapPropertyValue(rv reflect.Value, key string) (reflect.Value, bool)
- func SetStructField(structure any, fieldName string, value any) error
- func SetStructureField(field reflect.Value, value any)
- func SetUnexportedStructureField(structure any, fieldName string, value any)
- func StructFieldByPropertyName(rt reflect.Type, key string) (reflect.StructField, bool)
- func StructPropertyNames(rt reflect.Type) []string
- func StructPropertyValue(rv reflect.Value, key string) (reflect.Value, bool)
- func ToStructPtr(obj reflect.Value) (val any, err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetStructField ¶
GetStructField checks if the given structure has a given field. The structure should be passed by reference. It returns an interface and a boolean, the field's content and a boolean denoting whether or not the field exists. If the boolean is false then there is no such field on the structure. If the boolean is true but the interface stores "" then the field exists but is not set. If the boolean is true and the interface is not empty, the field exists and is set.
func GetStructureField ¶
func InheritsFrom ¶
InheritsFrom uses reflection to find if a struct "inherits" from a certain type. In other words it checks whether the struct embeds a struct of that type.
func IsEmpty ¶ added in v1.4.2
IsEmpty checks whether a value is empty i.e. "", nil, 0, [], {}, false, etc. For Strings, a string is considered empty if it is "" or if it only contains whitespaces
func IsNilInterface ¶ added in v1.158.0
IsNilInterface checks whether an interface value is nil even when it has been passed around as `any`.
func IsNotEmpty ¶ added in v1.161.0
IsNotEmpty checks whether a value is not empty. See IsEmpty for more details about what is considered empty.
func MapLookupKey ¶ added in v1.167.0
MapLookupKey converts a string property name into a reflected map key value when the map key type is directly compatible with strings.
Interface-typed maps may still require a fallback scan of existing keys when the stored dynamic key type is a named string type or another string-like implementation such as fmt.Stringer. See MapPropertyValue.
func MapPropertyValue ¶ added in v1.167.0
MapPropertyValue returns the value stored under key when rv is a map whose key type can be matched safely from the supplied string key.
It supports maps keyed by strings, named string types, and interface key types whose stored key values either are strings or implement fmt.Stringer.
The returned value is the reflected map element. The `found` flag reports whether a matching key exists and the element can be accessed safely.
Example:
value, found := MapPropertyValue(reflect.ValueOf(map[string]any{"name": "alice"}), "name")
func SetStructField ¶
SetStructField attempts to set a field of a structure to the given value It returns nil or an error, in case the field doesn't exist on the structure or the value and the field have different types
func SetStructureField ¶
func StructFieldByPropertyName ¶ added in v1.167.0
StructFieldByPropertyName resolves key to an exported struct field using the Go field name first and then the `json` tag name when present.
Example:
field, found := StructFieldByPropertyName(reflect.TypeOf(cfg), "name")
func StructPropertyNames ¶ added in v1.167.0
StructPropertyNames returns the exported property names exposed by rt using `json` tag names when present and Go field names otherwise.
Example:
names := StructPropertyNames(reflect.TypeOf(cfg))
func StructPropertyValue ¶ added in v1.167.0
StructPropertyValue returns the exported struct property named key when it can be accessed safely without panicking.
The property name may be either the Go field name or the `json` tag name when one is defined.
The returned value is the reflected field value. The `found` flag reports whether a matching exported field exists and can be interfaced safely.
Example:
value, found := StructPropertyValue(reflect.ValueOf(cfg), "name")
Types ¶
This section is empty.