reflectx

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2023 License: MIT Imports: 5 Imported by: 0

README

reflectx

make reflect type serializable, conveniently for RPC

一个可以序列化的反射类型,代替 reflect.Type,便于编写 RPC 代码

example

TypeOf gets the type

使用 TypeOf 获得类型

	tp := TypeOf(1)
	// {"kind":"int"}
	t.Log(tp.String())

    tp = TypeOf(struct {
		Name string
		Age  int64
	}{})
	// {"kind":"struct","struct":{"Age":{"kind":"int64"},"Name":{"kind":"string"}},"extra":{"field_0":"Name","field_1":"Age","fields_number":2}}
	t.Log(tp.String())

    tp = TypeOf(map[string]int{})
	// {"kind":"map","struct":{"key_type":{"kind":"string"},"value_type":{"kind":"int"}}}
	t.Log(tp.String())

    tp = TypeOf([]string{})
	// {"kind":"slice","struct":{"element_type":{"kind":"string"}}}
	t.Log(tp.String())

FromGoType converts the std reflect.Type to Type

FromGoType 可以将标准库中的 reflect.Type 转为 Type


GoType returns the std lib reflect.Type representation of the Type

GoType 将 Type 转为 标准库中的 reflect.Type

Documentation

Index

Constants

View Source
const (
	ElementType = "element_type"  // element type of Array Chan Pointer Slice
	ArrayLength = "array_length"  // length of Array
	KeyType     = "key_type"      // key type of Map
	ValueType   = "value_type"    // value type of Map
	NumIn       = "in_number"     // input parameter count of Func
	NumOut      = "out_number"    // output parameter count of Func
	In          = "in_"           // specify the index of in type
	Out         = "out_"          // specify the index of out type
	NumField    = "fields_number" // a struct type's field count
	Field       = "field_"        // specify the inedex of of field name
	ChanDir     = "chan_dir"      // dir of Chan
	Variadic    = "variadic"      // variadic of Func
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Type

type Type struct {
	Kind   string          `json:"kind,omitempty"`   // reflect.Type.Kind().String()
	Struct map[string]Type `json:"struct,omitempty"` // struct info of the type
	Extra  map[string]any  `json:"extra,omitempty"`  // extra info of the type. Such as array length
}

Type is a serializable representation of a Go type.

Field Kind is same as reflect.Type::Kind() Struct gives the complete info of the type if the Kind field cannot specify the type

func FromGoType

func FromGoType(goType reflect.Type) Type

FromGoType converts the std reflect.Type to Type

func TypeOf

func TypeOf(i any) Type

TypeOf returns the type of i

func (Type) GoType

func (t Type) GoType() reflect.Type

GoType returns the std lib reflect.Type representation of the Type

func (Type) String

func (t Type) String() string

Jump to

Keyboard shortcuts

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