reflectutil

package
v0.11.1 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2025 License: MIT Imports: 6 Imported by: 5

Documentation

Overview

Package reflectutil provides utility functions for the reflect package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compare added in v0.7.7

func Compare(a, b reflect.Value) int

Compare compares two values of the same type.

func ConvertValueCanInterface added in v0.6.0

func ConvertValueCanInterface(v reflect.Value) (reflect.Value, bool)

ConvertValueCanInterface attempts to converts a reflect.Value so it can be used with reflect.Value.Interface.

The returned boolean indicates if the conversion was successful.

If the conversion was successful, the returned reflect.Value can be used with reflect.Value.Interface. If the conversion was not successful, the returned reflect.Value is the same as the input reflect.Value.

func GetCompareFunc added in v0.7.7

func GetCompareFunc(typ reflect.Type) func(a, b reflect.Value) int

GetCompareFunc returns a function that compares two values of the given type.

It panics if the type is not supported.

func TryValueInterface added in v0.6.0

func TryValueInterface(v reflect.Value) (any, bool)

TryValueInterface calls ConvertValueCanInterface and reflect.Value.Interface.

func TryValueInterfaceFor added in v0.6.0

func TryValueInterfaceFor[T any](v reflect.Value) (T, bool)

TryValueInterfaceFor calls ConvertValueCanInterface and ValueInterfaceFor.

func TypeFullName

func TypeFullName(typ reflect.Type) string

TypeFullName returns the full name of the type.

It contains the full package path if the type is defined in a package.

func TypeFullNameFor

func TypeFullNameFor[T any]() string

TypeFullNameFor returns the full name of type parameter.

func ValueInterfaceFor added in v0.6.0

func ValueInterfaceFor[T any](v reflect.Value) T

ValueInterfaceFor calls reflect.Value.Interface and returns the result as the specified type.

The caller must ensure that the type is correct.

Types

type MapEntries added in v0.7.7

type MapEntries []MapEntry

MapEntries is a slice of MapEntry.

func GetMapEntries added in v0.7.7

func GetMapEntries(m reflect.Value) MapEntries

GetMapEntries returns the entries of a map.

func GetSortedMap added in v0.7.7

func GetSortedMap(m reflect.Value) MapEntries

GetSortedMap returns a sorted MapEntries of the given map.

func (MapEntries) Release added in v0.7.7

func (es MapEntries) Release()

Release releases the MapEntries.

It helps to reduce memory allocations.

type MapEntry added in v0.7.7

type MapEntry struct {
	Key   reflect.Value
	Value reflect.Value
}

MapEntry represents a key-value pair in a map.

Jump to

Keyboard shortcuts

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