From a3caefef3f0a24c0587512684516cb9e74337f41 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:15:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90mapping=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- reflect.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/reflect.go b/reflect.go index d748e44..6814c0d 100644 --- a/reflect.go +++ b/reflect.go @@ -144,6 +144,58 @@ func (rt *ReflectType) fillMappingRule(field *StructField, inputMappingVal strin item = strings.TrimSpace(item) // 要赋值的字段名 itemArr := strings.Split(item, ":") + if len(itemArr) != 2 { + // 配置格式错误, 跳过 + continue + } rule.TargetField = strings.TrimSpace(itemArr[0]) + mapRuleArr := strings.Split(strings.TrimSpace(itemArr[1]), "|") + for _, itemMapRule := range mapRuleArr { + itemMapRule = strings.TrimLeft(itemMapRule, "#") + itemMapRuleArr := strings.Split(itemMapRule, "#") + // 注意 : # 为特殊分隔符, 如配置成 mapping:"project_id:#source_project_id#xxx_project_id" 实际等价于 mapping:"project_id:#source_project_id" 多余配置自动跳过 + if len(itemMapRuleArr[0]) < 2 { + // 没有指定位置,默认all, 即配置格式: mapping:"project_id:#source_project_id" + itemMapRuleArr[0] = MappingLocationAll + itemMapRuleArr = []string{MappingLocationAll, itemMapRuleArr[0]} + } + + switch itemMapRuleArr[0] { + // 从header读取 + case MappingLocationHeader: + rule.RuleList = append(rule.RuleList, MappingRuleItem{ + Location: MappingLocationHeader, + Field: itemMapRuleArr[1], + }) + // 从请求参数读取 + case MappingLocationParam: + rule.RuleList = append(rule.RuleList, MappingRuleItem{ + Location: MappingLocationParam, + Field: itemMapRuleArr[1], + }) + // 从响应数据读取 + case MappingLocationResponse: + rule.RuleList = append(rule.RuleList, MappingRuleItem{ + Location: MappingLocationResponse, + Field: itemMapRuleArr[1], + }) + // 从扩展数据读取 + case MappingLocationExtension: + rule.RuleList = append(rule.RuleList, MappingRuleItem{ + Location: MappingLocationExtension, + Field: itemMapRuleArr[1], + }) + // 全部读取一遍 + case MappingLocationAll: + fallthrough + default: + for _, itemLocation := range mappingLocationList { + rule.RuleList = append(rule.RuleList, MappingRuleItem{ + Location: itemLocation, + Field: itemMapRuleArr[1], + }) + } + } + } } }