This commit is contained in:
libin
2023-03-21 08:26:17 +08:00
parent bc3355c2c5
commit 3c506d07c3
8 changed files with 178 additions and 17 deletions

View File

@@ -8,4 +8,7 @@ lint:
golangci-lint run golangci-lint run
bench: bench:
go test -benchmem -bench . go test -benchmem -bench .
doc:
godoc -http=:6060 -play -index

View File

@@ -428,8 +428,4 @@ func AnyToUint64(input interface{}) (uint64, error)
``` ```
## 赞助与支持
`GoEasyUtils ` Thank JetBrains for their support
<a href="https://www.jetbrains.com"><img src="https://raw.githubusercontent.com/panjf2000/illustrations/master/jetbrains/jetbrains-variant-4.png" height="100" alt="JetBrains"/></a>

View File

@@ -36,8 +36,8 @@ func main() {
## Function list ## Function list
| Package name | Function Outline | README | | Package name | Function Outline | Document |
| ------------ | ----------------------------------------------------------------------------------------- | -------------------- | | ------------ | ----------------------------------------------------------------------------------------- |----------------------|
| anyUtil | Convert any type of data to the specified type | [README](anyUtil) | | anyUtil | Convert any type of data to the specified type | [README](anyUtil) |
| byteUtil | Conversion of byte array | [README](byteUtil) | | byteUtil | Conversion of byte array | [README](byteUtil) |
| cryptoUtil | Various encryption processing | [README](cryptoUtil) | | cryptoUtil | Various encryption processing | [README](cryptoUtil) |

View File

@@ -1,5 +1,3 @@
// 加密与解密工具
package cryptoUtil package cryptoUtil
import ( import (

View File

@@ -1,5 +1,3 @@
// emoji表情处理工具
package emojiUtil package emojiUtil
import ( import (

View File

@@ -1,5 +1,3 @@
// Package jsonUtil Json数据处理包
// 可用于json赋值结构体json数据转义
package jsonUtil package jsonUtil
import ( import (
@@ -52,6 +50,17 @@ func JsonToStruct(jsonData string, result interface{}) error {
switch fieldValue.Kind() { switch fieldValue.Kind() {
case reflect.String: case reflect.String:
fieldValue.SetString(value.(string)) fieldValue.SetString(value.(string))
//case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
// switch value.(type) {
// case float64:
// fieldValue.SetInt(int64(value.(float64)))
// case string:
// if intValue, err := strconv.ParseInt(value.(string), 10, 64); err == nil {
// fieldValue.SetInt(intValue)
// }
// default:
// fieldValue.SetInt(int64(value.(int)))
// }
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
switch value.(type) { switch value.(type) {
case float64: case float64:
@@ -60,8 +69,16 @@ func JsonToStruct(jsonData string, result interface{}) error {
if intValue, err := strconv.ParseInt(value.(string), 10, 64); err == nil { if intValue, err := strconv.ParseInt(value.(string), 10, 64); err == nil {
fieldValue.SetInt(intValue) fieldValue.SetInt(intValue)
} }
default: case int:
fieldValue.SetInt(int64(value.(int))) fieldValue.SetInt(int64(value.(int)))
case int8:
fieldValue.SetInt(int64(value.(int8)))
case int16:
fieldValue.SetInt(int64(value.(int16)))
case int32:
fieldValue.SetInt(int64(value.(int32)))
case int64:
fieldValue.SetInt(value.(int64))
} }
case reflect.Float32, reflect.Float64: case reflect.Float32, reflect.Float64:
switch value.(type) { switch value.(type) {
@@ -105,6 +122,115 @@ func JsonToStruct(jsonData string, result interface{}) error {
return nil return nil
} }
//func JsonToStruct(jsonData string, result interface{}) error {
// var data map[string]interface{}
// err := json.Unmarshal([]byte(jsonData), &data)
// if err != nil {
// return err
// }
//
// resultValue := reflect.ValueOf(result).Elem()
// resultType := resultValue.Type()
//
// for i := 0; i < resultType.NumField(); i++ {
// fieldType := resultType.Field(i)
// fieldName := fieldType.Name
// fieldValue := resultValue.FieldByName(fieldName)
//
// jsonTag := fieldType.Tag.Get("json")
// if jsonTag == "" {
// jsonTag = fieldName
// }
//
// value, ok := data[jsonTag]
// if !ok {
// continue
// }
//
// fmt.Println(fieldType.Name, fieldValue.Kind())
//
// switch fieldValue.Kind() {
// case reflect.String:
// fieldValue.SetString(value.(string))
// case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
// switch value.(type) {
// case float64:
// fieldValue.SetInt(int64(value.(float64)))
// case string:
// if intValue, err := strconv.ParseInt(value.(string), 10, 64); err == nil {
// fieldValue.SetInt(intValue)
// }
// default:
// fieldValue.SetInt(int64(value.(int)))
// }
// case reflect.Float32, reflect.Float64:
// switch value.(type) {
// case float64:
// fieldValue.SetFloat(value.(float64))
// case string:
// if floatValue, err := strconv.ParseFloat(value.(string), 64); err == nil {
// fieldValue.SetFloat(floatValue)
// }
// }
// case reflect.Struct:
// if subData, ok := value.(map[string]interface{}); ok {
// subResult := reflect.New(fieldValue.Type())
// JsonToStruct(convertToJSONString(subData), subResult.Interface())
// fieldValue.Set(subResult.Elem())
// }
// case reflect.Slice:
// if subData, ok := value.([]interface{}); ok {
// subResult := reflect.MakeSlice(fieldValue.Type(), len(subData), len(subData))
// for j := 0; j < len(subData); j++ {
// subValue := subData[j]
// subElem := subResult.Index(j)
//
// switch subElem.Kind() {
// case reflect.Struct:
// if subDataElem, ok := subValue.(map[string]interface{}); ok {
// subResultElem := reflect.New(subElem.Type())
// JsonToStruct(convertToJSONString(subDataElem), subResultElem.Interface())
// subElem.Set(subResultElem.Elem())
// }
// case reflect.Slice:
// if subDataElem, ok := subValue.([]interface{}); ok {
// subResultElem := reflect.MakeSlice(subElem.Type(), len(subDataElem), len(subDataElem))
// for k := 0; k < len(subDataElem); k++ {
// subValueElem := subDataElem[k]
// subElemElem := subResultElem.Index(k)
//
// if subElemElem.Kind() == reflect.Struct {
// if subDataElemElem, ok := subValueElem.(map[string]interface{}); ok {
// subResultElemElem := reflect.New(subElemElem.Type())
// JsonToStruct(convertToJSONString(subDataElemElem), subResultElemElem.Interface())
// subElemElem.Set(subResultElemElem.Elem())
// }
// } else {
// subElemElem.Set(reflect.ValueOf(subValueElem))
// }
// }
// subElem.Set(subResultElem)
// }
// default:
// subElem.Set(reflect.ValueOf(subValue))
// }
// }
// fieldValue.Set(subResult)
// }
// default:
// if reflect.TypeOf(value).Kind() == reflect.Map {
// subResult := reflect.New(fieldValue.Type())
// JsonToStruct(convertToJSONString(value.(map[string]interface{})), subResult.Interface())
// fieldValue.Set(subResult.Elem())
// } else {
// fieldValue.Set(reflect.ValueOf(value))
// }
// }
// }
//
// return nil
//}
func convertToJSONString(data map[string]interface{}) string { func convertToJSONString(data map[string]interface{}) string {
jsonBytes, _ := json.Marshal(data) jsonBytes, _ := json.Marshal(data)
return string(jsonBytes) return string(jsonBytes)

View File

@@ -6,7 +6,7 @@ import (
"testing" "testing"
) )
func TestJsonToStruct(t *testing.T) { func TestJsonToStruct1(t *testing.T) {
type Address struct { type Address struct {
City string `json:"city"` City string `json:"city"`
Country string `json:"country"` Country string `json:"country"`
@@ -124,3 +124,45 @@ func TestJsonToStruct2(t *testing.T) {
fmt.Printf("person2%+v address%+v \n", person2, person2.Address) fmt.Printf("person2%+v address%+v \n", person2, person2.Address)
} }
// 多层级json测试
func TestJsonToStruct3(t *testing.T) {
type Address struct {
City string `json:"city"`
Street string `json:"street"`
Zipcode uint64 `json:"zipcode"`
}
type Score struct {
Subject string `json:"subject"`
Score int `json:"score"`
}
type Student struct {
Name string `json:"name"`
Age int `json:"age"`
Address Address `json:"address"`
Scores []Score `json:"scores"`
}
jsonStr4 := `{
"name": "Alice",
"age": 30,
"address": {
"city": "Beijing",
"street": "Zhangsan Street",
"zipcode": 100
},
"scores": [
{"subject": "Math", "score": 80},
{"subject": "English", "score": 90}
]
}`
var student Student
if err := JsonToStruct(jsonStr4, &student); err != nil {
fmt.Println(err)
return
}
fmt.Printf("%+v \n", student)
}

View File

@@ -1,5 +1,3 @@
// 验证工具类
package validUtil package validUtil
import ( import (