Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Jul 28, 2024
1 parent 88d5c10 commit 59e3181
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 22 deletions.
39 changes: 39 additions & 0 deletions events/events_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package events

import (
"fmt"
"reflect"
"testing"
)
Expand Down Expand Up @@ -441,3 +442,41 @@ func Test_ReflectValue(t *testing.T) {

eq(testEventRes["testReflectValueFunc"], "init6", "Test_ReflectValue")
}

func Test_Struct_fail(t *testing.T) {
eq := assertDeepEqualT(t)

defer func() {
if e := recover(); e != nil {
err := fmt.Sprintf("%v", e)

check := "go-events: struct type error"
eq(err, check, "Struct failed")
}
}()

action := NewAction()
action.Listen("Test_Struct_fail", "testReflectValueFunc", DefaultSort)

data1 := "init6"
action.Trigger("Test_Struct_fail", data1)
}

func Test_Struct_fail_2(t *testing.T) {
eq := assertDeepEqualT(t)

defer func() {
if e := recover(); e != nil {
err := fmt.Sprintf("%v", e)

check := "go-events: call func type error"
eq(err, check, "Struct failed 2")
}
}()

action := NewAction()
action.Listen("Test_Struct_fail_2", reflect.ValueOf("testReflectValueFunc"), DefaultSort)

data1 := "init6"
action.Trigger("Test_Struct_fail_2", data1)
}
1 change: 1 addition & 0 deletions events/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (this *Filter) Subscribe(subscribers ...any) *Filter {
return this
}

// Trigger func
func (this *Filter) Trigger(event any, params ...any) any {
this.mu.RLock()
defer this.mu.RUnlock()
Expand Down
55 changes: 33 additions & 22 deletions events/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewPool() *Pool {
func (this *Pool) CallFunc(fn any, args []any) any {
val := reflect.ValueOf(fn)
if val.Kind() != reflect.Func {
panic("go-events: call func type error")
panic("go-events: func type error")
}

return this.Call(val, args)
Expand All @@ -30,7 +30,6 @@ func (this *Pool) CallFunc(fn any, args []any) any {
// listen struct
func (this *Pool) CallStructMethod(in any, method string, args []any) any {
val := reflect.ValueOf(in)

if val.Kind() != reflect.Pointer && val.Kind() != reflect.Struct {
panic("go-events: struct type error")
}
Expand All @@ -41,24 +40,18 @@ func (this *Pool) CallStructMethod(in any, method string, args []any) any {

// Call Func
func (this *Pool) Call(fn reflect.Value, args []any) any {
if !fn.IsValid() {
if fn.Kind() != reflect.Func {
panic("go-events: call func type error")
}

fnType := fn.Type()

numIn := fnType.NumIn()
if len(args) != numIn {
err := fmt.Sprintf("go-events: func params error (args %d, func args %d)", len(args), numIn)
panic(err)
if !fn.IsValid() {
panic("go-events: call func valid error")
}

fnType := fn.Type()

// 参数
params := make([]reflect.Value, 0)
for i := 0; i < numIn; i++ {
dataValue := this.convertTo(fnType.In(i), args[i])
params = append(params, dataValue)
}
params := this.bindParams(fnType, args)

res := fn.Call(params)
if len(res) == 0 {
Expand All @@ -70,22 +63,40 @@ func (this *Pool) Call(fn reflect.Value, args []any) any {

// is Struct
func (this *Pool) IsStruct(in any) bool {
typ := reflect.ValueOf(in)
if typ.Kind() != reflect.Pointer && typ.Kind() != reflect.Struct {
return false
val := reflect.ValueOf(in)
if val.Kind() == reflect.Pointer || val.Kind() == reflect.Struct {
return true
}

return true
return false
}

// is Func
func (this *Pool) IsFunc(in any) bool {
fnObject := reflect.ValueOf(in)
if !(fnObject.IsValid() && fnObject.Kind() == reflect.Func) {
return false
val := reflect.ValueOf(in)
if val.Kind() == reflect.Func {
return true
}

return false
}

// bind params
func (this *Pool) bindParams(fnType reflect.Type, args []any) []reflect.Value {
numIn := fnType.NumIn()
if len(args) != numIn {
err := fmt.Sprintf("go-events: func params error (args %d, func args %d)", len(args), numIn)
panic(err)
}

// 参数
params := make([]reflect.Value, 0)
for i := 0; i < numIn; i++ {
dataValue := this.convertTo(fnType.In(i), args[i])
params = append(params, dataValue)
}

return true
return params
}

// src convert type to new typ
Expand Down

0 comments on commit 59e3181

Please sign in to comment.