Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2:jsonutils optimization #43

Merged
merged 1 commit into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 4 additions & 25 deletions jsonUtil/json_to_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"fmt"
"log"
"reflect"
"strings"
)
Expand Down Expand Up @@ -48,32 +47,12 @@ func JsonToStruct(jsonData string, result any) error {
}

switch fieldValue.Kind() {
case reflect.String:
fieldValue.SetString(toStringReflect(value))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
val, err := toInt64Reflect(value)
if err != nil {
log.Printf("toInt64Reflect err:%s \n", err)
case reflect.String, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64, reflect.Bool:
if err := parsePrimitiveValue(fieldValue, value); err != nil {
return err
}
fieldValue.SetInt(val)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
val, err := toUint64Reflect(value)
if err != nil {
log.Printf("toUint64Reflect err:%s \n", err)
return err
}
fieldValue.SetUint(val)
case reflect.Float32, reflect.Float64:
val, err := toFloat64Reflect(value)
if err != nil {
log.Printf("toFloat64Reflect err:%s \n", err)
return err
}
fieldValue.SetFloat(val)
case reflect.Bool:
val := toBoolReflect(value)
fieldValue.SetBool(val)
case reflect.Struct:
if subData, ok := value.(map[string]any); ok {
subResult := reflect.New(fieldValue.Type())
Expand Down
34 changes: 6 additions & 28 deletions jsonUtil/parse_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package jsonUtil
import (
"encoding/json"
"fmt"
"log"
"reflect"
)

Expand Down Expand Up @@ -45,43 +44,22 @@ func parseSlice(fieldValue reflect.Value, subData []any) error {
} else {
// 否则,将项目转换为适当的类型并设置元素值
switch elem.Kind() {
case reflect.String:
elem.SetString(toStringReflect(item))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
val, err := toInt64Reflect(item)
case reflect.String, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64, reflect.Bool:
err := parsePrimitiveValue(elem, item)
if err != nil {
log.Printf("toInt64Reflect err:%s \n", err)
return err
}
elem.SetInt(val)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
val, err := toUint64Reflect(item)
if err != nil {
log.Printf("toUint64Reflect err:%s \n", err)
return err
}
elem.SetUint(val)
case reflect.Float32, reflect.Float64:
val, err := toFloat64Reflect(item)
if err != nil {
log.Printf("toFloat64Reflect err:%s \n", err)
return err
}
elem.SetFloat(val)
case reflect.Bool:
val := toBoolReflect(item)
elem.SetBool(val)
default:
return fmt.Errorf("unsupported slice element type: %s", elemType.String())
}
}

// 将元素附加到切片
slice = reflect.Append(slice, elem)
slice = reflect.Append(slice, elem) // 将元素附加到切片
}

// 将切片值设置为目标字段
fieldValue.Set(slice)
fieldValue.Set(slice) // 将切片值设置为目标字段

return nil
}