-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamicstruct_test.go
87 lines (81 loc) · 1.52 KB
/
dynamicstruct_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package restflix
import (
"encoding/json"
"reflect"
"testing"
)
type Container struct {
Name string
Items []Item
}
type Item struct {
Name string
Container Container
Vals []string
}
func TestDynamicStruct(t *testing.T) {
//item := Item{}
//c := reflect.Value{}
//c.SetBool(true)
//c.SetString("xd")
//
////val := reflect.ValueOf(item)
//bx, _ := json.Marshal(c.Interface())
//
//t.Log(string(bx))
//
//return
v0 := reflect.StructOf([]reflect.StructField{
{
Name: "C",
Type: reflect.TypeOf(int(0)),
Tag: `json:"c"`,
},
{
Name: "D",
Type: reflect.TypeOf(""),
Tag: `json:"d"`,
},
})
v := reflect.StructOf([]reflect.StructField{
{
Name: "A",
Type: reflect.TypeOf(int(0)),
Tag: `json:"a"`,
},
{
Name: "B",
Type: reflect.TypeOf(""),
Tag: `json:"B"`,
},
{
Name: "V0",
Type: reflect.TypeOf(reflect.ValueOf(v0).Interface()),
Tag: `json:"v0"`,
},
})
d := reflect.New(v).Interface()
b0, _ := json.Marshal(d)
t.Log(string(b0), reflect.ValueOf(v0).Interface())
//
//b0, _ := json.Marshal(d)
//
//instance1 := dynamicstruct.NewStruct().
// AddField("Z", "", `json:"z"`).
// AddField("E", 0.0, `json:"e"`).
// Build().
// New()
//
//b1, _ := json.Marshal(instance1)
//
//instance := dynamicstruct.NewStruct().
// AddField("X", "", `json:"x"`).
// AddField("Y", 0.0, `json:"y"`).
// AddField("Instance1", &b1, `json:"instance1"`).
// Build().
// New()
//
//b, _ := json.Marshal(instance)
//
//t.Log(string(b), string(b1), string(b0))
}