From ea3f8df7d11f30c5ed9ec45f66398cf849665fb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 2 Feb 2023 15:11:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=80=9A=E8=BF=87=E5=8F=8D?= =?UTF-8?q?=E5=B0=84=E5=A4=8D=E5=88=B6=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- reflect.go | 62 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 10 deletions(-) diff --git a/reflect.go b/reflect.go index 9e34abb..7efd2ef 100644 --- a/reflect.go +++ b/reflect.go @@ -337,7 +337,10 @@ func (rv *ReflectValue) fillFieldValue(reflectValue reflect.Value, fieldInfo *St responseByte, _ := json.Marshal(preSendHandler.GetResponseData()) extensionByte, _ := json.Marshal(preSendHandler.GetExtensionData()) for _, item := range fieldInfo.MappingRuleList { - + if rv.setDataValue(reflectValue, fieldInfo, item, header, paramByte, responseByte, extensionByte) { + // 找到数据,并且赋值成功,结束继续查找 + break + } } } @@ -347,20 +350,59 @@ func (rv *ReflectValue) fillFieldValue(reflectValue reflect.Value, fieldInfo *St // // Date : 14:24 2023/2/2 func (rv *ReflectValue) setDataValue(reflectVal reflect.Value, fieldInfo *StructField, rule MappingRuleItem, header http.Header, paramByte, responseByte, extensionByte []byte) bool { - switch rule.Location { - case MappingLocationHeader: - str := header.Get(rule.Field) - if len(str) == 0 { - // 未进行值设置 + inputVal := rv.getInputValue(rule, header, paramByte, responseByte, extensionByte) + if len(inputVal) == 0 { + return false + } + switch fieldInfo.Type { + case reflect.String: + reflectVal.Field(fieldInfo.Idx).SetString(inputVal) + return true + case reflect.Int64: + var i int64 + if err := util.ConvertAssign(&i, inputVal); nil != err { return false } - case MappingLocationParam: - case MappingLocationResponse: - case MappingLocationExtension: + if i == 0 { + return false + } + if fieldInfo.IsPtr { + reflectVal.Field(fieldInfo.Idx).Set(reflect.ValueOf(&i)) + } else { + reflectVal.Field(fieldInfo.Idx).SetInt(i) + } + return true + case reflect.Float64: + var f float64 + if err := util.ConvertAssign(&f, inputVal); nil != err { + return false + } + if f == 0 { + return false + } + if fieldInfo.IsPtr { + reflectVal.Field(fieldInfo.Idx).Set(reflect.ValueOf(&f)) + } else { + reflectVal.Field(fieldInfo.Idx).SetFloat(f) + } + return true + case reflect.Interface: + reflectVal.Field(fieldInfo.Idx).Set(reflect.ValueOf(&inputVal)) + return true + case reflect.Slice: + fallthrough + case reflect.Map: + fallthrough + case reflect.Struct: + var v interface{} + if err := json.Unmarshal([]byte(inputVal), &v); nil != err { + return false + } + reflectVal.Field(fieldInfo.Idx).Set(reflect.ValueOf(&v)) + return true default: return false } - return true } // getInputValue 获取输入的值