-
Notifications
You must be signed in to change notification settings - Fork 0
/
unmarshal_test.go
118 lines (101 loc) · 2.85 KB
/
unmarshal_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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package parquet
import (
"fmt"
"testing"
)
type Student struct {
Name string `parquet:"name=name, type=BYTE_ARRAY, convertedtype=UTF8"`
Age int32 `parquet:"name=age, type=INT32"`
Weight *int32 `parquet:"name=weight, type=INT32"`
Classes *map[string][]*Class `parquet:"name=classes, keytype=BYTE_ARRAY, keyconvertedtype=UTF8"`
}
type Class struct {
Name string `parquet:"name=name, type=BYTE_ARRAY, convertedtype=UTF8"`
ID *int64 `parquet:"name=id, type=INT64"`
Required []string `parquet:"name=required, valuetype=BYTE_ARRAY, valueconvertedtype=UTF8"`
}
func (c Class) String() string {
id := "nil"
if c.ID != nil {
id = fmt.Sprintf("%d", *c.ID)
}
res := fmt.Sprintf("{Name:%s, ID:%v, Required:%s}", c.Name, id, fmt.Sprint(c.Required))
return res
}
func (s Student) String() string {
weight := "nil"
if s.Weight != nil {
weight = fmt.Sprintf("%d", *s.Weight)
}
cs := "{"
for key, classes := range *s.Classes {
s := key + ":["
for _, class := range classes {
s += class.String() + ","
}
s += "]"
cs += s
}
cs += "}"
res := fmt.Sprintf("{Name:%s, Age:%d, Weight:%s, Classes:%s}", s.Name, s.Age, weight, cs)
return res
}
func TestMarshalUnmarshal(t *testing.T) {
schemaHandler, _ := NewSchemaHandlerFromStruct(new(Student))
fmt.Println("SchemaHandler Finished")
math01ID := int64(1)
math01 := Class{
Name: "Math1",
ID: &math01ID,
Required: make([]string, 0),
}
math02ID := int64(2)
math02 := Class{
Name: "Math2",
ID: &math02ID,
Required: make([]string, 0),
}
math02.Required = append(math02.Required, "Math01")
physics := Class{
Name: "Physics",
ID: nil,
Required: make([]string, 0),
}
physics.Required = append(physics.Required, "Math01", "Math02")
weight01 := int32(60)
stu01Class := make(map[string][]*Class)
stu01Class["Science"] = make([]*Class, 0)
stu01Class["Science"] = append(stu01Class["Science"], &math01, &math02)
stu01 := Student{
Name: "zxt",
Age: 18,
Weight: &weight01,
Classes: &stu01Class,
}
stu02Class := make(map[string][]*Class)
stu02Class["Science"] = make([]*Class, 0)
stu02Class["Science"] = append(stu02Class["Science"], &physics)
stu02 := Student{
Name: "tong",
Age: 29,
Weight: nil,
Classes: &stu02Class,
}
stus := make([]interface{}, 0)
stus = append(stus, stu01, stu02)
src, err := marshal(stus, schemaHandler)
fmt.Println("Marshal Finished", err)
for name, table := range *src {
fmt.Println(name)
fmt.Println("Val: ", table.Values)
fmt.Println("RL: ", table.RepetitionLevels)
fmt.Println("DL: ", table.DefinitionLevels)
}
dst := make([]Student, 0)
if err := unmarshal(src, 0, len(stus), &dst, schemaHandler, ""); err != nil {
t.Fatal(err)
}
if s0, s1 := fmt.Sprint(stus), fmt.Sprint(dst); s0 != s1 {
t.Errorf("Fail expect %s, get %s", s0, s1)
}
}