reflection

package
v1.170.4 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2026 License: Apache-2.0 Imports: 7 Imported by: 7

Documentation

Overview

* Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. * SPDX-License-Identifier: Apache-2.0

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Converter added in v1.170.0

func Converter(converter IValueConverter, to reflect.Type, value any) (any, error)

Converter resolves the runtime source type for value and applies the provided converter.

func GetStructField

func GetStructField(structure any, fieldName string) (any, bool)

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 GetStructureField(field reflect.Value) any

func GetUnexportedStructureField

func GetUnexportedStructureField(structure any, fieldName string) any

func InheritsFrom

func InheritsFrom(object any, parentType reflect.Type) bool

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

func IsEmpty(value any) bool

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

func IsNilInterface(i any) bool

IsNilInterface checks whether an interface value is nil even when it has been passed around as `any`.

func IsNotEmpty added in v1.161.0

func IsNotEmpty(value any) bool

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

func MapLookupKey(keyType reflect.Type, key string) (reflect.Value, bool)

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

func MapPropertyValue(rv reflect.Value, key string) (reflect.Value, bool)

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

func SetStructField(structure any, fieldName string, value any) error

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 SetStructureField(field reflect.Value, value any)

func SetUnexportedStructureField

func SetUnexportedStructureField(structure any, fieldName string, value any)

func StructFieldByPropertyName added in v1.167.0

func StructFieldByPropertyName(rt reflect.Type, key string) (reflect.StructField, bool)

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

func StructPropertyHasTag(rv reflect.Value, key, tagName string) bool

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

func StructPropertyHasTagValue(rv reflect.Value, key, tagName, expectedValue string) bool

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

func StructPropertyNames(rt reflect.Type) []string

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

func StructPropertyValue(rv reflect.Value, key string) (reflect.Value, bool)

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

func StructTypeHasFieldPropertyName(rt reflect.Type, key, propertyName string) bool

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

func StructTypeHasFieldTag(rt reflect.Type, key, tagName string) bool

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

func StructTypeHasFieldTagValue(rt reflect.Type, key, tagName, expectedValue string) bool

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")

func ToStructPtr added in v1.8.0

func ToStructPtr(obj reflect.Value) (val any, err error)

ToStructPtr returns an instance of the pointer (interface) to the object obj.

Types

type IValueConverter added in v1.170.0

type IValueConverter func(reflect.Type, reflect.Type, any) (any, error)

IValueConverter transforms a value from its runtime source type into a new target type.

Jump to

Keyboard shortcuts

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