优化mapping解析
This commit is contained in:
parent
a3caefef3f
commit
cd9bc4aadb
30
define.go
30
define.go
@ -57,13 +57,6 @@ func SetMappingLocationList(locationList []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
|
||||||
// PriorityLocation 读取位置优先
|
|
||||||
PriorityLocation = "location"
|
|
||||||
// PriorityField 读取字段优先
|
|
||||||
PriorityField = "field"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MappingRuleItem 规则
|
// MappingRuleItem 规则
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
@ -74,29 +67,18 @@ type MappingRuleItem struct {
|
|||||||
Field string `json:"field"` // 查询的字段名称
|
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 结构体字段信息
|
// StructField 结构体字段信息
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 14:35 2023/2/1
|
// Date : 14:35 2023/2/1
|
||||||
type StructField struct {
|
type StructField struct {
|
||||||
Idx int // 字段在结构体的索引
|
Idx int // 字段在结构体的索引
|
||||||
Name string // 字段名
|
Name string // 字段名
|
||||||
Type reflect.Kind // 字段类型
|
Type reflect.Kind // 字段类型
|
||||||
JsonTag string // json标签
|
JsonTag string // json标签
|
||||||
EventTag string // 事件标签
|
EventTag string // 事件标签
|
||||||
MappingRule *MappingRule // 数据映射规则
|
MappingRuleList []MappingRuleItem // 规则列表
|
||||||
}
|
}
|
||||||
|
|
||||||
// StructInfo 结构体信息
|
// StructInfo 结构体信息
|
||||||
|
46
reflect.go
46
reflect.go
@ -69,14 +69,14 @@ func (rt *ReflectType) Do(dataFlag string, data interface{}) *StructInfo {
|
|||||||
res.IsStruct = true
|
res.IsStruct = true
|
||||||
for idx := 0; idx < reflectType.NumField(); idx++ {
|
for idx := 0; idx < reflectType.NumField(); idx++ {
|
||||||
field := &StructField{
|
field := &StructField{
|
||||||
Idx: idx,
|
Idx: idx,
|
||||||
Name: reflectType.Field(idx).Name,
|
Name: reflectType.Field(idx).Name,
|
||||||
Type: reflectType.Field(idx).Type.Kind(),
|
Type: reflectType.Field(idx).Type.Kind(),
|
||||||
JsonTag: reflectType.Field(idx).Tag.Get(JsonTag),
|
JsonTag: reflectType.Field(idx).Tag.Get(JsonTag),
|
||||||
EventTag: reflectType.Field(idx).Tag.Get(OutEventTag),
|
EventTag: reflectType.Field(idx).Tag.Get(OutEventTag),
|
||||||
MappingRule: nil,
|
MappingRuleList: make([]MappingRuleItem, 0),
|
||||||
}
|
}
|
||||||
|
rt.fillMappingRule(field, reflectType.Field(idx).Tag.Get(MappingTag))
|
||||||
res.StructFieldList = append(res.StructFieldList, field)
|
res.StructFieldList = append(res.StructFieldList, field)
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
@ -101,7 +101,7 @@ func (rt *ReflectType) fillTagInfo(field *StructField) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 没有设置event tag,则和
|
// 没有设置event tag,则和 json tag保持一致
|
||||||
if len(field.EventTag) == 0 {
|
if len(field.EventTag) == 0 {
|
||||||
field.EventTag = field.JsonTag
|
field.EventTag = field.JsonTag
|
||||||
}
|
}
|
||||||
@ -114,29 +114,16 @@ func (rt *ReflectType) fillTagInfo(field *StructField) {
|
|||||||
// Date : 17:14 2023/2/1
|
// Date : 17:14 2023/2/1
|
||||||
//
|
//
|
||||||
// mapping:"user_id:param#user_id|header#id"
|
// mapping:"user_id:param#user_id|header#id"
|
||||||
func (rt *ReflectType) fillMappingRule(field *StructField, inputMappingVal string, inputPriorityVal string) {
|
func (rt *ReflectType) fillMappingRule(field *StructField, inputMappingVal 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 {
|
if len(inputMappingVal) == 0 {
|
||||||
// 没有指定规则, 有默认规则
|
// 没有指定规则, 有默认规则
|
||||||
for _, location := range mappingLocationList {
|
for _, location := range mappingLocationList {
|
||||||
rule.RuleList = append(rule.RuleList, MappingRuleItem{
|
field.MappingRuleList = append(field.MappingRuleList, MappingRuleItem{
|
||||||
Location: location,
|
Location: location,
|
||||||
Field: field.JsonTag,
|
Field: field.JsonTag,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
field.MappingRule = rule
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
mappingArr := strings.Split(inputMappingVal, ",")
|
mappingArr := strings.Split(inputMappingVal, ",")
|
||||||
@ -148,7 +135,6 @@ func (rt *ReflectType) fillMappingRule(field *StructField, inputMappingVal strin
|
|||||||
// 配置格式错误, 跳过
|
// 配置格式错误, 跳过
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
rule.TargetField = strings.TrimSpace(itemArr[0])
|
|
||||||
mapRuleArr := strings.Split(strings.TrimSpace(itemArr[1]), "|")
|
mapRuleArr := strings.Split(strings.TrimSpace(itemArr[1]), "|")
|
||||||
for _, itemMapRule := range mapRuleArr {
|
for _, itemMapRule := range mapRuleArr {
|
||||||
itemMapRule = strings.TrimLeft(itemMapRule, "#")
|
itemMapRule = strings.TrimLeft(itemMapRule, "#")
|
||||||
@ -163,25 +149,25 @@ func (rt *ReflectType) fillMappingRule(field *StructField, inputMappingVal strin
|
|||||||
switch itemMapRuleArr[0] {
|
switch itemMapRuleArr[0] {
|
||||||
// 从header读取
|
// 从header读取
|
||||||
case MappingLocationHeader:
|
case MappingLocationHeader:
|
||||||
rule.RuleList = append(rule.RuleList, MappingRuleItem{
|
field.MappingRuleList = append(field.MappingRuleList, MappingRuleItem{
|
||||||
Location: MappingLocationHeader,
|
Location: MappingLocationHeader,
|
||||||
Field: itemMapRuleArr[1],
|
Field: itemMapRuleArr[1],
|
||||||
})
|
})
|
||||||
// 从请求参数读取
|
// 从请求参数读取
|
||||||
case MappingLocationParam:
|
case MappingLocationParam:
|
||||||
rule.RuleList = append(rule.RuleList, MappingRuleItem{
|
field.MappingRuleList = append(field.MappingRuleList, MappingRuleItem{
|
||||||
Location: MappingLocationParam,
|
Location: MappingLocationParam,
|
||||||
Field: itemMapRuleArr[1],
|
Field: itemMapRuleArr[1],
|
||||||
})
|
})
|
||||||
// 从响应数据读取
|
// 从响应数据读取
|
||||||
case MappingLocationResponse:
|
case MappingLocationResponse:
|
||||||
rule.RuleList = append(rule.RuleList, MappingRuleItem{
|
field.MappingRuleList = append(field.MappingRuleList, MappingRuleItem{
|
||||||
Location: MappingLocationResponse,
|
Location: MappingLocationResponse,
|
||||||
Field: itemMapRuleArr[1],
|
Field: itemMapRuleArr[1],
|
||||||
})
|
})
|
||||||
// 从扩展数据读取
|
// 从扩展数据读取
|
||||||
case MappingLocationExtension:
|
case MappingLocationExtension:
|
||||||
rule.RuleList = append(rule.RuleList, MappingRuleItem{
|
field.MappingRuleList = append(field.MappingRuleList, MappingRuleItem{
|
||||||
Location: MappingLocationExtension,
|
Location: MappingLocationExtension,
|
||||||
Field: itemMapRuleArr[1],
|
Field: itemMapRuleArr[1],
|
||||||
})
|
})
|
||||||
@ -190,7 +176,7 @@ func (rt *ReflectType) fillMappingRule(field *StructField, inputMappingVal strin
|
|||||||
fallthrough
|
fallthrough
|
||||||
default:
|
default:
|
||||||
for _, itemLocation := range mappingLocationList {
|
for _, itemLocation := range mappingLocationList {
|
||||||
rule.RuleList = append(rule.RuleList, MappingRuleItem{
|
field.MappingRuleList = append(field.MappingRuleList, MappingRuleItem{
|
||||||
Location: itemLocation,
|
Location: itemLocation,
|
||||||
Field: itemMapRuleArr[1],
|
Field: itemMapRuleArr[1],
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user