reflects

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2022 License: MIT Imports: 8 Imported by: 12

README

Reflects

reflects Provide extends reflect util functions.

  • some features

Install

go get github.com/gookit/goutil/reflects

Go docs

Usage

Testings

go test -v ./reflects/...

Test limit by regexp:

go test -v -run ^TestSetByKeys ./reflects/...

Documentation

Overview

Package reflects Provide extends reflect util functions.

Index

Constants

View Source
const (
	// Int for all intX types
	Int = BKind(reflect.Int)
	// Uint for all uintX types
	Uint = BKind(reflect.Uint)
	// Float for all floatX types
	Float = BKind(reflect.Float32)
	// Array for array,slice types
	Array = BKind(reflect.Array)
	// Complex for all complexX types
	Complex = BKind(reflect.Complex64)
)

base kinds

Variables

This section is empty.

Functions

func BaseTypeVal added in v0.5.10

func BaseTypeVal(v reflect.Value) (value any, err error)

BaseTypeVal convert custom type or intX,uintX,floatX to generic base type.

intX/unitX 	=> int64
floatX      => float64
string 	    => string

returns int64,string,float or error

func Elem

func Elem(v reflect.Value) reflect.Value

Elem returns the value that the interface v contains or that the pointer v points to.

func FlatMap added in v0.5.12

func FlatMap(rv reflect.Value, fn FlatFunc)

FlatMap process tree map to flat key-value map.

Examples:

{"top": {"sub": "value", "sub2": "value2"} }
->
{"top.sub": "value", "top.sub2": "value2" }

func HasChild added in v0.5.6

func HasChild(v reflect.Value) bool

HasChild check. eg: array, slice, map, struct

func Indirect added in v0.5.12

func Indirect(v reflect.Value) reflect.Value

Indirect like reflect.Indirect(), but can also indirect reflect.Interface

func IsEmpty added in v0.5.6

func IsEmpty(v reflect.Value) bool

IsEmpty reflect value check

func IsEmptyValue added in v0.5.8

func IsEmptyValue(v reflect.Value) bool

IsEmptyValue reflect value check. Difference the IsEmpty(), if value is ptr, will check real elem.

From src/pkg/encoding/json/encode.go.

func IsEqual added in v0.5.6

func IsEqual(src, dst any) bool

IsEqual determines if two objects are considered equal.

TIP: cannot compare function type

func IsFunc added in v0.5.6

func IsFunc(val any) bool

IsFunc value

func IsNil added in v0.5.6

func IsNil(v reflect.Value) bool

IsNil reflect value

func Len added in v0.5.6

func Len(v reflect.Value) int

Len get reflect value length

func SetValue added in v0.5.11

func SetValue(rv reflect.Value, val any) error

SetValue to a reflect.Value

func SliceSubKind added in v0.5.8

func SliceSubKind(typ reflect.Type) reflect.Kind

SliceSubKind get sub-elem kind of the array, slice, variadic-var.

Usage:

SliceSubKind(reflect.TypeOf([]string{"abc"})) // reflect.String

func String added in v0.5.12

func String(rv reflect.Value) string

String convert

func ToString added in v0.5.12

func ToString(rv reflect.Value) (str string, err error)

ToString convert

func ValToString added in v0.5.12

func ValToString(rv reflect.Value, defaultAsErr bool) (str string, err error)

ValToString convert handle

func ValueByKind added in v0.5.10

func ValueByKind(val any, kind reflect.Kind) (rv reflect.Value, err error)

ValueByKind create reflect.Value by give reflect.Kind

TIPs:

Only support kind: string, bool, intX, uintX, floatX

func ValueByType added in v0.5.11

func ValueByType(val any, typ reflect.Type) (rv reflect.Value, err error)

ValueByType create reflect.Value by give reflect.Type

Types

type BKind

type BKind uint

BKind base data kind type

func ToBKind added in v0.5.6

func ToBKind(kind reflect.Kind) BKind

ToBKind convert reflect.Kind to base kind

func ToBaseKind

func ToBaseKind(kind reflect.Kind) BKind

ToBaseKind convert reflect.Kind to base kind

type FlatFunc added in v0.5.12

type FlatFunc func(path string, val reflect.Value)

FlatFunc custom collect handle func

type Type

type Type interface {
	reflect.Type
	// BaseKind value
	BaseKind() BKind
}

Type struct

func TypeOf

func TypeOf(v any) Type

TypeOf value

type Value

type Value struct {
	reflect.Value
	// contains filtered or unexported fields
}

Value struct

func ValueOf

func ValueOf(v any) Value

ValueOf the give value

func Wrap

func Wrap(rv reflect.Value) Value

Wrap the give value

func (Value) BKind added in v0.5.6

func (v Value) BKind() BKind

BKind value

func (Value) BaseKind

func (v Value) BaseKind() BKind

BaseKind value

func (Value) Elem

func (v Value) Elem() Value

Elem returns the value that the interface v contains or that the pointer v points to.

TIP: not like reflect.Value.Elem. otherwise, will return self.

func (Value) HasChild added in v0.5.6

func (v Value) HasChild() bool

HasChild check. eg: array, slice, map, struct

func (Value) Indirect

func (v Value) Indirect() Value

Indirect value. alias of the reflect.Indirect()

func (Value) Int added in v0.5.6

func (v Value) Int() int64

Int value. if is uintX will convert to int64

func (Value) Type

func (v Value) Type() Type

Type of value.

func (Value) Uint added in v0.5.6

func (v Value) Uint() uint64

Uint value. if is intX will convert to uint64

Jump to

Keyboard shortcuts

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