-
Notifications
You must be signed in to change notification settings - Fork 12
/
nested_test.go
117 lines (85 loc) · 2.08 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
package main
import (
"testing"
cv "github.com/glycerine/goconvey/convey"
)
func TestNestedStructs016(t *testing.T) {
cv.Convey("Given go structs that contain structs \n"+
" then these should marshal and unmarshal their inner struct members", t, func() {
ex0 := `
type Inner struct {
C int
}
type Outer struct {
A int
B Inner
}
`
cv.So(ExtractString2String(ex0), ShouldMatchModuloWhiteSpace, `
struct InnerCapn {
c @0: Int64;
}
struct OuterCapn {
a @0: Int64; b @1: InnerCapn;
}
func (s *Inner) Save(w io.Writer) error {
seg := capn.NewBuffer(nil)
InnerGoToCapn(seg, s)
_, err := seg.WriteTo(w)
return err
}
func (s *Inner) Load(r io.Reader) error {
capMsg, err := capn.ReadFromStream(r, nil)
if err != nil {
//panic(fmt.Errorf("capn.ReadFromStream error: %s", err))
return err
}
z := ReadRootInnerCapn(capMsg)
InnerCapnToGo(z, s)
return nil
}
func InnerCapnToGo(src InnerCapn, dest *Inner) *Inner {
if dest == nil {
dest = &Inner{}
}
dest.C = int(src.C())
return dest
}
func InnerGoToCapn(seg *capn.Segment, src *Inner) InnerCapn {
dest := AutoNewInnerCapn(seg)
dest.SetC(int64(src.C))
return dest
}
func (s *Outer) Save(w io.Writer) error {
seg := capn.NewBuffer(nil)
OuterGoToCapn(seg, s)
_, err := seg.WriteTo(w)
return err
}
func (s *Outer) Load(r io.Reader) error {
capMsg, err := capn.ReadFromStream(r, nil)
if err != nil {
//panic(fmt.Errorf("capn.ReadFromStream error: %s", err))
return err
}
z := ReadRootOuterCapn(capMsg)
OuterCapnToGo(z, s)
return nil
}
func OuterCapnToGo(src OuterCapn, dest *Outer) *Outer {
if dest == nil {
dest = &Outer{}
}
dest.A = int(src.A())
dest.B = *InnerCapnToGo(src.B(), nil)
return dest
}
func OuterGoToCapn(seg *capn.Segment, src *Outer) OuterCapn {
dest := AutoNewOuterCapn(seg)
dest.SetA(int64(src.A))
dest.SetB(InnerGoToCapn(seg, &src.B))
return dest
}
`)
})
}