xreflect

package
v0.0.0-...-48c8420 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2017 License: LGPL-3.0 Imports: 13 Imported by: 0

README

xreflect

The package xreflect is a wrapper aroung Go standard packages reflect and go/types that emulates the missing features of reflect package:

  • NamedOf: declare new named types at runtime
  • AddMethod: add method to a named type at runtime
  • InterfaceOf: declare new interfaces at runtime

License

GPL v3+

Documentation

Index

Constants

View Source
const (
	StrGensymInterface = "\U0001202A" // name of extra struct field needed by the interpreter when creating interpreted interfaces
	StrGensymPrivate   = "\U00012038" // prefix to generate names for unexported struct fields.
	StrGensymEmbedded  = "\U00012039" // prefix to generate names for embedded struct fields.
)

the following constants must match with github.com/cosmos72/gomacro/base/constants.go

View Source
const MaxDepth = int(^uint(0) >> 1)

Variables

This section is empty.

Functions

func GensymEmbedded

func GensymEmbedded(name string) string

func GensymPrivate

func GensymPrivate(name string) string

func IsGoUntypedKind

func IsGoUntypedKind(gkind types.BasicKind) bool

func SameType

func SameType(t, u Type) bool

func ToBasicKind

func ToBasicKind(kind reflect.Kind, untyped bool) types.BasicKind

func ToReflectKind

func ToReflectKind(gkind types.BasicKind) reflect.Kind

func Zero

func Zero(t Type) reflect.Value

Zero returns a Value representing the zero value for the specified type.

Types

type Error

type Error struct {
	Type Type
	// contains filtered or unexported fields
}

func (*Error) Error

func (e *Error) Error() string

type Importer

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

func DefaultImporter

func DefaultImporter() *Importer

func (*Importer) Import

func (imp *Importer) Import(path string) (*types.Package, error)

func (*Importer) ImportFrom

func (imp *Importer) ImportFrom(path string, srcDir string, mode types.ImportMode) (*types.Package, error)

type InterfaceHeader

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

InterfaceHeader is the internal header of interpreted interfaces

func MakeInterfaceHeader

func MakeInterfaceHeader(val reflect.Value, typ Type) InterfaceHeader

func (InterfaceHeader) Type

func (h InterfaceHeader) Type() Type

func (InterfaceHeader) Value

func (h InterfaceHeader) Value() reflect.Value

type Method

type Method struct {
	Name       string
	Pkg        *Package
	Type       Type             // method type
	Funs       *[]reflect.Value // (*Funs)[Index] is the method, with receiver as first argument
	Index      int              // index for Type.Method
	FieldIndex []int            // embedded fields index sequence for reflect.Type.FieldByIndex or reflect.Value.FieldByIndex
	GoFun      *types.Func      // for completeness
}

type Package

type Package types.Package

func (*Package) GoPackage

func (pkg *Package) GoPackage() *types.Package

func (*Package) Name

func (pkg *Package) Name() string

func (*Package) Path

func (pkg *Package) Path() string

type QName

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

QName is a replacement for go/types.Id and implements accurate comparison of type names, field names and method names. It recognizes unexported names, and names declared in different packages.

To compare two names, build two QNames with the functions QName* then compare the two QName structs with ==

func QName1

func QName1(q QNameI) QName

func QName2

func QName2(name, pkgpath string) QName

func QNameGo

func QNameGo(obj types.Object) QName

func QNameGo2

func QNameGo2(name string, pkg *types.Package) QName

func (QName) Name

func (q QName) Name() string

func (QName) PkgPath

func (q QName) PkgPath() string

type QNameI

type QNameI interface {
	Name() string
	PkgPath() string
}

type QNameI_github_com_cosmos72_gomacro_xreflect

type QNameI_github_com_cosmos72_gomacro_xreflect struct {
	Object   interface{}
	Name_    func() string
	PkgPath_ func() string
}

--------------- proxy for github.com/cosmos72/gomacro/xreflect.QNameI ---------------

func (*QNameI_github_com_cosmos72_gomacro_xreflect) Name

func (*QNameI_github_com_cosmos72_gomacro_xreflect) PkgPath

type StructField

type StructField struct {
	// Name is the field name.
	Name string
	// Pkg is the package that qualifies a lower case (unexported)
	// field name. It may be nil for upper case (exported) field names.
	// See https://golang.org/ref/spec#Uniqueness_of_identifiers
	Pkg       *Package
	Type      Type              // field type
	Tag       reflect.StructTag // field tag string
	Offset    uintptr           // offset within struct, in bytes
	Index     []int             // index sequence for reflect.Type.FieldByIndex or reflect.Value.FieldByIndex
	Anonymous bool              // is an embedded field. Note: embedded field's name should be set to the type's name
}

type Type

type Type []xtype

func ArrayOf

func ArrayOf(count int, elem Type) Type

func ChanOf

func ChanOf(dir reflect.ChanDir, elem Type) Type

func FuncOf

func FuncOf(in []Type, out []Type, variadic bool) Type

func InterfaceOf

func InterfaceOf(methodnames []string, methods []Type, embeddeds []Type) Type

InterfaceOf returns a new interface for the given methods and embedded types. After the methods and embeddeds are fully defined, call Complete() to mark the interface as complete and compute wrapper methods for embedded fields.

WARNING: the Type returned by InterfaceOf is not complete, i.e. its method set is not computed yet. Once you know that methods and embedded interfaces are complete, call Complete() to compute the method set and mark this Type as complete.

func MapOf

func MapOf(key, elem Type) Type

func MethodOf

func MethodOf(recv Type, in []Type, out []Type, variadic bool) Type

func PtrTo

func PtrTo(elem Type) Type

func SliceOf

func SliceOf(elem Type) Type

func StructOf

func StructOf(fields []StructField) Type

func (Type) AddMethod

func (t Type) AddMethod(name string, signature Type) int

AddMethod adds method with given name and signature to type, unless it is already in the method list. It panics if the type is unnamed, or if the signature is not a function-with-receiver type. Returns the method index, or < 0 in case of errors

func (Type) Align

func (t Type) Align() int

Align returns the alignment in bytes of a value of this type when allocated in memory.

func (Type) AssignableTo

func (t Type) AssignableTo(u Type) bool

AssignableTo reports whether a value of the type is assignable to type u.

func (Type) Bits

func (t Type) Bits() int

Bits returns the size of the type in bits. It panics if the type's Kind is not one of the sized or unsized Int, Uint, Float, or Complex kinds.

func (Type) ChanDir

func (t Type) ChanDir() reflect.ChanDir

ChanDir returns a channel type's direction. It panics if the type's Kind is not Chan.

func (Type) Comparable

func (t Type) Comparable() bool

Comparable reports whether values of this type are comparable.

func (Type) Complete

func (t Type) Complete() Type

Complete marks an interface type as complete and computes wrapper methods for embedded fields. It must be called by users of InterfaceOf after the interface's embedded types are fully defined and before using the interface type in any way other than to form other types. Complete returns the receiver.

func (Type) ConvertibleTo

func (t Type) ConvertibleTo(u Type) bool

ConvertibleTo reports whether a value of the type is convertible to type u.

func (Type) Elem

func (t Type) Elem() Type

Elem returns a type's element type. It panics if the type's Kind is not Array, Chan, Map, Ptr, or Slice.

func (Type) Field

func (t Type) Field(i int) StructField

Field returns a struct type's i-th field. It panics if the type's Kind is not Struct. It panics if i is not in the range [0, NumField()).

func (Type) FieldAlign

func (t Type) FieldAlign() int

FieldAlign returns the alignment in bytes of a value of this type when used as a field in a struct.

func (Type) FieldByName

func (t Type) FieldByName(name, pkgpath string) (field StructField, count int)

FieldByName returns the (possibly embedded) struct field with the given name and the number of fields found at the same (shallowest) depth: 0 if not found. Private fields are returned only if they were declared in pkgpath.

func (Type) GetMethods

func (t Type) GetMethods() *[]reflect.Value

GetMethods returns the pointer to the method values. It panics if the type is unnamed

func (Type) GoType

func (t Type) GoType() types.Type

GoType returns the go/types.Type corresponding to the given type.

func (Type) Implements

func (t Type) Implements(u Type) bool

Implements reports whether the type implements the interface type u. It panics if u's Kind is not Interface

func (Type) In

func (t Type) In(i int) Type

In returns the type of a function type's i'th input parameter. It panics if the type's Kind is not Func. It panics if i is not in the range [0, NumIn()).

func (Type) IsMethod

func (t Type) IsMethod() bool

IsMethod reports whether a function type's contains a receiver, i.e. is a method. If IsMethod returns true, the actual receiver type is available as the first parameter, i.e. Type.In(0) It panics if the type's Kind is not Func.

func (Type) IsVariadic

func (t Type) IsVariadic() bool

IsVariadic reports whether a function type's final input parameter is a "..." parameter. If so, t.In(t.NumIn() - 1) returns the parameter's implicit actual type []T. IsVariadic panics if the type's Kind is not Func.

func (Type) Key

func (t Type) Key() Type

Key returns a map type's key type. It panics if the type's Kind is not Map.

func (Type) Kind

func (t Type) Kind() reflect.Kind

Kind returns the specific kind of the type.

func (Type) Len

func (t Type) Len() int

Len returns an array type's length. It panics if the type's Kind is not Array.

func (Type) Method

func (t Type) Method(i int) Method

Method return the i-th explicitly declared method of named type or interface t. Wrapper methods for embedded fields or embedded interfaces are not returned. It panics if the type is unnamed, or if the type's Kind is not Interface

func (Type) MethodByName

func (t Type) MethodByName(name, pkgpath string) (method Method, count int)

MethodByName returns the method with given name (including wrapper methods for embedded fields) and the number of methods found at the same (shallowest) depth: 0 if not found. Private methods are returned only if they were declared in pkgpath.

func (Type) Name

func (t Type) Name() string

Name returns the type's name within its package. It returns an empty string for unnamed types.

func (Type) Named

func (t Type) Named() bool

Named returns whether the type is named. It returns false for unnamed types.

func (Type) NumField

func (t Type) NumField() int

NumField returns a struct type's field count. It panics if the type's Kind is not Struct.

func (Type) NumIn

func (t Type) NumIn() int

NumIn returns a function type's input parameter count. It panics if the type's Kind is not Func.

func (Type) NumMethod

func (t Type) NumMethod() int

NumMethod returns the number of explicitly declared methods of named type or interface t. Wrapper methods for embedded fields or embedded interfaces are not counted.

func (Type) NumOut

func (t Type) NumOut() int

NumOut returns a function type's output parameter count. It panics if the type's Kind is not Func.

func (Type) Out

func (t Type) Out(i int) Type

Out returns the type of a function type's i'th output parameter. It panics if the type's Kind is not Func. It panics if i is not in the range [0, NumOut()).

func (Type) Pkg

func (t Type) Pkg() *Package

Pkg returns a named type's package, that is, the package where it was defined. If the type was predeclared (string, error) or unnamed (*T, struct{}, []int), Pkg will return nil.

func (Type) PkgName

func (t Type) PkgName() string

PkgName returns a named type's package name, that is, the default name that the package provides when imported. If the type was predeclared (string, error) or unnamed (*T, struct{}, []int), the package name will be the empty string.

func (Type) PkgPath

func (t Type) PkgPath() string

PkgPath returns a named type's package path, that is, the import path that uniquely identifies the package, such as "encoding/base64". If the type was predeclared (string, error) or unnamed (*T, struct{}, []int), the package path will be the empty string.

func (Type) ReflectType

func (t Type) ReflectType() reflect.Type

ReflectType returns a best-effort reflect.Type that approximates the type. It may be inexact for the following reasons:

  1. missing reflect.NamedOf(): no way to programmatically create named types, or to access the underlying type of a named type
  2. missing reflect.InterfaceOf(): interface types created at runtime will be approximated by structs
  3. missing reflect.MethodOf(): method types created at runtime will be approximated by functions whose first parameter is the receiver
  4. reflect.StructOf() does not support embedded or unexported fields
  5. go/reflect lacks the ability to create self-referencing types: references to the type itself will be replaced by interface{}.

Examples:

after invoking at runtime type2.NewStruct() and type2.NewNamed()
to create the following type:
    type List struct { Elem int; Rest *List }
ReflectType will return a reflect.Type equivalent to:
    struct { Elem int; Rest interface{} }
i.e. the type name will be missing due to limitation 1 above,
and the field 'Rest' will have type interface{} instead of *List due to limitation 5.

func (Type) RemoveMethods

func (t Type) RemoveMethods(names []string, pkgpath string)

RemoveMethods removes given methods from type. It panics if the type is unnamed.

func (Type) SetUnderlying

func (t Type) SetUnderlying(underlying Type)

SetUnderlying sets the underlying type of a named type and marks it as complete. It panics if the type is unnamed, or if the underlying type is named, or if SetUnderlying() was already invoked on the named type.

func (Type) Size

func (t Type) Size() uintptr

Size returns the number of bytes needed to store a value of the given type; it is analogous to unsafe.Sizeof.

func (Type) String

func (t Type) String() string

String returns a string representation of a type.

func (Type) Universe

func (t Type) Universe() *Universe

func (Type) UnsafeForceReflectType

func (t Type) UnsafeForceReflectType(rtype reflect.Type)

type Types

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

type Universe

type Universe struct {
	Types
	ReflectTypes    map[reflect.Type]Type
	BasicTypes      []Type
	TypeOfInterface Type
	TypeOfError     Type
	TryResolve      func(name, pkgpath string) Type
	Packages        map[string]*Package
	Importer        types.ImporterFrom
	RebuildDepth    int

	ThreadSafe bool
	// contains filtered or unexported fields
}

func DefaultUniverse

func DefaultUniverse() *Universe

func NewUniverse

func NewUniverse() *Universe

func (*Universe) ArrayOf

func (v *Universe) ArrayOf(count int, elem Type) Type

func (*Universe) CachePackage

func (v *Universe) CachePackage(pkg *types.Package)

CachePackage unconditionally adds pkg to Universe.Packages, then also adds its imports if not cached already

func (*Universe) ChanOf

func (v *Universe) ChanOf(dir reflect.ChanDir, elem Type) Type

func (*Universe) FromReflectType

func (v *Universe) FromReflectType(rtype reflect.Type) Type

func (*Universe) FuncOf

func (v *Universe) FuncOf(in []Type, out []Type, variadic bool) Type

func (*Universe) Init

func (v *Universe) Init() *Universe

func (*Universe) InterfaceOf

func (v *Universe) InterfaceOf(methodnames []string, methods []Type, embeddeds []Type) Type

func (*Universe) InvalidateCache

func (v *Universe) InvalidateCache()

clears all xtype.fieldcache and xtype.methodcache. invoked by NamedOf() when a type is redefined.

func (*Universe) InvalidateMethodCache

func (v *Universe) InvalidateMethodCache()

clears all xtype.methodcache. invoked by AddMethod() when a method is redefined.

func (*Universe) LoadPackage

func (v *Universe) LoadPackage(path string) *Package

func (*Universe) MakeType

func (v *Universe) MakeType(gtype types.Type, rtype reflect.Type) Type

func (*Universe) MapOf

func (v *Universe) MapOf(key, elem Type) Type

func (*Universe) MethodOf

func (v *Universe) MethodOf(recv Type, in []Type, out []Type, variadic bool) Type

func (*Universe) NamedOf

func (v *Universe) NamedOf(name, pkgpath string) Type

func (*Universe) PtrTo

func (v *Universe) PtrTo(elem Type) Type

func (*Universe) SliceOf

func (v *Universe) SliceOf(elem Type) Type

func (*Universe) StructOf

func (v *Universe) StructOf(fields []StructField) Type

func (*Universe) TypeOf

func (v *Universe) TypeOf(rvalue interface{}) Type

type Value

type Value struct {
	reflect.Value
	XType Type
}

func (Value) Convert

func (v Value) Convert(t Type) Value

func (Value) FieldByName

func (v Value) FieldByName(name, pkgpath string) Value

func (Value) Kind

func (v Value) Kind() reflect.Kind

func (Value) Type

func (v Value) Type() Type

Jump to

Keyboard shortcuts

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