reflection

package
v1.167.0 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2026 License: Apache-2.0 Imports: 6 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 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 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 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 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

This section is empty.

Jump to

Keyboard shortcuts

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