1 Star 0 Fork 0

574893508/golang-socketio

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
caller.go 1.57 KB
一键复制 编辑 原始数据 按行查看 历史
package gosocketio
import (
"errors"
"reflect"
)
type caller struct {
Func reflect.Value
Args reflect.Type
ArgsPresent bool
Out bool
}
var (
ErrorCallerNotFunc = errors.New("f is not function")
ErrorCallerNot2Args = errors.New("f should have 1 or 2 args")
ErrorCallerMaxOneValue = errors.New("f should return not more than one value")
)
/**
Parses function passed by using reflection, and stores its representation
for further call on message or ack
*/
func newCaller(f interface{}) (*caller, error) {
fVal := reflect.ValueOf(f)
if fVal.Kind() != reflect.Func {
return nil, ErrorCallerNotFunc
}
fType := fVal.Type()
if fType.NumOut() > 1 {
return nil, ErrorCallerMaxOneValue
}
curCaller := &caller{
Func: fVal,
Out: fType.NumOut() == 1,
}
if fType.NumIn() == 1 {
curCaller.Args = nil
curCaller.ArgsPresent = false
} else if fType.NumIn() == 2 {
curCaller.Args = fType.In(1)
curCaller.ArgsPresent = true
} else {
return nil, ErrorCallerNot2Args
}
return curCaller, nil
}
/**
returns function parameter as it is present in it using reflection
*/
func (c *caller) getArgs() interface{} {
return reflect.New(c.Args).Interface()
}
/**
calls function with given arguments from its representation using reflection
*/
func (c *caller) callFunc(h *Channel, args interface{}) []reflect.Value {
//nil is untyped, so use the default empty value of correct type
if args == nil {
args = c.getArgs()
}
a := []reflect.Value{reflect.ValueOf(h), reflect.ValueOf(args).Elem()}
if !c.ArgsPresent {
a = a[0:1]
}
return c.Func.Call(a)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zyl1221/golang-socketio.git
[email protected]:zyl1221/golang-socketio.git
zyl1221
golang-socketio
golang-socketio
master

搜索帮助