reflecth

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: May 9, 2023 License: Apache-2.0 Imports: 8 Imported by: 1

Documentation

Overview

Package reflecth provides helper functions for GoLang reflect package. It also includes binary, unary, shift and compare operations on reflect.Value as Go specification describes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assign

func Assign(x reflect.Value, t reflect.Type) (r reflect.Value, ok bool)

Assign is a MustAssign with check for assignation possibility.

func AssignableTo

func AssignableTo(x reflect.Value, t reflect.Type) bool

AssignableTo is just a shortcut for x.Type().AssignableTo(t).

func BinaryOp

func BinaryOp(x reflect.Value, op token.Token, y reflect.Value) (r reflect.Value, err error)

BinaryOp performs binary operation <x><op><y> as Go language specification describes. Supported operations: && || + - * / % & | ^ &^ . If operation cannot be performed then error will be returned.

func ChanDirFromAst

func ChanDirFromAst(dir ast.ChanDir) (r reflect.ChanDir)

ChanDirFromAst preforms conversion channel direction from ast.ChanDir representation to reflect.ChanDir representation. ChanDirFromAst returns 0 if dir cannot be represented as reflect.ChanDir (if dir invalid itself).

func ChanDirToAst

func ChanDirToAst(dir reflect.ChanDir) (r ast.ChanDir)

ChanDirToAst preforms conversion channel direction from reflect.ChanDir representation to ast.ChanDir representation. ChanDirToAst returns 0 if dir cannot be represented as ast.ChanDir (if dir invalid itself).

func CompareOp

func CompareOp(x reflect.Value, op token.Token, y reflect.Value) (r bool, err error)

CompareOp performs compare operation <x><op><y> as Go language specification describes. Supported operations: < <= >= > == != . If operation cannot be performed then error will be returned.

func Convert

func Convert(x reflect.Value, t reflect.Type) (r reflect.Value, ok bool)

Convert is a MustConvert with check for conversion possibility.

func ConvertibleTo

func ConvertibleTo(x reflect.Value, t reflect.Type) bool

ConvertibleTo is just a shortcut for x.Type().ConvertibleTo(t).

func IsAnyInt

func IsAnyInt(k reflect.Kind) bool

IsAnyInt returns true if k is any integer kind (signed or unsigned).

func IsComplex

func IsComplex(k reflect.Kind) bool

IsComplex returns true if k is any complex kind.

func IsFloat

func IsFloat(k reflect.Kind) bool

IsFloat returns true if k is any float kind.

func IsInt

func IsInt(k reflect.Kind) bool

IsInt returns true if k is any signed integer kind.

func IsUint

func IsUint(k reflect.Kind) bool

IsUint returns true if k is any unsigned integer kind.

func MakeSettable deprecated

func MakeSettable(x reflect.Value) reflect.Value

MakeSettable make x settable. It panics if CanAddr returns false for x.

Deprecated: In any case it is bad practice.

func MustAssign

func MustAssign(x reflect.Value, t reflect.Type) reflect.Value

MustAssign assign x to newly created reflect.Value of type t. It panics if it is impossible to create reflect.Value of type t of if x cannot be assign to t.

func MustConvert

func MustConvert(x reflect.Value, t reflect.Type) reflect.Value

MustConvert is just a x.Convert(t).

func Set deprecated

func Set(dst, src reflect.Value)

Set assigns src to the value dst. It is similar to dst.Set(src) but this function also allow to set private fields. Primary reason for this is to avoid restriction with your own struct variable. It panics if CanAddr returns false. As in Go, x's value must be assignable to v's type.

Deprecated: In any case it is bad practice to change private fields in 3rd party variables/classes.

func ShiftOp

func ShiftOp(x reflect.Value, op token.Token, s uint) (r reflect.Value, err error)

ShiftOp performs shift operation <x><op><s> as Go language specification describes. Supported operations: << >> . If operation cannot be performed then error will be returned.

func TypeAssert

func TypeAssert(x reflect.Value, t reflect.Type) (r reflect.Value, ok bool, valid bool)

TypeAssert perform GoLang type assertion (see language specs). Variable x must be of interface kind (or panic/undefined behaviour happened). Variable valid identify is assertion valid. Assertion is valid if t is of interface kind or if t implements type of x. In Go invalid assertion causes compile time error (something like "impossible type assertion: <t> does not implement <type of x>"). Variable ok identity is assertion true. Assertion is true if x is not nil and the value stored in x is of type t. Variable ok sets to false if valid is false, so it is valid way to check only variable ok and ignore variable valid if details of false result does not have mean.

func TypeBool

func TypeBool() reflect.Type

TypeBool returns type of bool.

func TypeByte

func TypeByte() reflect.Type

TypeByte returns type of byte.

func TypeComplex128

func TypeComplex128() reflect.Type

TypeComplex128 returns type of complex128.

func TypeComplex64

func TypeComplex64() reflect.Type

TypeComplex64 returns type of complex64.

func TypeEmptyInterface

func TypeEmptyInterface() reflect.Type

TypeEmptyInterface returns type of empty interface.

func TypeFloat32

func TypeFloat32() reflect.Type

TypeFloat32 returns type of float32.

func TypeFloat64

func TypeFloat64() reflect.Type

TypeFloat64 returns type of float64.

func TypeInt

func TypeInt() reflect.Type

TypeInt returns type of int.

func TypeInt16

func TypeInt16() reflect.Type

TypeInt16 returns type of int16.

func TypeInt32

func TypeInt32() reflect.Type

TypeInt32 returns type of int32.

func TypeInt64

func TypeInt64() reflect.Type

TypeInt64 returns type of int64.

func TypeInt8

func TypeInt8() reflect.Type

TypeInt8 returns type of int8.

func TypeOfPtr

func TypeOfPtr(i interface{}) reflect.Type

TypeOfPtr returns type of value pointed to by i. If i is not a pointer than TypeOfPtr returns nil.

func TypeRune

func TypeRune() reflect.Type

TypeRune returns type of rune.

func TypeString

func TypeString() reflect.Type

TypeString returns type of string.

func TypeUint

func TypeUint() reflect.Type

TypeUint returns type of uint.

func TypeUint16

func TypeUint16() reflect.Type

TypeUint16 returns type of uint16.

func TypeUint32

func TypeUint32() reflect.Type

TypeUint32 returns type of uint32.

func TypeUint64

func TypeUint64() reflect.Type

TypeUint64 returns type of uint64.

func TypeUint8

func TypeUint8() reflect.Type

TypeUint8 returns type of uint8.

func TypeUintptr

func TypeUintptr() reflect.Type

TypeUintptr returns type of uintptr.

func UnaryOp

func UnaryOp(op token.Token, y reflect.Value) (r reflect.Value, err error)

UnaryOp performs unary operation <op><y> as Go language specification describes. Supported operations: + - ^ ! & <- . If operation cannot be performed then error will be returned. Special note for token.AND (&) operation: if passed y is not addressable (reflect.Value.CanAddr) then new variable will be created with the same as y type and value and address of new variable will be returned.

func ValueOfPtr

func ValueOfPtr(i interface{}) reflect.Value

ValueOfPtr returns value pointed to by i. If i is not a pointer than ValueOfPtr returns zero Value. This function is useful for passing interface to reflect.Value via passing pointer to interface to this function (it is impossible to make reflect.Value of kind interface via passing interface directly to ValueOf function).

Types

This section is empty.

Jump to

Keyboard shortcuts

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