event/send.go

51 lines
1.3 KiB
Go

// Package event ...
//
// Description : event ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-02-02 15:15
package event
import (
"encoding/json"
"git.zhangdeman.cn/zhangdeman/event/abstract"
)
// SendEvent 发送事件
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:16 2023/2/2
func SendEvent(dataFlag string, data interface{}, preSendHandler abstract.IPreSendHandler, sendEventHandler abstract.ISendEventHandler) {
if nil == preSendHandler {
preSendHandler = &DefaultPreSendHandler{}
}
if nil == sendEventHandler {
sendEventHandler = &DefaultSendEventHandler{}
}
// noSendReason, isNeedSend := preSendHandler.NeedSend()
_, isNeedSend := preSendHandler.NeedSend()
res := SendResult{}
if !isNeedSend {
sendEventHandler.NoSendCallback(data, res)
return
}
// 通过反射填充数据
ReflectValueInstance.Do(dataFlag, data, preSendHandler)
if byteData, err := json.Marshal(data); nil != err {
sendEventHandler.FailCallback(map[string]interface{}{
"err": err.Error(),
"reason": "data marshal fail",
}, err)
} else {
sendResult, sendErr := sendEventHandler.Send(byteData)
if nil != sendErr {
sendEventHandler.FailCallback(sendResult, sendErr)
return
}
sendEventHandler.SuccessCallback(sendResult)
}
}