reflectx

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FunctionName

func FunctionName(fn any) string

func IsFunction

func IsFunction(fn any) bool

func IsRefinedType

func IsRefinedType[R any](value reflect.Type) bool

func IsStructMethod

func IsStructMethod(f any) bool

IsStructMethod checks if the provided value is a method expression (e.g., (*Type).Method or Type.Method). It distinguishes between method expressions and regular functions that take struct parameters.

A method expression is created by qualifying a method with a type name, like (*T).Method or T.Method. This is different from: - Regular functions that take struct parameters - Method values (like t.Method where t is an instance) - Anonymous functions

The function works by: 1. Verifying the input is a function with at least one parameter 2. Checking that the first parameter is a struct type (or pointer to struct) 3. Using regex to identify method expressions and extract the type name 4. Comparing the extracted type name with the actual struct type

func IsZero added in v0.0.10

func IsZero(v any) bool

IsZero checks if a value is the zero value for its type. It handles nil values, pointers, interfaces, and all other types. Returns true if the value is zero or nil, false otherwise.

func ResultImplements added in v0.1.1

func ResultImplements[T any](function interface{}) bool

ResultImplements checks if any of the result arguments of a function implements the given interface type T. This is particularly useful for runtime type checking and reflection-based functionality where you need to verify function return types against interfaces.

Design choices:

  • Generic implementation allows compile-time type safety for the interface type
  • Accepts both function values and reflect.Type for flexibility
  • Returns false for nil or non-function inputs rather than panicking
  • Uses reflect.TypeOf(&zero).Elem() pattern to get interface type to handle both concrete and interface types

Example usage:

type Stringer interface { String() string }

func returnsStringer() fmt.Stringer { return nil }
func returnsMultiple() (int, fmt.Stringer, error) { return 0, nil, nil }

ResultImplements[fmt.Stringer](returnsStringer)    // returns true
ResultImplements[fmt.Stringer](returnsMultiple)    // returns true
ResultImplements[error](returnsMultiple)           // returns true
ResultImplements[fmt.Stringer](func() int { })     // returns false

Parameters:

  • function: Either a function value or reflect.Type of a function. If nil or not a function, returns false.

Returns:

  • bool: true if any result argument implements interface T, false otherwise

Types

This section is empty.

Jump to

Keyboard shortcuts

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