forked from actgardner/gogen-avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
primitive.go
executable file
·182 lines (165 loc) · 3.07 KB
/
primitive.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
// Code generated by github.com/actgardner/gogen-avro. DO NOT EDIT.
/*
* SOURCE:
* example.avsc
*/
package avro
import (
"io"
"math"
)
type ByteWriter interface {
Grow(int)
WriteByte(byte) error
}
type StringWriter interface {
WriteString(string) (int, error)
}
func encodeFloat(w io.Writer, byteCount int, bits uint64) error {
var err error
var bb []byte
bw, ok := w.(ByteWriter)
if ok {
bw.Grow(byteCount)
} else {
bb = make([]byte, 0, byteCount)
}
for i := 0; i < byteCount; i++ {
if bw != nil {
err = bw.WriteByte(byte(bits & 255))
if err != nil {
return err
}
} else {
bb = append(bb, byte(bits&255))
}
bits = bits >> 8
}
if bw == nil {
_, err = w.Write(bb)
return err
}
return nil
}
func encodeInt(w io.Writer, byteCount int, encoded uint64) error {
var err error
var bb []byte
bw, ok := w.(ByteWriter)
// To avoid reallocations, grow capacity to the largest possible size
// for this integer
if ok {
bw.Grow(byteCount)
} else {
bb = make([]byte, 0, byteCount)
}
if encoded == 0 {
if bw != nil {
err = bw.WriteByte(0)
if err != nil {
return err
}
} else {
bb = append(bb, byte(0))
}
} else {
for encoded > 0 {
b := byte(encoded & 127)
encoded = encoded >> 7
if !(encoded == 0) {
b |= 128
}
if bw != nil {
err = bw.WriteByte(b)
if err != nil {
return err
}
} else {
bb = append(bb, b)
}
}
}
if bw == nil {
_, err := w.Write(bb)
return err
}
return nil
}
func writeBool(r bool, w io.Writer) error {
var b byte
if r {
b = byte(1)
}
var err error
if bw, ok := w.(ByteWriter); ok {
err = bw.WriteByte(b)
} else {
bb := make([]byte, 1)
bb[0] = b
_, err = w.Write(bb)
}
if err != nil {
return err
}
return nil
}
func writeBytes(r []byte, w io.Writer) error {
err := writeLong(int64(len(r)), w)
if err != nil {
return err
}
_, err = w.Write(r)
return err
}
func writeDemoSchema(r *DemoSchema, w io.Writer) error {
var err error
err = writeInt(r.IntField, w)
if err != nil {
return err
}
err = writeDouble(r.DoubleField, w)
if err != nil {
return err
}
err = writeString(r.StringField, w)
if err != nil {
return err
}
err = writeBool(r.BoolField, w)
if err != nil {
return err
}
err = writeBytes(r.BytesField, w)
if err != nil {
return err
}
return nil
}
func writeDouble(r float64, w io.Writer) error {
bits := uint64(math.Float64bits(r))
const byteCount = 8
return encodeFloat(w, byteCount, bits)
}
func writeInt(r int32, w io.Writer) error {
downShift := uint32(31)
encoded := uint64((uint32(r) << 1) ^ uint32(r>>downShift))
const maxByteSize = 5
return encodeInt(w, maxByteSize, encoded)
}
func writeLong(r int64, w io.Writer) error {
downShift := uint64(63)
encoded := uint64((r << 1) ^ (r >> downShift))
const maxByteSize = 10
return encodeInt(w, maxByteSize, encoded)
}
func writeString(r string, w io.Writer) error {
err := writeLong(int64(len(r)), w)
if err != nil {
return err
}
if sw, ok := w.(StringWriter); ok {
_, err = sw.WriteString(r)
} else {
_, err = w.Write([]byte(r))
}
return err
}