reflectkit

package
v0.296.0 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2025 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const ErrInvalid errorkit.Error = "ErrInvalid"
View Source
const ErrNotComparable errorkit.Error = "ErrNotComparable"
View Source
const ErrTypeMismatch = internal.ErrTypeMismatch

Variables

This section is empty.

Functions

func BaseTypeOf

func BaseTypeOf(i any) reflect.Type

func BaseValue

func BaseValue(v reflect.Value) reflect.Value

func BaseValueOf

func BaseValueOf(i any) reflect.Value

func Cast

func Cast[T any](v any) (T, bool)

func Clone added in v0.280.0

func Clone(value reflect.Value) reflect.Value

Clone recursively creates a deep copy of a reflect.Value

func Compare added in v0.288.0

func Compare[T reflect.Value | any](a, b T) (int, error)

Compare will compare "a" and "b" and return the comparison result.

  • -1 if "a" is smaller than "b"
  • 0 if "a" is equal to "b"
  • 1 if "a" is bigger than "b"

func DerefType added in v0.274.0

func DerefType(T reflect.Type) (_ reflect.Type, depth int)

func Equal

func Equal(x, y any) bool

Equal will compare two value deeply. It looks for Equal method on the value as well. You can define how a type's value should be compared by using RegisterEqual.

Example
package main

import (
	"go.llib.dev/frameless/pkg/reflectkit"
)

func main() {
	reflectkit.Equal("a", "a") // true
	reflectkit.Equal("a", "b") // false
}
Output:

func FullyQualifiedName

func FullyQualifiedName(v any) string
Example
package main

import (
	"fmt"

	"go.llib.dev/frameless/pkg/reflectkit"
)

func main() {
	fmt.Println(reflectkit.FullyQualifiedName(Example{}))
}

type Example struct {
	Name string
}
Output:

func IsBuiltInType added in v0.287.0

func IsBuiltInType(typ reflect.Type) bool

func IsEmpty added in v0.207.0

func IsEmpty(val reflect.Value) bool

func IsMutableType added in v0.280.0

func IsMutableType(typ reflect.Type) bool

func IsNil added in v0.278.0

func IsNil(val reflect.Value) bool

func IsNilable added in v0.290.0

func IsNilable[T reflect.Kind | reflect.Value](v T) bool

func IsZero added in v0.217.0

func IsZero(val reflect.Value) bool
func Link(src, ptr any) (err error)

Link will make destination interface be linked with the src value.

func LookupField added in v0.278.0

func LookupField[FieldID LookupFieldID](rStruct reflect.Value, i FieldID) (reflect.StructField, reflect.Value, bool)

func OverMap added in v0.281.0

func OverMap(rMap reflect.Value) iter.Seq2[reflect.Value, reflect.Value]

func OverSlice added in v0.281.0

func OverSlice(rSlice reflect.Value) iter.Seq2[int, reflect.Value]

func OverStruct added in v0.281.0

func OverStruct(rStruct reflect.Value) iter.Seq2[reflect.StructField, reflect.Value]

func PointerOf added in v0.181.0

func PointerOf(value reflect.Value) reflect.Value

func RegisterEqual

func RegisterEqual[T any](fn func(x, y T) bool) struct{}

func SetValue

func SetValue(variable, value reflect.Value)

SetValue will force set

func SymbolicName

func SymbolicName(v any) string
Example
package main

import (
	"fmt"

	"go.llib.dev/frameless/pkg/reflectkit"
)

func main() {
	fmt.Println(reflectkit.SymbolicName(Example{}))
}

type Example struct {
	Name string
}
Output:

func ToSettable added in v0.278.0

func ToSettable(rv reflect.Value) (_ reflect.Value, ok bool)
Example
package main

import (
	"reflect"

	"go.llib.dev/frameless/pkg/reflectkit"
)

func main() {
	type T struct{ v int }
	var v T
	_ = v.v

	ptr := reflect.ValueOf(&v)
	rStruct := ptr.Elem()

	got, ok := reflectkit.ToSettable(rStruct.FieldByName("v"))
	_, _ = got, ok
}
Output:

func ToType added in v0.290.0

func ToType(T any) reflect.Type

func ToValue added in v0.261.0

func ToValue(v any) reflect.Value

func TypeOf added in v0.180.0

func TypeOf[T any](i ...T) reflect.Type

Types

type CmpComparable added in v0.288.0

type CmpComparable[T any] interface {
	Cmp(T) int
}

type Comparable added in v0.288.0

type Comparable[T any] interface {
	// Compare returns:
	//   -1 if receiver is less than the argument,
	//    0 if they're equal, and
	//    +1 if receiver is greater.
	//
	// Implementors must ensure consistent ordering semantics.
	Compare(T) int
}

Comparable is an optional interface type that allows implementing types to perform value comparisons.

Types implementing this interface must provide a Compare method that defines the ordering or equivalence of values. This pattern is useful when working with: 1. Custom user-defined types requiring comparison logic 2. Encapsulated values needing semantic comparisons 3. Comparison-agnostic systems (e.g., sorting algorithms)

Example usage:

type MyNumber struct {
    Value int
}

func (m MyNumber) Compare(other MyNumber) int {
    if m.Value < other.Value { return -1 }
    if m.Value > other.Value { return +1 }
    return 0
}

type LookupFieldID added in v0.280.0

type LookupFieldID interface {
	reflect.StructField | int | string
}

type TagHandler added in v0.280.0

type TagHandler[T any] struct {
	Name string
	// Alias is a list of optional tag alias that will be checked if Name is not avaialble.
	Alias []string
	// Parse meant to interpret the content of a raw tag and converts it into a tag value.
	//
	// Ideally, parsing occurs only once, provided the tag value remains immutable.
	// If ForceCache is enabled, a successful Parse is guaranteed to run only once.
	//
	// If an unrecoverable error occurs during parsing, such as invalid tag format,
	// consider either panicking or enabling PanicOnParseError to handle the failure.
	Parse func(field reflect.StructField, tagValue string) (T, error)
	// Use specifies what should be done with the parse tag value
	// while the tag is being applied on a given struct field.
	Use func(field reflect.StructField, value reflect.Value, v T) error
	// ForceCache will force the TagHandler to cache the parse results, regardless if the value is mutable or not.
	ForceCache bool
	// HandleUntagged will force the Handle functions to call Parse and Use even on fields where tag is empty.
	HandleUntagged bool
	// PanicOnParseError triggers a panic if a parsing error occurs.
	//
	// Enabling this flag enforces strict tag formatting,
	// as tags are determined at compile time, making runtime fixes impossible.
	PanicOnParseError bool
	// contains filtered or unexported fields
}

func (*TagHandler[T]) HandleStruct added in v0.280.0

func (h *TagHandler[T]) HandleStruct(rStruct reflect.Value) error

func (*TagHandler[T]) HandleStructField added in v0.280.0

func (h *TagHandler[T]) HandleStructField(field reflect.StructField, value reflect.Value) error

func (*TagHandler[T]) LookupTag added in v0.280.0

func (h *TagHandler[T]) LookupTag(field reflect.StructField) (T, bool, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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