goo

package
v0.0.0-...-45da7de Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FuncPkgFullName

func FuncPkgFullName(val reflect.Value) string

func FuncPkgName

func FuncPkgName(val reflect.Value) string

func GoFuncName

func GoFuncName(val reflect.Value) string

func GoPtrTypeAndValue

func GoPtrTypeAndValue(obj interface{}) (reflect.Type, reflect.Value)

func GoTypeAndValue

func GoTypeAndValue(obj interface{}) (reflect.Type, reflect.Value, bool)

func GoTypeName

func GoTypeName(typ reflect.Type) string

func IsAnyKind

func IsAnyKind(typ reflect.Type, kinds ...reflect.Kind) bool

func IsComplex

func IsComplex(typ reflect.Type) bool

func IsFieldExported

func IsFieldExported(f reflect.StructField) bool

func IsFloat

func IsFloat(typ reflect.Type) bool

func IsMethodExported

func IsMethodExported(m reflect.Method) bool

func IsNumber

func IsNumber(typ reflect.Type) bool

func IsSigned

func IsSigned(typ reflect.Type) bool

func IsUnsigned

func IsUnsigned(typ reflect.Type) bool

func PanicIf

func PanicIf(err error)

func ParseFloat

func ParseFloat(val string, bitSize int) float64

func ParseInt

func ParseInt(s string, typ Integer) (resultValue interface{}, err error)

Types

type Array

type Array interface {
	Type
	New
	ElemType() Type
	Len() int
}

type BitSize

type BitSize int
const (
	Bit8   BitSize = 8
	Bit16  BitSize = 16
	Bit32  BitSize = 32
	Bit64  BitSize = 64
	Bit128 BitSize = 128
)

func BitSizeIf

func BitSizeIf(k, ifv reflect.Kind, a, b BitSize) BitSize

type Bool

type Bool interface {
	Type
	New
	ToBool(value string) bool
	ToStr(value bool) string
}

type Complex

type Complex interface {
	Number
	ImaginaryData(val interface{}) interface{}
	RealData(val interface{}) interface{}
}

type Exportable

type Exportable interface {
	IsExported() bool
}

type Field

type Field interface {
	Member
	Taggable
	IsAnonymous() bool
	Type() Type
	CanSet() bool
	Set(instance interface{}, value interface{})
	Get(instance interface{}) interface{}
}

func ConvertGoField

func ConvertGoField(f reflect.StructField) Field

type Float

type Float interface {
	Number
}

type Func

type Func interface {
	Type
	InTypes() []Type
	InNum() int
	OutTypes() []Type
	OutNum() int
	Call(args []interface{}) []interface{}
}

type Integer

type Integer interface {
	Number
	IsSigned() bool
}

type Interface

type Interface interface {
	Type
	Methods() []Method
	MethodNum() int
}

type Invokable

type Invokable interface {
	Invoke(obj interface{}, args ...interface{}) []interface{}
}

type Map

type Map interface {
	Type
	New
	KeyType() Type
	ValueType() Type
}

type Member

type Member interface {
	Exportable
	Name() string
	String() string
}

type Method

type Method interface {
	Member
	Invokable
	OutNum() int
	OutTypes() []Type
	InNum() int
	InTypes() []Type
}

func ConvertGoMethod

func ConvertGoMethod(m reflect.Method) Method

type New

type New interface {
	New() interface{}
}

type Number

type Number interface {
	Type
	New
	Type() NumberType
	BitSize() BitSize
	Overflow(val interface{}) bool
	ToString(val interface{}) string
}

type NumberType

type NumberType int
const (
	IntType NumberType = iota
	FloatType
	ComplexType
)

type Slice

type Slice interface {
	Type
	New
	GetElementType() Type
}

type String

type String interface {
	Type
	New
	ToNumber(val string, number Number) (interface{}, error)
	ToInt(val string) int
	ToInt8(val string) int8
	ToInt16(val string) int16
	ToInt32(val string) int32
	ToInt64(val string) int64
	ToUint(val string) uint
	ToUint8(val string) uint8
	ToUint16(val string) uint16
	ToUint32(val string) uint32
	ToUint64(val string) uint64
	ToFloat32(val string) float32
	ToFloat64(val string) float64
}

type Struct

type Struct interface {
	Type
	New
	Fields() []Field
	FieldNum() int
	FieldsExported() []Field
	FieldExportedNum() int
	FieldsUnexported() []Field
	FieldUnexportedNum() int
	FieldsAnonymous() []Field
	FieldAnonymousNum() int
	Methods() []Method
	MethodNum() int
	Implements(i Interface) bool
	IsEmbedded(candidate Struct) bool
}

type Tag

type Tag struct {
	Name  string
	Value string
}

func (Tag) String

func (t Tag) String() string

type Taggable

type Taggable interface {
	Tags() []Tag
	TagByName(name string) (Tag, error)
}

type Type

type Type interface {
	TypeConverter
	Name() string
	NameFull() string
	PkgName() string
	PkgNameFull() string
	PtrType() reflect.Type
	GoPtrValue() reflect.Value
	GoType() reflect.Type
	GoValue() reflect.Value
	IsPtr() bool
	IsInstantiable() bool
	String() string
	Equals(anotherType Type) bool
}

func FromGoType

func FromGoType(typ reflect.Type) Type

func TypeOf

func TypeOf(obj interface{}) Type
Example
package main

import (
	"github.com/bingoohuang/gg/pkg/goo"
)

type MyInterface interface {
	Method1(str string)
	Method2(val int, str string)
}

type MyStruct struct {
	Name  string
	Price float32
}

func MyFunc(string, int, MyStruct) (string, error) {
	return "test", nil
}

func main() {
	fn := goo.TypeOf(MyFunc)
	if fn.IsFunc() {
		fnType := fn.ToFuncType()
		outputs := fnType.Call([]interface{}{"test", 25, MyStruct{}})
		if len(outputs) > 0 {
			// ...
		}
	}

	testStructType := goo.TypeOf(&MyStruct{})
	if testStructType.IsStruct() {
		structType := testStructType.ToStructType()
		for _, method := range structType.Methods() {
			method.Name()
			// method.Invoke(...)
			method.IsExported()
			method.OutTypes()
			method.OutNum()
			method.InTypes()
			method.InNum()
		}

		// ...
		structType.FieldsExported()
		for _, field := range structType.Fields() {
			field.Name()
			field.Type()
			field.Get(&MyStruct{})
			// field.Set(testStructInstance, nil)
			field.Tags()
			tag, err := field.TagByName("json")
			if err == nil {
				if tag.Value != "" && tag.Name != "" {
					// ...
				}
			}
		}
	}

	if testStructType.IsInstantiable() {
		structType := testStructType.ToStructType()
		newStructInstance := structType.New()
		if newStructInstance != nil {
			// ...
		}
	}

	testInterfaceType := goo.TypeOf((*MyInterface)(nil))
	if testInterfaceType.IsInterface() {
		interfaceType := testInterfaceType.ToInterfaceType()
		interfaceType.Methods()
		// ...
		interfaceType.MethodNum()
	}

	testSignedIntType := goo.TypeOf(25)
	if testSignedIntType.IsNumber() {
		numberType := testSignedIntType.ToNumberType()
		if goo.IntType == numberType.Type() {
			integerType := numberType.(goo.Integer)
			if integerType.IsSigned() {
				// ...
			}
		}
	}

	testFloat32Type := goo.TypeOf(float32(42.28))
	if testFloat32Type.IsNumber() {
		numberType := testFloat32Type.ToNumberType()
		if goo.FloatType == numberType.Type() {
			floatType := numberType.(goo.Float)
			if goo.Bit32 == floatType.BitSize() {
				// ...
			}
		}
	}

	testMapType := goo.TypeOf(make(map[string]bool, 0))
	if testMapType.IsMap() {
		mapType := testMapType.ToMapType()

		keyType := mapType.KeyType()
		if keyType.IsString() {
			// ...
		}

		valueType := mapType.ValueType()
		if valueType.IsBool() {
			// ...
		}
	}

	if testMapType.IsInstantiable() {
		mapType := testMapType.ToMapType()
		newMapInstance := mapType.New()
		if newMapInstance != nil {
			// ...
		}
	}

	stringTestType := goo.TypeOf("test")
	if stringTestType.IsString() {
		stringType := stringTestType.ToStringType()
		stringType.ToUint8("20")
		stringType.ToUint64("58745")
		stringType.ToInt8("-23")
		stringType.ToUint64("9823")
		stringType.ToFloat32("23.52")
		stringType.ToFloat64("82387.32")
	}

	array := [5]string{}
	testArrayType := goo.TypeOf(array)
	if testArrayType.IsArray() {
		arrayType := testArrayType.ToArrayType()
		arrayType.ElemType()
		// ...
		arrayType.Len()
	}

	testSliceType := goo.TypeOf(array[2:])
	if testSliceType.IsSlice() {
		sliceType := testSliceType.ToSliceType()
		sliceType.GetElementType()
		// ...
		sliceType.New()
	}

}
Output:

type TypeConverter

type TypeConverter interface {
	IsBool() bool
	ToBoolType() Bool
	IsNumber() bool
	ToNumberType() Number
	IsFunc() bool
	ToFuncType() Func
	IsStruct() bool
	ToStructType() Struct
	IsInterface() bool
	ToInterfaceType() Interface
	IsString() bool
	ToStringType() String
	IsMap() bool
	ToMapType() Map
	IsArray() bool
	ToArrayType() Array
	IsSlice() bool
	ToSliceType() Slice
}

Jump to

Keyboard shortcuts

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