2023-02-01 14:41:09 +08:00
|
|
|
// Package event ...
|
|
|
|
//
|
|
|
|
// Description : event ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2023-02-01 14:30
|
|
|
|
package event
|
|
|
|
|
2023-02-01 18:46:18 +08:00
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ReflectTypeInstance 反射实例
|
|
|
|
ReflectTypeInstance *ReflectType
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
ReflectTypeInstance = &ReflectType{
|
|
|
|
lock: &sync.RWMutex{},
|
|
|
|
cacheTable: make(map[string]*StructInfo),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 14:41:09 +08:00
|
|
|
// ReflectType 反射数据类型
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 14:31 2023/2/1
|
|
|
|
type ReflectType struct {
|
2023-02-01 18:46:18 +08:00
|
|
|
// 数据锁
|
|
|
|
lock *sync.RWMutex
|
|
|
|
// 反射结果缓存
|
|
|
|
cacheTable map[string]*StructInfo
|
2023-02-01 14:41:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do 反射获取数据类型
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 14:34 2023/2/1
|
2023-02-01 18:46:18 +08:00
|
|
|
//
|
|
|
|
// 为特定结构体生成全局唯一的标识, 并进行缓存, 加速反射结果获取
|
|
|
|
func (rt *ReflectType) Do(dataFlag string, data interface{}) *StructInfo {
|
|
|
|
rt.lock.Lock()
|
|
|
|
defer rt.lock.Unlock()
|
|
|
|
if cacheResult, exist := rt.cacheTable[dataFlag]; exist {
|
|
|
|
// 缓存存在, 直接是有缓存结果
|
|
|
|
return cacheResult
|
|
|
|
}
|
2023-02-01 14:41:09 +08:00
|
|
|
|
2023-02-01 18:46:18 +08:00
|
|
|
// 缓存不存在, 解析
|
|
|
|
res := &StructInfo{
|
|
|
|
Flag: dataFlag,
|
|
|
|
IsStruct: false,
|
|
|
|
StructFieldList: make([]*StructField, 0),
|
|
|
|
}
|
|
|
|
reflectType := reflect.TypeOf(data)
|
|
|
|
if reflectType.Kind() == reflect.Ptr {
|
|
|
|
reflectType = reflectType.Elem()
|
|
|
|
}
|
|
|
|
if reflectType.Kind() != reflect.Struct {
|
|
|
|
// 非结构体,无需反射
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
res.IsStruct = true
|
|
|
|
for idx := 0; idx < reflectType.NumField(); idx++ {
|
|
|
|
field := &StructField{
|
|
|
|
Idx: idx,
|
|
|
|
Name: reflectType.Field(idx).Name,
|
|
|
|
Type: reflectType.Field(idx).Type.Kind(),
|
|
|
|
JsonTag: reflectType.Field(idx).Tag.Get(JsonTag),
|
|
|
|
EventTag: reflectType.Field(idx).Tag.Get(OutEventTag),
|
|
|
|
MappingRule: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
res.StructFieldList = append(res.StructFieldList, field)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// fillTagInfo 填充标签信息
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 17:11 2023/2/1
|
|
|
|
func (rt *ReflectType) fillTagInfo(field *StructField) {
|
|
|
|
if len(field.JsonTag) == 0 {
|
|
|
|
field.JsonTag = field.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// jsonTag 去掉 omitempty
|
|
|
|
jsonTagValArr := strings.Split(field.JsonTag, ",")
|
|
|
|
for _, item := range jsonTagValArr {
|
|
|
|
if len(item) > 0 && item != OmitemptyTag {
|
|
|
|
field.JsonTag = item
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 没有设置event tag,则和
|
|
|
|
if len(field.EventTag) == 0 {
|
|
|
|
field.EventTag = field.JsonTag
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fillMappingRule 解析参数映射规则
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 17:14 2023/2/1
|
|
|
|
//
|
|
|
|
// mapping:"user_id:param#user_id|header#id"
|
|
|
|
func (rt *ReflectType) fillMappingRule(field *StructField, inputMappingVal string, inputPriorityVal string) {
|
|
|
|
rule := &MappingRule{
|
|
|
|
RuleList: make([]MappingRuleItem, 0),
|
|
|
|
Priority: "",
|
|
|
|
TargetField: "",
|
|
|
|
}
|
|
|
|
switch inputPriorityVal {
|
|
|
|
case PriorityLocation:
|
|
|
|
rule.Priority = PriorityLocation
|
|
|
|
case PriorityField:
|
|
|
|
rule.Priority = PriorityField
|
|
|
|
default:
|
|
|
|
rule.Priority = PriorityField
|
|
|
|
}
|
|
|
|
if len(inputMappingVal) == 0 {
|
|
|
|
// 没有指定规则, 有默认规则
|
|
|
|
for _, location := range mappingLocationList {
|
|
|
|
rule.RuleList = append(rule.RuleList, MappingRuleItem{
|
|
|
|
Location: location,
|
|
|
|
Field: field.JsonTag,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
field.MappingRule = rule
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mappingArr := strings.Split(inputMappingVal, ",")
|
|
|
|
for _, item := range mappingArr {
|
|
|
|
item = strings.TrimSpace(item)
|
|
|
|
// 要赋值的字段名
|
|
|
|
itemArr := strings.Split(item, ":")
|
|
|
|
rule.TargetField = strings.TrimSpace(itemArr[0])
|
|
|
|
}
|
2023-02-01 14:41:09 +08:00
|
|
|
}
|