Documentation
¶
Overview ¶
* Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. * SPDX-License-Identifier: Apache-2.0
Index ¶
- func Converter(converter IValueConverter, to reflect.Type, value any) (any, error)
- 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 NewValueTypeConverter(converter IValueConverter) valueUtils.IValueConverter
- 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 StructPropertyHasTag(rv reflect.Value, key, tagName string) bool
- func StructPropertyHasTagValue(rv reflect.Value, key, tagName, expectedValue string) bool
- func StructPropertyNames(rt reflect.Type) []string
- func StructPropertyValue(rv reflect.Value, key string) (reflect.Value, bool)
- func StructTypeHasFieldPropertyName(rt reflect.Type, key, propertyName string) bool
- func StructTypeHasFieldTag(rt reflect.Type, key, tagName string) bool
- func StructTypeHasFieldTagValue(rt reflect.Type, key, tagName, expectedValue string) bool
- func ToStructPtr(obj reflect.Value) (val any, err error)
- type IValueConverter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Converter ¶ added in v1.170.0
Converter resolves the runtime source type for value and applies the provided converter.
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 NewValueTypeConverter ¶ added in v1.170.0
func NewValueTypeConverter(converter IValueConverter) valueUtils.IValueConverter
NewValueTypeConverter adapts a reflection-style converter into a plain value converter.
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 StructPropertyHasTag ¶ added in v1.170.0
StructPropertyHasTag reports whether the exported field identified by key on rv defines tagName.
Concept: use this when you have a runtime value rather than a type and want to check only whether a tag key exists. Collection values return true when any contained item resolves to a matching struct field.
rv may be a struct value, a non-nil pointer/interface resolving to a struct value, or a slice/array/map whose items are checked until any matching field is found.
Example:
ok := StructPropertyHasTag(reflect.ValueOf(cfg), "password", "mask")
func StructPropertyHasTagValue ¶ added in v1.170.0
StructPropertyHasTagValue reports whether the exported field identified by key on rv defines tagName with expectedValue.
Concept: use this when you have a runtime value rather than a type and want to check a specific tag key/value pair. Collection values return true when any contained item resolves to a matching struct field.
rv may be a struct value, a non-nil pointer/interface resolving to a struct value, or a slice/array/map whose items are checked until any matching field is found.
Example:
ok := StructPropertyHasTagValue(reflect.ValueOf(cfg), "password", "mask", "redact")
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")
func StructTypeHasFieldPropertyName ¶ added in v1.170.0
StructTypeHasFieldPropertyName reports whether the exported field identified by key is exposed under propertyName through either its `json` or `yaml` tag.
Concept: use this when you want to check the external serialised property name of a field without naming a specific tag key.
The field may be identified by its Go field name or by an existing `json` or `yaml` property name.
Example:
ok := StructTypeHasFieldPropertyName(reflect.TypeOf(cfg), "Password", "password")
func StructTypeHasFieldTag ¶ added in v1.170.0
StructTypeHasFieldTag reports whether the exported field identified by key on rt defines tagName.
Concept: use this when you only care whether a tag key exists on a field, regardless of its value.
The field may be identified by its Go field name or by a property name taken from common serialisation tags such as `json` and `yaml`, for example the property name `name` resolving the field tagged `json:"name,omitempty"`.
Example:
ok := StructTypeHasFieldTag(reflect.TypeOf(cfg), "Password", "mask")
func StructTypeHasFieldTagValue ¶ added in v1.170.0
StructTypeHasFieldTagValue reports whether the exported field identified by key on rt defines tagName with expectedValue.
Concept: use this when you have a struct type and want to check a specific tag key/value pair on one of its exported fields, for example whether a field has `mask:"redact"`.
The field may be identified by its Go field name or by a property name taken from common serialisation tags such as `json` and `yaml`, for example the property name `name` resolving the field tagged `json:"name,omitempty"`.
Example:
ok := StructTypeHasFieldTagValue(reflect.TypeOf(cfg), "Password", "mask", "redact")