reflects

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

README

Reflects

reflects Provide extends reflect util functions.

  • some features

Install

go get github.com/zhangyiming748/pretty/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

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 ConvSlice

func ConvSlice(oldSlRv reflect.Value, newElemTyp reflect.Type) (rv reflect.Value, err error)

ConvSlice make new type slice from old slice

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

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

func HasChild(v reflect.Value) bool

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

func Indirect

func Indirect(v reflect.Value) reflect.Value

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

func IsArrayOrSlice

func IsArrayOrSlice(k reflect.Kind) bool

IsArrayOrSlice check. eg: array, slice

func IsEmpty

func IsEmpty(v reflect.Value) bool

IsEmpty reflect value check

func IsEmptyValue

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

func IsEqual(src, dst any) bool

IsEqual determines if two objects are considered equal.

TIP: cannot compare function type

func IsFunc

func IsFunc(val any) bool

IsFunc value

func IsNil

func IsNil(v reflect.Value) bool

IsNil reflect value

func Len

func Len(v reflect.Value) int

Len get reflect value length

func SetValue

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

SetValue to a reflect.Value

func SliceSubKind

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

func String(rv reflect.Value) string

String convert

func ToString

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

ToString convert

func ValToString

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

ValToString convert handle

func ValueByKind

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

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

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

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

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

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

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

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