优化mapping解析

This commit is contained in:
白茶清欢 2023-02-01 20:30:32 +08:00
parent a3caefef3f
commit cd9bc4aadb
2 changed files with 22 additions and 54 deletions

View File

@ -57,13 +57,6 @@ func SetMappingLocationList(locationList []string) {
}
}
const (
// PriorityLocation 读取位置优先
PriorityLocation = "location"
// PriorityField 读取字段优先
PriorityField = "field"
)
// MappingRuleItem 规则
//
// Author : go_developer@163.com<白茶清欢>
@ -74,29 +67,18 @@ type MappingRuleItem struct {
Field string `json:"field"` // 查询的字段名称
}
// MappingRule 数据映射规则
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:24 2023/2/1
type MappingRule struct {
RuleList []MappingRuleItem `json:"rule_list"` // 规则列表
Priority string `json:"priority"` // 查找优先级 : location - 位置优先 field - 字段优先
TargetField string `json:"target_field"` // 目标字段
}
// StructField 结构体字段信息
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:35 2023/2/1
type StructField struct {
Idx int // 字段在结构体的索引
Name string // 字段名
Type reflect.Kind // 字段类型
JsonTag string // json标签
EventTag string // 事件标签
MappingRule *MappingRule // 数据映射规则
Idx int // 字段在结构体的索引
Name string // 字段名
Type reflect.Kind // 字段类型
JsonTag string // json标签
EventTag string // 事件标签
MappingRuleList []MappingRuleItem // 规则列表
}
// StructInfo 结构体信息

View File

@ -69,14 +69,14 @@ func (rt *ReflectType) Do(dataFlag string, data interface{}) *StructInfo {
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,
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),
MappingRuleList: make([]MappingRuleItem, 0),
}
rt.fillMappingRule(field, reflectType.Field(idx).Tag.Get(MappingTag))
res.StructFieldList = append(res.StructFieldList, field)
}
return res
@ -101,7 +101,7 @@ func (rt *ReflectType) fillTagInfo(field *StructField) {
}
}
// 没有设置event tag,则和
// 没有设置event tag,则和 json tag保持一致
if len(field.EventTag) == 0 {
field.EventTag = field.JsonTag
}
@ -114,29 +114,16 @@ func (rt *ReflectType) fillTagInfo(field *StructField) {
// 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
}
func (rt *ReflectType) fillMappingRule(field *StructField, inputMappingVal string) {
if len(inputMappingVal) == 0 {
// 没有指定规则, 有默认规则
for _, location := range mappingLocationList {
rule.RuleList = append(rule.RuleList, MappingRuleItem{
field.MappingRuleList = append(field.MappingRuleList, MappingRuleItem{
Location: location,
Field: field.JsonTag,
})
}
field.MappingRule = rule
return
}
mappingArr := strings.Split(inputMappingVal, ",")
@ -148,7 +135,6 @@ func (rt *ReflectType) fillMappingRule(field *StructField, inputMappingVal strin
// 配置格式错误, 跳过
continue
}
rule.TargetField = strings.TrimSpace(itemArr[0])
mapRuleArr := strings.Split(strings.TrimSpace(itemArr[1]), "|")
for _, itemMapRule := range mapRuleArr {
itemMapRule = strings.TrimLeft(itemMapRule, "#")
@ -163,25 +149,25 @@ func (rt *ReflectType) fillMappingRule(field *StructField, inputMappingVal strin
switch itemMapRuleArr[0] {
// 从header读取
case MappingLocationHeader:
rule.RuleList = append(rule.RuleList, MappingRuleItem{
field.MappingRuleList = append(field.MappingRuleList, MappingRuleItem{
Location: MappingLocationHeader,
Field: itemMapRuleArr[1],
})
// 从请求参数读取
case MappingLocationParam:
rule.RuleList = append(rule.RuleList, MappingRuleItem{
field.MappingRuleList = append(field.MappingRuleList, MappingRuleItem{
Location: MappingLocationParam,
Field: itemMapRuleArr[1],
})
// 从响应数据读取
case MappingLocationResponse:
rule.RuleList = append(rule.RuleList, MappingRuleItem{
field.MappingRuleList = append(field.MappingRuleList, MappingRuleItem{
Location: MappingLocationResponse,
Field: itemMapRuleArr[1],
})
// 从扩展数据读取
case MappingLocationExtension:
rule.RuleList = append(rule.RuleList, MappingRuleItem{
field.MappingRuleList = append(field.MappingRuleList, MappingRuleItem{
Location: MappingLocationExtension,
Field: itemMapRuleArr[1],
})
@ -190,7 +176,7 @@ func (rt *ReflectType) fillMappingRule(field *StructField, inputMappingVal strin
fallthrough
default:
for _, itemLocation := range mappingLocationList {
rule.RuleList = append(rule.RuleList, MappingRuleItem{
field.MappingRuleList = append(field.MappingRuleList, MappingRuleItem{
Location: itemLocation,
Field: itemMapRuleArr[1],
})