完成mapping解析

This commit is contained in:
白茶清欢 2023-02-01 20:15:15 +08:00
parent 7b7aee4070
commit a3caefef3f

View File

@ -144,6 +144,58 @@ func (rt *ReflectType) fillMappingRule(field *StructField, inputMappingVal strin
item = strings.TrimSpace(item) item = strings.TrimSpace(item)
// 要赋值的字段名 // 要赋值的字段名
itemArr := strings.Split(item, ":") itemArr := strings.Split(item, ":")
if len(itemArr) != 2 {
// 配置格式错误, 跳过
continue
}
rule.TargetField = strings.TrimSpace(itemArr[0]) 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],
})
}
}
}
} }
} }