reflectkit

package
v0.284.0 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2025 License: Apache-2.0 Imports: 13 Imported by: 1

Documentation

Index

Examples

Constants

View Source
const ErrInvalid errorkit.Error = "ErrInvalid"
View Source
const ErrTypeMismatch errorkit.Error = "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 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
}

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
}

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 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
}

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
}

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 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
	Parse func(sf reflect.StructField, tagValue string) (T, error)
	Use   func(sf reflect.StructField, field 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
	// 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