reflectx

package module
v0.3.10 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2020 License: Apache-2.0 Imports: 6 Imported by: 5

README

reflectx

Golang reflect package hack tools

Go1.14 Go1.15

struct unexported field can set

  • reflectx.CanSet
  • reflectx.Field
  • reflectx.FieldByIndex
  • reflectx.FieldByName
  • reflectx.FieldByNameFunc
type Point struct {
    x int
    y int
}

x := &Point{10, 20}
v := reflect.ValueOf(x).Elem()
sf := v.Field(0)

fmt.Println(sf.CanSet()) // output: false
// sf.SetInt(102)        
// panic: reflect: reflect.Value.SetInt using value obtained using unexported field

sf = reflectx.CanSet(sf)
fmt.Println(sf.CanSet()) // output: true

sf.SetInt(102)           // x.x = 102
fmt.Println(x.x)         // output: 102

reflectx.Field(x,1).SetInt(100) // x.y = 100

embedded type more than one field

  • reflectx.StructOf
type Buffer struct {
	*bytes.Buffer
	X int
	Y int
}

typ := reflect.TypeOf((*Buffer)(nil)).Elem()
var fs []reflect.StructField
for i := 0; i < typ.NumField(); i++ {
	fs = append(fs, typ.Field(i))
}

// reflect.StructOf(fs) 
// panic reflect: embedded type with methods not implemented if there is more than one field

reflectx.StructOf(fs)

different types of fields generated by name

  • reflectx.NamedStructOf
fs := []reflect.StructField{
	reflect.StructField{Name: "X", Type: reflect.TypeOf(0)},
	reflect.StructField{Name: "Y", Type: reflect.TypeOf(0)},
}
t1 := reflectx.NamedStructOf("github.com/goplus/reflectx","Point", fs)
t2 := reflectx.NamedStructOf("github.com/goplus/reflectx","Point", fs)
t3 := reflectx.NamedStructOf("github.com/goplus/reflectx", "Point2", fs)

// t1 == t2
// t1 != t3
//
// t1.String() == reflectx.Point
// t1.Name() == Point
// t1.PkgPath() == github.com/goplus/reflectx
  • reflectx.NamedTypeOf
typ := reflectx.NamedTypeOf("main","Int",reflect.TypeOf(int(0)))
// typ.Kind() == reflect.Int
// typ.String() == "main.Int"
// typ.Name() == "Int"
// typ.PkgPath() == "main"
  • reflectx.IsNamed
  • reflectx.ToNamed

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CanSet

func CanSet(v reflect.Value) reflect.Value

func Field

func Field(s reflect.Value, i int) reflect.Value

func FieldByIndex

func FieldByIndex(s reflect.Value, index []int) reflect.Value

func FieldByName

func FieldByName(s reflect.Value, name string) reflect.Value

func FieldByNameFunc

func FieldByNameFunc(s reflect.Value, match func(name string) bool) reflect.Value

func IsNamed added in v0.3.7

func IsNamed(typ reflect.Type) bool

func NamedStructOf added in v0.3.2

func NamedStructOf(pkgpath string, name string, fields []reflect.StructField) reflect.Type

func NamedTypeOf added in v0.3.5

func NamedTypeOf(pkgpath string, name string, from reflect.Type) (typ reflect.Type)

func SetValue added in v0.3.5

func SetValue(v reflect.Value, x reflect.Value)

func StructOf

func StructOf(fields []reflect.StructField) reflect.Type

Types

type ChanDir added in v0.3.5

type ChanDir int

ChanDir represents a channel type's direction.

const (
	RecvDir ChanDir             = 1 << iota // <-chan
	SendDir                                 // chan<-
	BothDir = RecvDir | SendDir             // chan
)

type Named added in v0.3.7

type Named struct {
	Type reflect.Type
	From reflect.Type
	Kind TypeKind
}

func ToNamed added in v0.3.7

func ToNamed(typ reflect.Type) (t *Named, ok bool)

type TypeKind added in v0.3.5

type TypeKind int
const (
	TkInvalid TypeKind = iota
	TkStruct
	TkType
	TkInterface
)

type Value

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

Jump to

Keyboard shortcuts

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