-
Notifications
You must be signed in to change notification settings - Fork 18
/
molecule.go
166 lines (145 loc) · 4.56 KB
/
molecule.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
package molecule
import (
"fmt"
"github.com/richardartoul/molecule/src/codec"
)
// MessageEachFn is a function that will be called for each top-level field in a
// message passed to MessageEach.
type MessageEachFn func(fieldNum int32, value Value) (bool, error)
// MessageEach iterates over each top-level field in the message stored in buffer
// and calls fn on each one.
func MessageEach(buffer *codec.Buffer, fn MessageEachFn) error {
for !buffer.EOF() {
v, err := buffer.DecodeVarint()
if err != nil {
return err
}
fieldNum, wireType, err := codec.AsTagAndWireType(v)
if err != nil {
return err
}
value := Value{
WireType: wireType,
}
switch wireType {
case codec.WireVarint:
value.Number, err = buffer.DecodeVarint()
case codec.WireFixed32:
value.Number, err = buffer.DecodeFixed32()
case codec.WireFixed64:
value.Number, err = buffer.DecodeFixed64()
case codec.WireBytes:
value.Bytes, err = buffer.DecodeRawBytes(false)
case codec.WireStartGroup, codec.WireEndGroup:
err = fmt.Errorf("MessageEach: encountered group wire type: %d. Groups not supported", wireType)
default:
err = fmt.Errorf("MessageEach: unknown wireType: %d", wireType)
}
if err != nil {
return fmt.Errorf("MessageEach: error reading value from buffer: %v", err)
}
shouldContinue, err := fn(fieldNum, value)
if err != nil || !shouldContinue {
return err
}
}
return nil
}
// Next populates the given value with the next value in the field and returns the field number or an error if one
// was encountered while reading the next field value
func Next(buffer *codec.Buffer, value *Value) (fieldNum int32, err error) {
var v uint64
var wireType codec.WireType
v, err = buffer.DecodeVarint()
if err != nil {
return
}
fieldNum, wireType, err = codec.AsTagAndWireType(v)
if err != nil {
return
}
value.WireType = wireType
switch wireType {
case codec.WireVarint:
value.Number, err = buffer.DecodeVarint()
case codec.WireFixed32:
value.Number, err = buffer.DecodeFixed32()
case codec.WireFixed64:
value.Number, err = buffer.DecodeFixed64()
case codec.WireBytes:
value.Bytes, err = buffer.DecodeRawBytes(false)
case codec.WireStartGroup, codec.WireEndGroup:
err = fmt.Errorf("MessageEach: encountered group wire type: %d. Groups not supported", wireType)
default:
err = fmt.Errorf("MessageEach: unknown wireType: %d", wireType)
}
if err != nil {
err = fmt.Errorf("MessageEach: error reading value from buffer: %v", err)
return
}
return
}
// PackedRepeatedEachFn is a function that is called for each value in a repeated field.
type PackedRepeatedEachFn func(value Value) (bool, error)
// PackedRepeatedEach iterates over each value in the packed repeated field stored in buffer
// and calls fn on each one.
//
// The fieldType argument should match the type of the value stored in the repeated field.
//
// PackedRepeatedEach only supports repeated fields encoded using packed encoding.
func PackedRepeatedEach(buffer *codec.Buffer, fieldType codec.FieldType, fn PackedRepeatedEachFn) error {
var wireType codec.WireType
switch fieldType {
case codec.FieldType_INT32,
codec.FieldType_INT64,
codec.FieldType_UINT32,
codec.FieldType_UINT64,
codec.FieldType_SINT32,
codec.FieldType_SINT64,
codec.FieldType_BOOL,
codec.FieldType_ENUM:
wireType = codec.WireVarint
case codec.FieldType_FIXED64,
codec.FieldType_SFIXED64,
codec.FieldType_DOUBLE:
wireType = codec.WireFixed64
case codec.FieldType_FIXED32,
codec.FieldType_SFIXED32,
codec.FieldType_FLOAT:
wireType = codec.WireFixed32
case codec.FieldType_STRING,
codec.FieldType_MESSAGE,
codec.FieldType_BYTES:
wireType = codec.WireBytes
default:
return fmt.Errorf(
"PackedRepeatedEach: unknown field type: %v", fieldType)
}
for !buffer.EOF() {
var err error
value := Value{
WireType: wireType,
}
switch wireType {
case codec.WireVarint:
value.Number, err = buffer.DecodeVarint()
case codec.WireFixed32:
value.Number, err = buffer.DecodeFixed32()
case codec.WireFixed64:
value.Number, err = buffer.DecodeFixed64()
case codec.WireBytes:
value.Bytes, err = buffer.DecodeRawBytes(false)
case codec.WireStartGroup, codec.WireEndGroup:
err = fmt.Errorf("PackedRepeatedEach: encountered group wire type: %d. Groups not supported", wireType)
default:
err = fmt.Errorf("PackedRepeatedEach: unknown wireType: %d", wireType)
}
if err != nil {
return fmt.Errorf("PackedRepeatedEach: error reading value from buffer: %v", err)
}
if shouldContinue, err := fn(value); err != nil || !shouldContinue {
return err
}
}
return nil
}