reflect_utils

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2026 License: Apache-2.0 Imports: 4 Imported by: 3

README

reflect_utils

reflect_utils is a Go library providing a collection of utility functions to simplify working with the reflect package. It offers helpers for instantiation, type checking, pointer manipulation, and struct tag handling.

Installation

go get github.com/zauberhaus/reflect_utils

Features

Instantiation
  • NewT any (T, error): Creates a new instance of type T.
  • NewOf(t reflect.Type) (any, error): Creates a new instance of the given reflect.Type. Handles pointers, slices, maps, structs, and interfaces (specifically context.Context).
  • NewWithDefaultsOf(t reflect.Type) (any, error): Creates a new instance of the given type and initializes fields based on default struct tags using github.com/creasty/defaults.
Type Checks & Inspection
  • IsPointer(value any) bool: Checks if a value, reflect.Value, or reflect.Type is a pointer.
  • IsStruct(value any) bool: Checks if a value is a struct (automatically dereferences pointers).
  • IsEnum(value any) bool: Checks if a type follows a specific enum convention (looks for an IsA<TypeName> method generated by github.com/dmarkham/enumer).
  • IsNil(value any) bool: Safely checks if a value is nil, handling typed nils for pointers, maps, channels, and slices.
  • IsEmpty(object any) bool: Checks if a value is "empty" (nil, zero value, or empty collection).
  • IsComparable(object any) bool: Checks if a value is comparable.
  • HasMethod(value any, method string) bool: Checks if a value has a method with the given name.
  • HasField(value any, field string) bool: Checks if a struct (or pointer to struct) has a field with the given name.
  • HasDefault(t reflect.Type) bool: Checks if a struct type contains fields with default tags.
Pointer & Memory Utilities
  • PtrT any *T: Returns a pointer to the given value.
  • CopyToHeap(val any) any: Copies a value to the heap. If the value is already a pointer, it returns it as is.
  • FromPointer(val any) any: Dereferences a value if it is a pointer; otherwise returns the value as is.

License

Copyright 2026 Zauberhaus.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyToHeap

func CopyToHeap(val any) any

func FromPointer

func FromPointer(val any) any

func HasDefault

func HasDefault(t reflect.Type) bool

func HasField

func HasField(value any, field string) bool

func HasMethod

func HasMethod(value any, method string) bool

func IsComparable

func IsComparable(object any) bool

func IsEmpty

func IsEmpty(object any) bool

func IsEnum

func IsEnum(value any) bool

func IsNil

func IsNil(value any) bool

func IsPointer

func IsPointer(value any) bool

func IsStruct

func IsStruct(value any) bool

func New

func New[T any](v ...T) (T, error)

func NewOf

func NewOf(t reflect.Type) (any, error)

func NewWithDefaultsOf

func NewWithDefaultsOf(t reflect.Type) (any, error)

func Ptr

func Ptr[T any](v T) *T

Types

This section is empty.

Jump to

Keyboard shortcuts

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