util

package
v1.1.0-alpha Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2021 License: Apache-2.0 Imports: 15 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var UnimplementedMethod = errors.New("unimplemented method")

UnimplementedMethod 如果某个方法未实现则可以抛出此错误。

View Source
var UnsupportedMethod = errors.New("unsupported method")

UnsupportedMethod 如果某个方法禁止被调用则可以抛出此错误。

Functions

func CopyBean

func CopyBean(src interface{}, dest interface{}) error

CopyBean 使用 JSON 序列化的方式进行拷贝,支持匿名字段,支持类型转换。

func CurrentMilliSeconds

func CurrentMilliSeconds() int64

CurrentMilliSeconds 返回当前的毫秒时间

func FileLine

func FileLine(fn interface{}) (file string, line int, fnName string)

FileLine 获取函数所在文件、行数以及函数名

func HasReceiver

func HasReceiver(t reflect.Type, receiver reflect.Type) bool

HasReceiver 函数是否具有接收者。

func Indirect

func Indirect(t reflect.Type) reflect.Type

Indirect 解除 Type 所有层级的指针。

func IsBeanReceiver

func IsBeanReceiver(t reflect.Type) bool

IsBeanReceiver 返回是否是合法的 bean receiver 类型。顾名思义,bean receiver 类型就是指可以保存 bean 地址的类型。除了 ptr、interface、chan、func 四种单体类 型可以承载对应的 bean 类型之外,它们的集合类型,即 map、slice、array 类型,也是 合法的 bean receiver 类型。它们应用于以单体方式注册 bean 然后以集合方式收集 bean 的场景。

func IsBeanType

func IsBeanType(t reflect.Type) bool

IsBeanType 返回是否是 bean 类型。在 go-spring 里,变量的类型分为三种: bean 类 型、value(值) 类型以及其他类型。如果一个变量赋值给另一个变量后二者指向相同的内存地 址,则称这个变量的类型为 bean 类型,反之则称为 value(值) 类型。根据这个定义,只有 ptr、interface、chan、func 这四种类型是 bean 类型。 可能有人会问,上述四种类型的集合类型如 []interface、map[string]*struct 等也是 bean 类型吗?根据 go-spring 的定义,它们不是合法的 bean 类型,但是它们是合法的 bean receiver 类型。那为什么没有把他们也定义为 bean 类型呢?因为如果是切片类型, 那么可以转换为注册切片的元素同时加上 order 排序,如果是 map 类型,那么很显然可以转 换为依次注册 map 的元素。 另外,ptr 一般指一层指针,因为多层指针在 web 开发中很少用到,甚至应该在纯业务代码中 禁止使用多层指针。

func IsConstructor

func IsConstructor(t reflect.Type) bool

IsConstructor 返回以函数形式注册 bean 的函数是否合法。一个合法 的注册函数需要以下条件:入参可以有任意多个,支持一般形式和 Option 形式,返回值只能有一个或者两个,第一个返回值必须是 bean 源,它可以是 结构体等值类型也可以是指针等引用类型,为值类型时内部会自动转换为引用类 型(获取可引用的地址),如果有第二个返回值那么它必须是 error 类型。

func IsContextType

func IsContextType(t reflect.Type) bool

IsContextType t 是否是 context.Context 类型。

func IsErrorType

func IsErrorType(t reflect.Type) bool

IsErrorType t 是否是 error 类型。

func IsFuncType

func IsFuncType(t reflect.Type) bool

IsFuncType t 是否是 func 类型。

func IsNil

func IsNil(v reflect.Value) bool

IsNil 返回 reflect.Value 的值是否为 nil,比原生方法更安全

func IsPrimitiveValueType

func IsPrimitiveValueType(t reflect.Type) bool

IsPrimitiveValueType 返回是否是原生值类型。首先,什么是值类型?在发生赋值时,如 果传递的是数据本身而不是数据的引用,则称这种类型为值类型。那什么是原生值类型?所谓原 生值类型是指 golang 定义的 26 种基础类型里面符合值类型定义的类型。罗列下来,就是说 Bool、Int、Int8、Int16、Int32、Int64、Uint、Uint8、Uint16、Uint32、Uint64、 Float32、Float64、Complex64、Complex128、String、Struct 这些基础数据类型都 是值类型。当然,需要特别说明的是 Struct 类型必须在保证所有字段都是值类型的时候才是 值类型,只要有不是值类型的字段就不是值类型。

func IsStructPtr

func IsStructPtr(t reflect.Type) bool

IsStructPtr 返回是否是结构体的指针类型。

func IsValueType

func IsValueType(t reflect.Type) bool

IsValueType 返回是否是 value 类型。除了原生值类型,它们的集合类型也是值类型,但 是仅限于一层复合结构,即 []string、map[string]struct 这种,像 [][]string 则 不是值类型,map[string]map[string]string 也不是值类型,因为程序开发过程中,配 置项应当越明确越好,而多层次嵌套结构显然会造成信息的不明确,因此不能是值类型。

func LocalIPv4

func LocalIPv4() string

LocalIPv4 获取本机的 IPv4 地址。

func MD5

func MD5(str string) string

MD5 获取 MD5 计算后的字符串。

func MilliSeconds

func MilliSeconds(d time.Duration) int64

MilliSeconds 返回对应的毫秒时长

func NewList

func NewList(v ...interface{}) *list.List

NewList 使用输入的元素创建列表。

func PatchValue

func PatchValue(v reflect.Value) reflect.Value

PatchValue 开放 v 的私有字段,返回修改后的副本。

func ReturnNothing

func ReturnNothing(t reflect.Type) bool

ReturnNothing 函数是否无返回值。

func ReturnOnlyError

func ReturnOnlyError(t reflect.Type) bool

ReturnOnlyError 函数是否只返回错误值。

func SafeCloseChan

func SafeCloseChan(ch chan struct{})

SafeCloseChan 安全地关闭一个通道。

func TripleSort

func TripleSort(sorting *list.List, fn GetBeforeItems) *list.List

TripleSort 三路排序

func TypeName

func TypeName(i interface{}) string

TypeName 返回原始类型的全限定名,类型的全限定名用于严格区分相同名称的 bean 对象。 类型的全限定名是指包的全路径加上类型名称,例如,gs 包里面的 Container 类型,它的 类型全限定名是 github.com/go-spring/spring-core/gs/gs.Container。因为 go 语言允许在不同的路径下存在名称相同的包,所以有可能出现(简单)类型名称相同、实例名称 相同的但实际上类型不相同的 bean 对象,因此有类型的全限定名这样的概念,用以严格区分 同名的 bean 对象。

func TypeOf

func TypeOf(i interface{}) reflect.Type

TypeOf 获取任意数据的真实类型。

Types

type GetBeforeItems

type GetBeforeItems func(sorting *list.List, current interface{}) *list.List

GetBeforeItems 获取 sorting 中排在 current 前面的元素

type PanicCond

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

PanicCond 封装触发 panic 的条件。

func NewPanicCond

func NewPanicCond(fn func() interface{}) *PanicCond

NewPanicCond PanicCond 的构造函数。

func Panic

func Panic(err error) *PanicCond

Panic 抛出一个异常值。

func Panicf

func Panicf(format string, a ...interface{}) *PanicCond

Panicf 抛出一段需要格式化的错误字符串。

func (*PanicCond) When

func (p *PanicCond) When(isPanic bool)

When 满足给定条件时抛出一个 panic 。

Jump to

Keyboard shortcuts

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