-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
example_test.go
123 lines (110 loc) · 2.78 KB
/
example_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
package capnp_test
import (
"fmt"
"os"
"capnproto.org/go/capnp/v3"
air "capnproto.org/go/capnp/v3/internal/aircraftlib"
)
func Example() {
// Make a brand new empty message.
msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
if err != nil {
panic(err)
}
// If you want runtime-type identification, this is easily obtained. Just
// wrap everything in a struct that contains a single anoymous union (e.g. struct Z).
// Then always set a Z as the root object in you message/first segment.
// The cost of the extra word of storage is usually worth it, as
// then human readable output is easily obtained via a shell command such as
//
// $ cat binary.cpz | capnp decode aircraft.capnp Z
//
// If you need to conserve space, and know your content in advance, it
// isn't necessary to use an anonymous union. Just supply the type name
// in place of 'Z' in the decode command above.
// There can only be one root. Subsequent NewRoot* calls will set the root
// pointer and orphan the previous root.
z, err := air.NewRootZ(seg)
if err != nil {
panic(err)
}
// then non-root objects:
aircraft, err := z.NewAircraft()
if err != nil {
panic(err)
}
b737, err := aircraft.NewB737()
if err != nil {
panic(err)
}
planebase, err := b737.NewBase()
if err != nil {
panic(err)
}
// Set primitive fields
planebase.SetCanFly(true)
planebase.SetName("Henrietta")
planebase.SetRating(100)
planebase.SetMaxSpeed(876) // km/hr
// if we don't set capacity, it will get the default value, in this case 0.
//planebase.SetCapacity(26020) // Liters fuel
// Creating a list
homes, err := planebase.NewHomes(2)
if err != nil {
panic(err)
}
homes.Set(0, air.Airport_jfk)
homes.Set(1, air.Airport_lax)
// Ready to write!
// You can write to memory...
buf, err := msg.Marshal()
if err != nil {
panic(err)
}
_ = buf
// ... or write to an io.Writer.
file, err := os.CreateTemp("", "go-capnproto")
if err != nil {
panic(err)
}
defer file.Close()
defer os.Remove(file.Name())
err = capnp.NewEncoder(file).Encode(msg)
if err != nil {
panic(err)
}
}
func ExampleUnmarshal() {
msg, s, err := capnp.NewMessage(capnp.SingleSegment(nil))
if err != nil {
fmt.Printf("allocation error %v\n", err)
return
}
d, err := air.NewRootZdate(s)
if err != nil {
fmt.Printf("root error %v\n", err)
return
}
d.SetYear(2004)
d.SetMonth(12)
d.SetDay(7)
data, err := msg.Marshal()
if err != nil {
fmt.Printf("marshal error %v\n", err)
return
}
// Read
msg, err = capnp.Unmarshal(data)
if err != nil {
fmt.Printf("unmarshal error %v\n", err)
return
}
d, err = air.ReadRootZdate(msg)
if err != nil {
fmt.Printf("read root error %v\n", err)
return
}
fmt.Printf("year %d, month %d, day %d\n", d.Year(), d.Month(), d.Day())
// Output:
// year 2004, month 12, day 7
}