完善数据类型转换

This commit is contained in:
Yun
2026-05-27 22:02:17 +08:00
parent 3fb090a620
commit 614ac34883
7 changed files with 1352 additions and 104 deletions
+36 -19
View File
@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"reflect"
"strings"
"github.com/spf13/cast"
)
@@ -120,16 +121,15 @@ func (sp *StructProcessor) AttactToStruct(structxx any, updateMap map[string]str
continue
}
oldValueStr, _ := cast.ToStringE(field.Interface())
oldValue := field.Interface()
newValue, err := sp.valueSetter.SetFieldValue(field, mapValue)
if err != nil {
return nil, fmt.Errorf("设置字段 %s 的值失败: %w", mapKey, err)
}
newValueStr, _ := cast.ToStringE(newValue)
changeMap[mapKey] = ChangeInfo{
Old: oldValueStr,
New: newValueStr,
Old: oldValue,
New: newValue,
Val: newValue,
}
}
@@ -148,15 +148,14 @@ func (sp *StructProcessor) getFieldByPath(v reflect.Value, index []int) (reflect
return reflect.Value{}, fmt.Errorf("字段索引 %d 无效", index[0])
}
// 处理指针
for field.Kind() == reflect.Ptr {
if field.IsNil() {
field.Set(reflect.New(field.Type().Elem()))
}
field = field.Elem()
}
if len(index) > 1 {
// 只有中间节点才自动解引用指针
for field.Kind() == reflect.Ptr {
if field.IsNil() {
field.Set(reflect.New(field.Type().Elem()))
}
field = field.Elem()
}
return sp.getFieldByPath(field, index[1:])
}
@@ -166,7 +165,9 @@ func (sp *StructProcessor) getFieldByPath(v reflect.Value, index []int) (reflect
// processSliceOrArrayField 处理切片和数组字段
func (sp *StructProcessor) processSliceOrArrayField(field reflect.Value, value string, fieldKey string) error {
var jsonData []interface{}
if err := json.Unmarshal([]byte(value), &jsonData); err != nil {
decoder := json.NewDecoder(strings.NewReader(value))
decoder.UseNumber()
if err := decoder.Decode(&jsonData); err != nil {
return fmt.Errorf("字段 %s 的值必须是JSON数组格式: %w", fieldKey, err)
}
@@ -215,7 +216,9 @@ func (sp *StructProcessor) processNestedStruct(field reflect.Value, value string
// 尝试解析为JSON对象
var nestedMap map[string]interface{}
if err := json.Unmarshal([]byte(value), &nestedMap); err == nil {
decoder := json.NewDecoder(strings.NewReader(value))
decoder.UseNumber()
if err := decoder.Decode(&nestedMap); err == nil {
stringMap := make(map[string]string, len(nestedMap))
for k, v := range nestedMap {
str, err := cast.ToStringE(v)
@@ -237,17 +240,31 @@ func (sp *StructProcessor) processNestedStruct(field reflect.Value, value string
}
// 尝试直接设置值
if hasUnmarshalJSON(structValue.Type()) || isBasicStructType(structValue.Type()) {
oldValueStr, _ := cast.ToStringE(field.Interface())
if hasUnmarshalJSON(structValue.Type()) {
oldValue := field.Interface()
if err := setUnmarshalJSONValue(structValue, value); err != nil {
return nil, fmt.Errorf("设置UnmarshalJSON值失败: %w", err)
}
changeMap[parentKey] = ChangeInfo{
Old: oldValue,
New: structValue.Interface(),
Val: structValue.Interface(),
}
return changeMap, nil
}
if isBasicStructType(structValue.Type()) {
oldValue := field.Interface()
if err := setBasicStructValue(structValue, value); err != nil {
return nil, fmt.Errorf("设置基本结构体值失败: %w", err)
}
newValueStr, _ := cast.ToStringE(field.Interface())
changeMap[parentKey] = ChangeInfo{
Old: oldValueStr,
New: newValueStr,
Old: oldValue,
New: structValue.Interface(),
Val: structValue.Interface(),
}
return changeMap, nil