-
Notifications
You must be signed in to change notification settings - Fork 3
/
example_test.go
184 lines (157 loc) · 4.26 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
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
package dproto_test
import (
"fmt"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/linux4life798/dproto"
)
func ExampleNewWireMessage() {
m := dproto.NewWireMessage()
// Add the following bool and int64
m.EncodeBool(1, true)
m.EncodeInt64(2, 10)
bytes, err := m.Marshal()
if err != nil {
panic("Error Marshaling: " + err.Error())
}
fmt.Printf("ProtobufBinary: [ %# x ]\n", bytes)
// Output: ProtobufBinary: [ 0x08 0x01 0x10 0x0a ]
}
func ExampleWireMessage_Marshal() {
m := dproto.NewWireMessage()
// Add the following bool and int64
m.EncodeBool(1, true)
m.EncodeInt64(2, 10)
bytes, err := m.Marshal()
if err != nil {
panic("Error Marshaling: " + err.Error())
}
fmt.Printf("ProtobufBinary: [ %# x ]\n", bytes)
// Output: ProtobufBinary: [ 0x08 0x01 0x10 0x0a ]
}
func ExampleWireMessage_Unmarshal() {
m := dproto.NewWireMessage()
// Add the following bool and int64
m.EncodeBool(1, true)
m.EncodeInt64(2, 10)
bytes, err := m.Marshal()
if err != nil {
panic("Error Marshaling: " + err.Error())
}
fmt.Printf("ProtobufBinary: [ %# x ]\n", bytes)
// Unmarshal the already marshalled bytes
m, err = dproto.Unmarshal(bytes)
if err != nil {
panic("Error Unmarshaling: " + err.Error())
}
if bVal, ok := m.DecodeBool(1); ok {
fmt.Println(bVal)
} else {
fmt.Println("No bool field 1")
}
if iVal, ok := m.DecodeInt64(2); ok {
fmt.Println(iVal)
} else {
fmt.Println("No int64 field 2")
}
// Output:
// ProtobufBinary: [ 0x08 0x01 0x10 0x0a ]
// true
// 10
}
// This example shows how to abstractly Marshal and Unmarshal
// protobuf messages.
// It should be noted that the ProtoFieldMap class already implements
// this behavior for you.
func ExampleWireMessage_Unmarshal_abstract() {
types := []descriptor.FieldDescriptorProto_Type{
descriptor.FieldDescriptorProto_TYPE_BOOL,
descriptor.FieldDescriptorProto_TYPE_INT64,
}
values := []interface{}{
bool(true),
int64(10),
}
m := dproto.NewWireMessage()
// Add the following bool and int64
for index, value := range values {
err := m.EncodeAs(dproto.FieldNum(index+1), value, types[index])
if err != nil {
panic(err)
}
}
// Marshal the message
bytes, err := m.Marshal()
if err != nil {
panic("Error Marshaling: " + err.Error())
}
fmt.Printf("ProtobufBinary: [ %# x ]\n", bytes)
// Unmarshal the already marshalled bytes
m, err = dproto.Unmarshal(bytes)
if err != nil {
panic("Error Unmarshaling: " + err.Error())
}
// Decode each field and print
for index, typ := range types {
val, err := m.DecodeAs(dproto.FieldNum(index+1), typ)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(val)
}
}
// Output:
// ProtobufBinary: [ 0x08 0x01 0x10 0x0a ]
// true
// 10
}
// Encodes and Decodes a protobuf buffer that would interface with
// a .proto file message containing "bool status =1;" and
// "int64 intensity = 2;".
func ExampleNewProtoFieldMap() {
// Setup the ProtoFieldMap to interface with the
// following .proto file:
// message LightStatus {
// bool status = 1;
// int64 intensity = 2;
// }
// Setup a FieldMap that holds the type-fieldnumber association
// This is effectively the information held in a .proto file
fm := dproto.NewProtoFieldMap()
// Add Protobuf bool as field 1 ("bool status = 1;")
if !fm.Add(1, descriptor.FieldDescriptorProto_TYPE_BOOL) {
panic("Failed to add bool field 1")
}
// Add Protobuf int64 as field 2 ("int64 intensity = 2;")
if !fm.Add(2, descriptor.FieldDescriptorProto_TYPE_INT64) {
panic("Failed to add bool field 1")
}
// Provide some values for our "status" and "intensity"
values := []dproto.FieldValue{
{
Field: 1, // status field number
Value: bool(true),
},
{
Field: 2, // intensity field number
Value: int64(10),
},
}
// Encode out values into the protobuf message described in fm
bytes, err := fm.EncodeBuffer(values)
if err != nil {
panic("Error Encoding: " + err.Error())
}
fmt.Printf("ProtobufBinary: [ %# x ]\n", bytes)
// Decode all protobuf fields
decodedValues, err := fm.DecodeBuffer(bytes)
if err != nil {
panic("Error Decoding: " + err.Error())
}
for _, val := range decodedValues {
fmt.Printf("%v: %v\n", val.Field, val.Value)
}
// Unordered output:
// ProtobufBinary: [ 0x08 0x01 0x10 0x0a ]
// 1: true
// 2: 10
}