-
Notifications
You must be signed in to change notification settings - Fork 4
/
example_pair_test.go
71 lines (63 loc) · 1.22 KB
/
example_pair_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
package ubjson_test
import (
"fmt"
"github.com/jmank88/ubjson"
)
// A Int8Vec3 encodes itself as a fixed length, strongly-typed array container.
type Int8Vec3 struct {
a, b, c int8
}
func (iv *Int8Vec3) UBJSONType() ubjson.Marker {
return ubjson.ArrayStartMarker
}
func (iv *Int8Vec3) MarshalUBJSON(e *ubjson.Encoder) error {
a, err := e.ArrayType(ubjson.Int8Marker, 3)
if err != nil {
return err
}
if err := a.EncodeInt8(iv.a); err != nil {
return err
}
if err := a.EncodeInt8(iv.b); err != nil {
return err
}
if err := a.EncodeInt8(iv.c); err != nil {
return err
}
return a.End()
}
func (iv *Int8Vec3) UnmarshalUBJSON(d *ubjson.Decoder) error {
a, err := d.Array()
if err != nil {
return err
}
if i, err := a.DecodeInt8(); err != nil {
return err
} else {
iv.a = i
}
if i, err := a.DecodeInt8(); err != nil {
return err
} else {
iv.b = i
}
if i, err := a.DecodeInt8(); err != nil {
return err
} else {
iv.c = i
}
return a.End()
}
func Example_pair() {
iv := &Int8Vec3{a: 100, b: 42, c: -55}
if b, err := ubjson.MarshalBlock(iv); err != nil {
fmt.Println("error: " + err.Error())
} else {
fmt.Println(string(b))
}
// Output:
// [[][$][i][#][U][3]
// [100]
// [42]
// [-55]
}