Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Jul 29, 2024
1 parent 18a86c6 commit bcd5b60
Show file tree
Hide file tree
Showing 13 changed files with 293 additions and 44 deletions.
2 changes: 1 addition & 1 deletion pkg/lakego-pkg/go-events/events/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Event struct {
func (this *Event) Observe(observer any, prefix string, sort int) *Event {
observerKind := reflect.TypeOf(observer).Kind()
if observerKind != reflect.Struct && observerKind != reflect.Pointer {
return this
panic("go-events: observe input type error")
}

if ob, ok := observer.(ISubscribePrefix); ok {
Expand Down
4 changes: 0 additions & 4 deletions pkg/lakego-pkg/go-events/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ package events
// 默认排序
const DefaultSort = 1;

// 默认事件
// default new events
var Default = New()

/**
* Events
*
Expand Down
27 changes: 27 additions & 0 deletions pkg/lakego-pkg/go-events/events/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,3 +775,30 @@ func Test_Struct_fail_5(t *testing.T) {
action.Trigger("Test_Struct_fail_5", data1)
}

type TestEventObserveOn struct{}

func (this TestEventObserveOn) OnTestEvent(data any) {
testEventRes["TestEventObserveOn"] = data
}

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

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

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

action := NewAction()
action.Subscribe(TestEventObserveOn{})
action.Subscribe("test")

data1 := "init77889"
action.Trigger("TestEvent", data1)

eq(testEventRes["TestEventObserveOn"], "init77889", "Test_EventObserveOn")
}
26 changes: 18 additions & 8 deletions pkg/lakego-pkg/go-events/events/helper.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,59 @@
package events

// 默认事件
// default new events
var defaultEvent = New()

// 默认事件
// default new events
func Default() *Events {
return defaultEvent
}

// 注册操作
// Add Action
func AddAction(event any, listener any, sort int) {
Default.Action().Listen(event, listener, sort)
defaultEvent.Action().Listen(event, listener, sort)
}

// 触发操作
// Do Action
func DoAction(event any, params ...any) {
Default.Action().Trigger(event, params...)
defaultEvent.Action().Trigger(event, params...)
}

// 移除操作
// Remove Action
func RemoveAction(event string, listener any, sort int) bool {
return Default.Action().RemoveListener(event, listener, sort)
return defaultEvent.Action().RemoveListener(event, listener, sort)
}

// 是否有操作
// Has Action
func HasAction(event string, listener any) bool {
return Default.Action().HasListener(event, listener)
return defaultEvent.Action().HasListener(event, listener)
}

// 注册过滤器
// Add Filter
func AddFilter(event any, listener any, sort int) {
Default.Filter().Listen(event, listener, sort)
defaultEvent.Filter().Listen(event, listener, sort)
}

// 触发过滤器
// Apply Filters
func ApplyFilters(event any, params ...any) any {
return Default.Filter().Trigger(event, params...)
return defaultEvent.Filter().Trigger(event, params...)
}

// 移除过滤器
// Remove Filter
func RemoveFilter(event string, listener any, sort int) bool {
return Default.Filter().RemoveListener(event, listener, sort)
return defaultEvent.Filter().RemoveListener(event, listener, sort)
}

// 是否有过滤器
// Has Filter
func HasFilter(event string, listener any) bool {
return Default.Filter().HasListener(event, listener)
return defaultEvent.Filter().HasListener(event, listener)
}
18 changes: 0 additions & 18 deletions pkg/lakego-pkg/go-events/events/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,10 @@ package events

import (
"reflect"
"regexp"
"runtime"
"strings"
)

// 匹配检测
// match Type Name
func matchTypeName(typeName string, current string) bool {
if strings.Index(typeName, "*") == -1 {
return typeName == current
}

typeName = strings.Replace(typeName, "*", `([0-9a-zA-Z-_.:])*`, -1)

result, _ := regexp.MatchString("^"+typeName, current)
if !result {
return false
}

return true
}

// 获取方法名称
// get Func Name
func getFuncName(data any) string {
Expand Down
6 changes: 6 additions & 0 deletions pkg/lakego-pkg/go-goch/goch/goch.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,9 @@ func ToDurationSlice(i any) []time.Duration {
v, _ := ToDurationSliceE(i)
return v
}

// ToJSON casts an interface to []type.
func ToJSON(i any) string {
v, _ := ToJSONE(i)
return v
}
39 changes: 39 additions & 0 deletions pkg/lakego-pkg/go-goch/goch/goch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package goch

import (
"testing"
"reflect"
)

func AssertEqualT(t *testing.T) func(any, any, string) {
return func(actual any, expected any, msg string) {
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Failed %s: actual: %v, expected: %v", msg, actual, expected)
}
}
}

func Test_ToJSON(t *testing.T) {
eq := AssertEqualT(t)

test := map[string]any{
"test1": "value1",
"test5": "value5",
}

res := ToJSON(test)
check := `{"test1":"value1","test5":"value5"}`

eq(string(res), check, "Test_ToJSON")
}

func Test_ToString(t *testing.T) {
eq := AssertEqualT(t)

test := []byte("test1")

res := ToString(test)
check := `test1`

eq(string(res), check, "Test_ToString")
}
Loading

0 comments on commit bcd5b60

Please sign in to comment.