Documentation
¶
Index ¶
- Constants
- func BaseTypeOf(i any) reflect.Type
- func BaseValue(v reflect.Value) reflect.Value
- func BaseValueOf(i any) reflect.Value
- func Cast[T any](v any) (T, bool)
- func Clone(value reflect.Value) reflect.Value
- func Compare[T reflect.Value | any](a, b T) (int, error)
- func DerefType(T reflect.Type) (_ reflect.Type, depth int)
- func Equal(x, y any) bool
- func FullyQualifiedName(v any) string
- func IsBuiltInType(typ reflect.Type) bool
- func IsEmpty(val reflect.Value) bool
- func IsMutableType(typ reflect.Type) bool
- func IsNil(val reflect.Value) bool
- func IsNilable[T reflect.Kind | reflect.Value](v T) bool
- func IsZero(val reflect.Value) bool
- func Link(src, ptr any) (err error)
- func LookupField[FieldID LookupFieldID](rStruct reflect.Value, i FieldID) (reflect.StructField, reflect.Value, bool)
- func OverMap(rMap reflect.Value) iter.Seq2[reflect.Value, reflect.Value]
- func OverSlice(rSlice reflect.Value) iter.Seq2[int, reflect.Value]
- func OverStruct(rStruct reflect.Value) iter.Seq2[reflect.StructField, reflect.Value]
- func PointerOf(value reflect.Value) reflect.Value
- func RegisterEqual[T any](fn func(x, y T) bool) struct{}
- func SetValue(variable, value reflect.Value)
- func SymbolicName(v any) string
- func ToSettable(rv reflect.Value) (_ reflect.Value, ok bool)
- func ToType(T any) reflect.Type
- func ToValue(v any) reflect.Value
- func TypeOf[T any](i ...T) reflect.Type
- type CmpComparable
- type Comparable
- type LookupFieldID
- type TagHandler
Examples ¶
Constants ¶
const ErrInvalid errorkit.Error = "ErrInvalid"
const ErrNotComparable errorkit.Error = "ErrNotComparable"
const ErrTypeMismatch = internal.ErrTypeMismatch
Variables ¶
This section is empty.
Functions ¶
func BaseTypeOf ¶
func BaseValueOf ¶
func Compare ¶ added in v0.288.0
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 Equal ¶
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 ¶
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 IsMutableType ¶ added in v0.280.0
func Link ¶
Link will make destination interface be linked with the src value.
Example ¶
package main import ( "go.llib.dev/frameless/pkg/reflectkit" ) func main() { var src = Example{Name: "Panda"} var dest Example if err := reflectkit.Link(src, &dest); err != nil { // handle err } } type Example struct { Name string }
Output:
func LookupField ¶ added in v0.278.0
func LookupField[FieldID LookupFieldID](rStruct reflect.Value, i FieldID) (reflect.StructField, reflect.Value, bool)
func OverStruct ¶ added in v0.281.0
func RegisterEqual ¶
func SymbolicName ¶
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
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:
Types ¶
type CmpComparable ¶ added in v0.288.0
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)