支持嵌套结构体解析!!!!!!
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
// Package dynamicstruct ...
|
||||
//
|
||||
// Description : dynamicstruct ...
|
||||
// Description : dynamic struct ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
@ -10,20 +10,24 @@ package dynamicstruct
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_dynamicStructImpl_New(t *testing.T) {
|
||||
instance := NewStruct().
|
||||
AddField("Integer", "", 0, `json:"int"`, false).
|
||||
/*AddField("Integer", "", 0, `json:"int"`, false).
|
||||
AddField("Text", "", "", `json:"someText"`, false).
|
||||
AddField("Float", "", 0.0, `json:"double"`, false).
|
||||
AddField("Boolean", "", false, "", false).
|
||||
AddField("Slice", "", []int{}, "", false).
|
||||
AddField("Anonymous", "", "", `json:"-"`, false).
|
||||
Build().
|
||||
New()
|
||||
AddField("Anonymous", "", "", `json:"-"`, false).*/
|
||||
AddField("user.base.age", "", 20, `json:"age"`, false).
|
||||
AddField("user.base.name", "", "", `json:"name"`, false).
|
||||
AddField("user.job.address", "", "", `json:"address"`, false).
|
||||
AddField("user.job.company.name", "", "", `json:"name"`, false).
|
||||
Build()
|
||||
|
||||
val := instance.New()
|
||||
|
||||
data := []byte(`
|
||||
{
|
||||
@ -32,10 +36,13 @@ func Test_dynamicStructImpl_New(t *testing.T) {
|
||||
"double": 123.45,
|
||||
"Boolean": true,
|
||||
"Slice": [1, 2, 3],
|
||||
"user": {"job":{"address":"beijing","company":{"name":"unknown"}}, "base":{"age": 1800, "name":"baicha"}},
|
||||
"Anonymous": "avoid to read"
|
||||
}
|
||||
`)
|
||||
err := json.Unmarshal(data, &instance)
|
||||
fmt.Println(err)
|
||||
fmt.Println(reflect.ValueOf(instance).Elem().FieldByName("Integer").Interface())
|
||||
err := json.Unmarshal(data, &val)
|
||||
fmt.Println(err, val)
|
||||
valByte, _ := json.Marshal(val)
|
||||
fmt.Println(string(valByte))
|
||||
// fmt.Println(reflect.ValueOf(val).Elem().FieldByName("Integer").Interface())
|
||||
}
|
||||
|
Reference in New Issue
Block a user