event/send.go

58 lines
1.4 KiB
Go
Raw Normal View History

2023-02-02 15:22:26 +08:00
// 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
2023-02-02 17:23:41 +08:00
func SendEvent(dataFlag string, data interface{}, preSendHandler abstract.IPreSendHandler, sendEventHandler abstract.ISendEventHandler) SendResult {
2023-02-02 16:41:30 +08:00
if nil == preSendHandler {
preSendHandler = &DefaultPreSendHandler{}
}
if nil == sendEventHandler {
sendEventHandler = &DefaultSendEventHandler{}
}
res := SendResult{}
2023-02-02 17:23:41 +08:00
if res.NoSendDetail, res.IsNeedSend = preSendHandler.NeedSend(); !res.IsNeedSend {
sendEventHandler.NoSendCallback(data, res)
2023-02-02 17:23:41 +08:00
return res
}
2023-02-02 15:22:26 +08:00
// 通过反射填充数据
ReflectValueInstance.Do(dataFlag, data, preSendHandler)
2023-02-02 17:23:41 +08:00
res.Data = data
var (
byteData []byte
err error
)
if byteData, err = json.Marshal(data); nil != err {
res.IsSuccess = false
res.FailReason = err.Error()
2023-02-02 15:22:26 +08:00
sendEventHandler.FailCallback(map[string]interface{}{
"err": err.Error(),
"reason": "data marshal fail",
}, err)
2023-02-02 17:23:41 +08:00
return res
}
if res.SendResult, err = sendEventHandler.Send(byteData); nil != err {
sendEventHandler.FailCallback(res.SendResult, err)
return res
2023-02-02 15:22:26 +08:00
}
2023-02-02 17:23:41 +08:00
sendEventHandler.SuccessCallback(res.SendResult)
return res
2023-02-02 15:22:26 +08:00
}