-
Notifications
You must be signed in to change notification settings - Fork 1
/
person_test.go
80 lines (67 loc) · 1.75 KB
/
person_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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package protoenc_test
import (
"encoding/hex"
"fmt"
"reflect"
"github.com/siderolabs/protoenc"
)
// Go-based protobuf definition for the example Person message format
//
//nolint:govet
type Person struct {
Name string `protobuf:"1"`
ID int32 `protobuf:"2"`
Email *string `protobuf:"3"`
Phone []PhoneNumber `protobuf:"4"`
}
type PhoneType uint32
const (
MOBILE PhoneType = iota
HOME
WORK
)
//nolint:govet
type PhoneNumber struct {
Number string `protobuf:"1"`
Type *PhoneType `protobuf:"2"`
}
// This example defines, encodes, and decodes a Person message format
// equivalent to the example used in the Protocol Buffers overview.
//
//nolint:nosnakecase
func Example_protobuf() {
// Create a Person record
email := "alice@somewhere"
ptype := WORK
person := Person{
"Alice",
123,
&email,
[]PhoneNumber{
{"111-222-3333", nil},
{"444-555-6666", &ptype},
},
}
// Encode it
buf, err := protoenc.Marshal(&person)
if err != nil {
panic("Encode failed: " + err.Error())
}
fmt.Print(hex.Dump(buf))
// Decode it
person2 := Person{}
if err = protoenc.Unmarshal(buf, &person2); err != nil {
panic(err)
}
if !reflect.DeepEqual(person, person2) {
panic("Decode failed")
}
// Output:
// 00000000 0a 05 41 6c 69 63 65 10 7b 1a 0f 61 6c 69 63 65 |..Alice.{..alice|
// 00000010 40 73 6f 6d 65 77 68 65 72 65 22 0e 0a 0c 31 31 |@somewhere"...11|
// 00000020 31 2d 32 32 32 2d 33 33 33 33 22 10 0a 0c 34 34 |1-222-3333"...44|
// 00000030 34 2d 35 35 35 2d 36 36 36 36 10 02 |4-555-6666..|
}