forked from jmckaskill/go-capnproto
-
Notifications
You must be signed in to change notification settings - Fork 21
/
nested_test.go
211 lines (170 loc) · 5.44 KB
/
nested_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package capn_test
import (
"bytes"
"fmt"
"io"
"reflect"
"testing"
capn "github.com/glycerine/go-capnproto"
air "github.com/glycerine/go-capnproto/aircraftlib"
cv "github.com/glycerine/goconvey/convey"
)
// demonstrate and test serialization to List(List(Struct(List))), nested lists.
// start with smaller Struct(List)
func Test001StructList(t *testing.T) {
cv.Convey("Given type Nester1 struct { Strs []string } in go, where Nester1 is a struct, and a mirror/parallel capnp struct air.Nester1Capn { strs @0: List(Text); } defined in the aircraftlib schema", t, func() {
cv.Convey("When we Save() Nester to capn and then Load() it back, the data should match, so that we have working Struct(List) serialization and deserializatoin in go-capnproto", func() {
// Does Nester1 alone serialization and deser okay?
rw := Nester1{Strs: []string{"xenophilia", "watchowski"}}
var o bytes.Buffer
rw.Save(&o)
seg, n, err := capn.ReadFromMemoryZeroCopy(o.Bytes())
cv.So(err, cv.ShouldEqual, nil)
cv.So(n, cv.ShouldBeGreaterThan, 0)
text := CapnpDecodeSegment(seg, "", "aircraftlib/aircraft.capnp", "Nester1Capn")
if false {
fmt.Printf("text = '%s'\n", text)
}
rw2 := &Nester1{}
rw2.Load(&o)
//fmt.Printf("rw = '%#v'\n", rw)
//fmt.Printf("rw2 = '%#v'\n", rw2)
same := reflect.DeepEqual(&rw, rw2)
cv.So(same, cv.ShouldEqual, true)
})
})
}
func Test002ListListStructList(t *testing.T) {
cv.Convey("Given type RWTest struct { NestMatrix [][]Nester1; } in go, where Nester1 is a struct, and a mirror/parallel capnp struct air.RWTestCapn { nestMatrix @0: List(List(Nester1Capn)); } defined in the aircraftlib schema", t, func() {
cv.Convey("When we Save() RWTest to capn and then Load() it back, the data should match, so that we have working List(List(Struct)) serialization and deserializatoin in go-capnproto", func() {
// full RWTest
rw := RWTest{
NestMatrix: [][]Nester1{[]Nester1{Nester1{Strs: []string{"z", "w"}}, Nester1{Strs: []string{"q", "r"}}}, []Nester1{Nester1{Strs: []string{"zebra", "wally"}}, Nester1{Strs: []string{"qubert", "rocks"}}}},
}
var o bytes.Buffer
rw.Save(&o)
seg, n, err := capn.ReadFromMemoryZeroCopy(o.Bytes())
cv.So(err, cv.ShouldEqual, nil)
cv.So(n, cv.ShouldBeGreaterThan, 0)
text := CapnpDecodeSegment(seg, "", "aircraftlib/aircraft.capnp", "RWTestCapn")
if false {
fmt.Printf("text = '%s'\n", text)
}
rw2 := &RWTest{}
rw2.Load(&o)
//fmt.Printf("rw = '%#v'\n", rw)
//fmt.Printf("rw2 = '%#v'\n", rw2)
same := reflect.DeepEqual(&rw, rw2)
cv.So(same, cv.ShouldEqual, true)
})
})
}
type Nester1 struct {
Strs []string
}
type RWTest struct {
NestMatrix [][]Nester1
}
func (s *Nester1) Save(w io.Writer) {
seg := capn.NewBuffer(nil)
Nester1GoToCapn(seg, s)
seg.WriteTo(w)
}
func Nester1GoToCapn(seg *capn.Segment, src *Nester1) air.Nester1Capn {
//fmt.Printf("\n\n Nester1GoToCapn sees seg = '%#v'\n", seg)
dest := air.AutoNewNester1Capn(seg)
mylist1 := seg.NewTextList(len(src.Strs))
for i := range src.Strs {
mylist1.Set(i, string(src.Strs[i]))
}
dest.SetStrs(mylist1)
//fmt.Printf("after Nester1GoToCapn setting\n")
return dest
}
func Nester1CapnToGo(src air.Nester1Capn, dest *Nester1) *Nester1 {
if dest == nil {
dest = &Nester1{}
}
dest.Strs = src.Strs().ToArray()
return dest
}
func (s *Nester1) Load(r io.Reader) {
capMsg, err := capn.ReadFromStream(r, nil)
if err != nil {
panic(fmt.Errorf("capn.ReadFromStream error: %s", err))
}
z := air.ReadRootNester1Capn(capMsg)
Nester1CapnToGo(z, s)
}
func (s *RWTest) Save(w io.Writer) {
seg := capn.NewBuffer(nil)
RWTestGoToCapn(seg, s)
seg.WriteTo(w)
}
func (s *RWTest) Load(r io.Reader) {
capMsg, err := capn.ReadFromStream(r, nil)
if err != nil {
panic(fmt.Errorf("capn.ReadFromStream error: %s", err))
}
z := air.ReadRootRWTestCapn(capMsg)
RWTestCapnToGo(z, s)
}
func RWTestCapnToGo(src air.RWTestCapn, dest *RWTest) *RWTest {
if dest == nil {
dest = &RWTest{}
}
var n int
// NestMatrix
n = src.NestMatrix().Len()
dest.NestMatrix = make([][]Nester1, n)
for i := 0; i < n; i++ {
dest.NestMatrix[i] = Nester1CapnListToSliceNester1(air.Nester1Capn_List(src.NestMatrix().At(i)))
}
return dest
}
func RWTestGoToCapn(seg *capn.Segment, src *RWTest) air.RWTestCapn {
dest := air.AutoNewRWTestCapn(seg)
// NestMatrix -> Nester1Capn (go slice to capn list)
if len(src.NestMatrix) > 0 {
//typedList := air.NewNester1CapnList(seg, len(src.NestMatrix))
plist := seg.NewPointerList(len(src.NestMatrix))
i := 0
for _, ele := range src.NestMatrix {
//plist.Set(i, capn.Object(Nester1GoToCapn(seg, &ele)))
r := capn.Object(SliceNester1ToNester1CapnList(seg, ele))
plist.Set(i, r)
i++
}
//dest.SetNestMatrix(typedList)
dest.SetNestMatrix(plist)
}
return dest
}
func Nester1CapnListToSliceNester1(p air.Nester1Capn_List) []Nester1 {
v := make([]Nester1, p.Len())
for i := range v {
Nester1CapnToGo(p.At(i), &v[i])
}
return v
}
func SliceNester1ToNester1CapnList(seg *capn.Segment, m []Nester1) air.Nester1Capn_List {
lst := air.NewNester1CapnList(seg, len(m))
for i := range m {
lst.Set(i, Nester1GoToCapn(seg, &m[i]))
}
return lst
}
func SliceStringToTextList(seg *capn.Segment, m []string) capn.TextList {
lst := seg.NewTextList(len(m))
for i := range m {
lst.Set(i, string(m[i]))
}
return lst
}
func TextListToSliceString(p capn.TextList) []string {
v := make([]string, p.Len())
for i := range v {
v[i] = string(p.At(i))
}
return v
}