增加通过反射复制逻辑
This commit is contained in:
parent
c8d3a6688f
commit
ea3f8df7d1
62
reflect.go
62
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
|
||||
}
|
||||
case MappingLocationParam:
|
||||
case MappingLocationResponse:
|
||||
case MappingLocationExtension:
|
||||
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
|
||||
}
|
||||
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 获取输入的值
|
||||
|
Loading…
Reference in New Issue
Block a user