energy/ipc/ipc-invoke.go

158 lines
4.5 KiB
Go
Raw Normal View History

2023-03-11 13:50:09 +08:00
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------
package ipc
import (
"fmt"
jsoniter "github.com/json-iterator/go"
"reflect"
)
// callback IPC 监听回调函数
type callback struct {
context *contextCallback //上下文参数回调
argument *argumentCallback //参数回调
}
// contextCallback 带上下文的回调函数
type contextCallback struct {
callback EmitContextCallback
}
// argumentCallback 带参数的回调函数
type argumentCallback struct {
2023-03-11 14:24:22 +08:00
callback *reflect.Value //回调函数
2023-03-11 13:50:09 +08:00
}
// ContextCallback 返回上下文参数回调
func (m *callback) ContextCallback() EmitContextCallback {
if m.context == nil {
return nil
}
return m.context.callback
}
// ArgumentCallback 参数回调
func (m *callback) ArgumentCallback() *argumentCallback {
if m.argument == nil {
return nil
}
return m.argument
}
// Invoke 调用函数
func (m *argumentCallback) Invoke(context IContext) {
argsList := context.ArgumentList()
argsSize := argsList.Size()
2023-03-11 14:24:22 +08:00
rt := m.callback.Type()
inArgsCount := rt.NumIn()
var inArgsValues = make([]reflect.Value, inArgsCount)
for i := 0; i < inArgsCount; i++ {
inType := rt.In(i)
2023-03-11 13:50:09 +08:00
if i < argsSize {
argsValue := argsList.GetByIndex(i)
if argsValue == nil {
2023-03-11 14:24:22 +08:00
inArgsValues[i] = reflect.New(inType).Elem()
2023-03-11 13:50:09 +08:00
continue
}
2023-03-11 14:24:22 +08:00
switch inType.Kind() {
case reflect.String:
2023-03-11 13:50:09 +08:00
inArgsValues[i] = reflect.ValueOf(argsValue.String())
2023-03-11 14:24:22 +08:00
case reflect.Int:
2023-03-11 13:50:09 +08:00
inArgsValues[i] = reflect.ValueOf(argsValue.Int())
2023-03-11 14:24:22 +08:00
case reflect.Int8:
2023-03-11 13:50:09 +08:00
inArgsValues[i] = reflect.ValueOf(int8(argsValue.Int()))
2023-03-11 14:24:22 +08:00
case reflect.Int16:
2023-03-11 13:50:09 +08:00
inArgsValues[i] = reflect.ValueOf(int16(argsValue.Int()))
2023-03-11 14:24:22 +08:00
case reflect.Int32:
2023-03-11 13:50:09 +08:00
inArgsValues[i] = reflect.ValueOf(int32(argsValue.Int()))
2023-03-11 14:24:22 +08:00
case reflect.Int64:
2023-03-11 13:50:09 +08:00
inArgsValues[i] = reflect.ValueOf(int64(argsValue.Int()))
2023-03-11 14:24:22 +08:00
case reflect.Uint:
2023-03-11 13:50:09 +08:00
inArgsValues[i] = reflect.ValueOf(uint(argsValue.Int()))
2023-03-11 14:24:22 +08:00
case reflect.Uint8:
2023-03-11 13:50:09 +08:00
inArgsValues[i] = reflect.ValueOf(uint8(argsValue.Int()))
2023-03-11 14:24:22 +08:00
case reflect.Uint16:
2023-03-11 13:50:09 +08:00
inArgsValues[i] = reflect.ValueOf(uint16(argsValue.Int()))
2023-03-11 14:24:22 +08:00
case reflect.Uint32:
2023-03-11 13:50:09 +08:00
inArgsValues[i] = reflect.ValueOf(uint32(argsValue.Int()))
2023-03-11 14:24:22 +08:00
case reflect.Uint64:
2023-03-11 13:50:09 +08:00
inArgsValues[i] = reflect.ValueOf(uint64(argsValue.Int()))
2023-03-11 14:24:22 +08:00
case reflect.Uintptr:
2023-03-11 13:50:09 +08:00
inArgsValues[i] = reflect.ValueOf(uintptr(argsValue.Int()))
2023-03-11 14:24:22 +08:00
case reflect.Float32:
2023-03-11 13:50:09 +08:00
inArgsValues[i] = reflect.ValueOf(float32(argsValue.Float()))
2023-03-11 14:24:22 +08:00
case reflect.Float64:
2023-03-11 13:50:09 +08:00
inArgsValues[i] = reflect.ValueOf(argsValue.Float())
2023-03-11 14:24:22 +08:00
case reflect.Bool:
2023-03-11 13:50:09 +08:00
inArgsValues[i] = reflect.ValueOf(argsValue.Bool())
2023-03-11 14:24:22 +08:00
case reflect.Struct:
2023-03-11 13:50:09 +08:00
if argsValue.IsObject() {
// struct
if jsonBytes := argsValue.Bytes(); jsonBytes != nil {
2023-03-11 14:24:22 +08:00
v := reflect.New(inType)
2023-03-11 13:50:09 +08:00
if err := jsoniter.Unmarshal(jsonBytes, v.Interface()); err == nil {
inArgsValues[i] = v.Elem()
continue
}
}
}
2023-03-11 14:24:22 +08:00
inArgsValues[i] = reflect.New(inType).Elem()
case reflect.Map:
2023-03-11 13:50:09 +08:00
if argsValue.IsObject() {
// map key=string : value != interface
2023-03-11 14:24:22 +08:00
if inType.Elem().Kind() != reflect.Interface {
2023-03-11 13:50:09 +08:00
if jsonBytes := argsValue.Bytes(); jsonBytes != nil {
2023-03-11 14:24:22 +08:00
vv := reflect.New(inType)
2023-03-11 13:50:09 +08:00
if err := jsoniter.Unmarshal(jsonBytes, vv.Interface()); err == nil {
inArgsValues[i] = vv.Elem()
continue
}
}
2023-03-11 14:24:22 +08:00
inArgsValues[i] = reflect.New(inType).Elem()
2023-03-11 13:50:09 +08:00
} else {
inArgsValues[i] = reflect.ValueOf(argsValue.Data())
}
} else {
2023-03-11 14:24:22 +08:00
inArgsValues[i] = reflect.New(inType).Elem()
2023-03-11 13:50:09 +08:00
}
2023-03-11 14:24:22 +08:00
case reflect.Slice:
2023-03-11 13:50:09 +08:00
if argsValue.IsArray() {
// slick value != interface
2023-03-11 14:24:22 +08:00
if inType.Elem().Kind() != reflect.Interface {
2023-03-11 13:50:09 +08:00
if jsonBytes := argsValue.Bytes(); jsonBytes != nil {
2023-03-11 14:24:22 +08:00
vv := reflect.New(inType)
2023-03-11 13:50:09 +08:00
if err := jsoniter.Unmarshal(jsonBytes, vv.Interface()); err == nil {
inArgsValues[i] = vv.Elem()
continue
}
}
2023-03-11 14:24:22 +08:00
inArgsValues[i] = reflect.New(inType).Elem()
2023-03-11 13:50:09 +08:00
} else {
inArgsValues[i] = reflect.ValueOf(argsValue.Data())
}
} else {
2023-03-11 14:24:22 +08:00
inArgsValues[i] = reflect.New(inType).Elem()
2023-03-11 13:50:09 +08:00
}
default:
2023-03-11 14:24:22 +08:00
inArgsValues[i] = reflect.New(inType).Elem()
2023-03-11 13:50:09 +08:00
}
} else {
2023-03-11 14:24:22 +08:00
inArgsValues[i] = reflect.New(inType).Elem()
2023-03-11 13:50:09 +08:00
}
}
// call
2023-03-11 14:24:22 +08:00
resultValues := m.callback.Call(inArgsValues)
2023-03-11 13:50:09 +08:00
// call result
fmt.Println("resultValues:", resultValues)
2023-03-11 14:24:22 +08:00
//context.Result()
2023-03-11 13:50:09 +08:00
}