From cd9bc4aadba6d24c6c79648c6bcd6c16fa8f80f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 1 Feb 2023 20:30:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96mapping=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define.go | 30 ++++++------------------------ reflect.go | 46 ++++++++++++++++------------------------------ 2 files changed, 22 insertions(+), 54 deletions(-) diff --git a/define.go b/define.go index 382dae7..e89d406 100644 --- a/define.go +++ b/define.go @@ -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 结构体信息 diff --git a/reflect.go b/reflect.go index 6814c0d..aa11abe 100644 --- a/reflect.go +++ b/reflect.go @@ -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], })