reflect

package
Version: v0.0.0-...-2778579 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2021 License: LGPL-3.0-or-later Imports: 2 Imported by: 1

README

package: github.com/mikelue/go-misc/utils GoDoc

This package contains some enhancements for manipulating reflect of GoLang.

reflect/

package: github.com/mikelue/go-misc/utils/reflect GoDoc

This package provides out-of-box methods to manipulate instances of reflect easily.

TypeExt - Some convenient methods to manipulate reflect.Type.

ValueExt - Some convenient methods to manipulate reflect.Value.

AnyValue - Some convenient methods to manipulate interface{}.

reflect/types

package: github.com/mikelue/go-misc/utils/reflect/types GoDoc

BasicTypes - Instance can be used directly without calling reflect.TypeOf(<type>) for builtin types provided by GoLang. This package provides instances of reflect.Type or reflect.Value for builtin types of GoLang.

PointerTypes - Instance can be used directly without calling reflect.PtrTo(<type>) for builtin types provided by GoLang.

SliceTypes - Instance can be used directly without calling reflect.SliceOf(<type>) for builtin types provided by GoLang.

ArrayTypes - Instance can be used directly without calling reflect.ArrayOf(<type>) for builtin types provided by GoLang.

import (
	"reflect"
	t "github.com/mikelue/go-misc/utils/reflect/types"
)

// New a pointer to "uint64"
pointerValueOfInt64 := reflect.New(t.BasicTypes.OfUint64())

Documentation

Overview

This package provides utilities to ease manipulation over objects in "reflect".

FuncInfo

As information keeper for meta-data of functions.

funcInfo := TypeExtBuilder(someFuncRef).FuncInfo()
funcInfo.InAsTypes()
funcInfo.OutAsTypes()

Type Extension

"TypeExt" is the extension of "reflect.Type"

// As type of "int32"
valueExt := TypeExtBuilder.NewByAny(int32(0))

TypeExtBuilder - See provided methods in "ITypeExtBuilder".

Value Extension

"ValueExt" is the extension of "reflect.Value".

// As value of int16(20)
valueExt := ValueExtBuilder.NewByAny(int16(20))

ValueExtBuilder - See provided methods in "IValueExtBuilder".

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FuncInfo

type FuncInfo struct {
	// contains filtered or unexported fields
}

Use the methods provided by this struct to access the meta-data.

Example (InTypes)
typeExt := TypeExtBuilder.NewByAny(SampleFunc)

fmt.Printf("In[%v]",
	typeExt.FuncInfo().InTypes()[0].Kind(),
)
Output:

In[int]
Example (OutTypes)
typeExt := TypeExtBuilder.NewByAny(SampleFunc)

fmt.Printf("Out[%v]",
	typeExt.FuncInfo().OutTypes()[0].Kind(),
)
Output:

Out[string]

func (*FuncInfo) InAsTypes

func (self *FuncInfo) InAsTypes() []reflect.Type

Gets the types of in params(as "[]reflect.Type")

See: "reflect.Type.In()"

func (*FuncInfo) InTypes

func (self *FuncInfo) InTypes() []*TypeExt

Gets the types of in params(as "[]*TypeExt")

See: "reflect.Type.In()"

func (*FuncInfo) OutAsTypes

func (self *FuncInfo) OutAsTypes() []reflect.Type

Gets the types of out params(as "[]*TypeExt")

See: "reflect.Type.Out()"

func (*FuncInfo) OutTypes

func (self *FuncInfo) OutTypes() []*TypeExt

Gets the types of out params(as "[]*TypeExt")

See: "reflect.Type.Out()"

type ITypeExtBuilder

type ITypeExtBuilder int
Example (NewByAny)
typeExt := TypeExtBuilder.NewByAny(20)
fmt.Printf("Type extension: %v", typeExt.Kind())
Output:

Type extension: int
Example (NewByType)
sampleString := "Hello"
sampleType := reflect.TypeOf(&sampleString)

typeExt := TypeExtBuilder.NewByType(sampleType)
fmt.Printf("Type extension: %v", typeExt.Kind())
Output:

Type extension: ptr
const TypeExtBuilder ITypeExtBuilder = 0

Collects functions used to construct "TypeExt", see "ITypeExtBuilder".

func (ITypeExtBuilder) NewByAny

func (self ITypeExtBuilder) NewByAny(v interface{}) *TypeExt

Constructs "TypeExt" by any interface("interface{}")

func (ITypeExtBuilder) NewByType

func (ITypeExtBuilder) NewByType(t reflect.Type) *TypeExt

Constructs "TypeExt" by "reflect.Type"

type IValueExtBuilder

type IValueExtBuilder int
Example (NewByAny)
valueExt := ValueExtBuilder.NewByAny(20)
fmt.Printf("Value: %v", valueExt.AsValue().Interface())
Output:

Value: 20
Example (NewByValue)
sampleValue := reflect.ValueOf("Hello")

valueExt := ValueExtBuilder.NewByValue(sampleValue)
fmt.Printf("Value: %v", valueExt.AsValue().Interface())
Output:

Value: Hello
const ValueExtBuilder IValueExtBuilder = 0

Collects functions used to construct "ValueExt", see "IValueExtBuilder".

func (IValueExtBuilder) NewByAny

func (self IValueExtBuilder) NewByAny(v interface{}) ValueExt

Constructs "ValueExt" by any interface("interface{}")

func (IValueExtBuilder) NewByValue

func (IValueExtBuilder) NewByValue(value reflect.Value) ValueExt

Constructs "ValueExt" by "reflect.Value"

type TypeExt

type TypeExt struct {
	// contains filtered or unexported fields
}

Holding the actual instance of "reflect.Type", see methods provided by this struct for features.

Example (RecursiveIndirect)
var u1 uint32 = 981
u1p := &u1
u1pp := &u1p

u1ppTypeExt := TypeExtBuilder.NewByAny(u1pp)

fmt.Printf("Type: %v. Leaf type: %v.",
	u1ppTypeExt.Kind(),
	u1ppTypeExt.RecursiveIndirect().Kind(),
)
Output:

Type: ptr. Leaf type: uint32.

func (*TypeExt) AsType

func (self *TypeExt) AsType() reflect.Type

Gets the instance as "reflect.Type"

func (*TypeExt) FuncInfo

func (self *TypeExt) FuncInfo() *FuncInfo

Gets function information of this type

func (*TypeExt) InterfaceType

func (self *TypeExt) InterfaceType() *TypeExt

In GoLang, you should use (*<Interface>)(nil) to reflect the type of an interface.

This method uses the "reflect.Type.Elem()" to get the "real type" of an interface.

returns: the type of target interface

func (*TypeExt) IsReflectType

func (self *TypeExt) IsReflectType() bool

Returns "true" if the type is "Reflect.Type"

func (*TypeExt) Kind

func (self *TypeExt) Kind() reflect.Kind

As same as "reflect.Type.Kind()"

func (*TypeExt) NewAsPointer

func (self *TypeExt) NewAsPointer() ValueExt

New returns a Value representing a pointer to a new zero value for the specified type.

See: "reflect.New"

func (*TypeExt) NewAsValue

func (self *TypeExt) NewAsValue() ValueExt

New returns a Value representing a instance for target type.

The type is indirected to non-pointer type recursively.

See: "reflect.New"

func (*TypeExt) RecursiveIndirect

func (self *TypeExt) RecursiveIndirect() *TypeExt

Gets the type to which of a pointer refer

type ValueExt

type ValueExt reflect.Value

Alias of reflect.Value, provides some convenient methods of features.

Example (IsViable)
printOut := func(content string, v interface{}) {
	fmt.Printf(content+": %v\n", ValueExtBuilder.NewByAny(v).IsViable())
}

/**
 * True
 */*/
printOut("0", 0)
printOut("false", false)
printOut("<empty string>", "")
bufferedChan := make(chan int, 2)
bufferedChan <- 1
printOut("chan int(one element)", bufferedChan)
// :~)

fmt.Println("--------------------")

/**
 * False
 */*/
printOut("(*int)(nil)", (*int)(nil))
printOut("([]string)(nil)", ([]string)(nil))
printOut("[0]string", [0]string{})
printOut("[]uint64(len == 0)", make([]uint64, 0, 4))
printOut("map[int]string(empty)", make(map[int]string))

<-bufferedChan
printOut("chan int(empty)", bufferedChan)
// :~)
Output:

0: true
false: true
<empty string>: true
chan int(one element): true
--------------------
(*int)(nil): false
([]string)(nil): false
[0]string: false
[]uint64(len == 0): false
map[int]string(empty): false
chan int(empty): false
Example (RecursiveIndirect)
var s1 string = "hello"
s1p := &s1
s1pp := &s1p

s1ppExt := ValueExtBuilder.NewByAny(s1pp)

fmt.Printf("Indirected value: %s", s1ppExt.RecursiveIndirect().AsAny())
Output:

Indirected value: hello

func (ValueExt) AsAny

func (self ValueExt) AsAny() interface{}

Converts this object to "interface{}"

func (ValueExt) AsValue

func (self ValueExt) AsValue() reflect.Value

Converts this object to "reflect.Value"

func (ValueExt) GetFieldValue

func (self ValueExt) GetFieldValue(tree ...string) ValueExt

Gets value of field, supporting tree visiting whether or not the value is struct or pointer to struct.

See: "ValueExt.SetFieldValue"

func (ValueExt) IsArrayOrSlice

func (self ValueExt) IsArrayOrSlice() bool

Returns true value if the value is array or slice

func (ValueExt) IsPointer

func (self ValueExt) IsPointer() bool

Returns true value if the value is reflect.Ptr, reflect.Uintptr, or reflect.UnsafePointer

func (ValueExt) IsViable

func (self ValueExt) IsViable() bool

Checks if a value is viable

For array, slice, map, chan: the value of "reflect.Value.Len()" must be > 0
For pointer, interface, or function: the value of "reflect.Value.IsNil()" must be false

Othewise: use reflect.Value.IsValid()

func (ValueExt) RecursiveIndirect

func (self ValueExt) RecursiveIndirect() ValueExt

Gets the value of struct(following points) represented by this value

func (ValueExt) SetFieldValue

func (self ValueExt) SetFieldValue(newValue ValueExt, tree ...string) ValueExt

Sets value of field, supporting tree visiting whether or not the value is struct or pointer to struct.

Note: Only instance of addressable("reflect.Value.CanAddr") struct can be set value of field.

returns: The original value

See: "ValueExt.GetFieldValue"

func (ValueExt) TypeExt

func (self ValueExt) TypeExt() *TypeExt

Gets the type of this value as "*TypeExt"

See: "TypeExtBuilder.NewByType"

Directories

Path Synopsis
This package contains instances of "reflect.Type".
This package contains instances of "reflect.Type".

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL