forked from linkedin/goavro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder.go
155 lines (145 loc) · 4.11 KB
/
decoder.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
// Copyright 2015 LinkedIn Corp. Licensed under the Apache License,
// Version 2.0 (the "License"); you may not use this file except in
// compliance with the License.
You may obtain a copy of the License
// at http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
//
distributed under the License is distributed on an "AS IS" BASIS,
//
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied.Copyright [201X] LinkedIn Corp. Licensed under the Apache
// License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License.
You may obtain a copy of
// the License at http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
//
distributed under the License is distributed on an "AS IS" BASIS,
//
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied.
package goavro
import (
"encoding/binary"
"fmt"
"io"
"math"
)
func nullDecoder(_ io.Reader) (interface{}, error) {
return nil, nil
}
func booleanDecoder(r io.Reader) (interface{}, error) {
bb := make([]byte, 1)
_, err := r.Read(bb)
if err != nil {
return nil, fmt.Errorf("cannot decode boolean: %v", err)
}
var datum bool
switch bb[0] {
case byte(0):
// zero value of boolean is false
case byte(1):
datum = true
default:
return nil, fmt.Errorf("cannot decode boolean: %x", bb[0])
}
return datum, nil
}
func intDecoder(r io.Reader) (interface{}, error) {
var v int
var err error
bb := make([]byte, 1)
for shift := uint(0); ; shift += 7 {
_, err = r.Read(bb)
if err != nil {
return nil, fmt.Errorf("cannot decode int: %v", err)
}
b := bb[0]
v |= int(b&mask) << shift
if b&flag == 0 {
break
}
}
datum := (int32(v>>1) ^ -int32(v&1))
return datum, nil
}
func longDecoder(r io.Reader) (interface{}, error) {
var v int
var err error
bb := make([]byte, 1)
for shift := uint(0); ; shift += 7 {
_, err = r.Read(bb)
if err != nil {
return nil, fmt.Errorf("cannot decode long: %v", err)
}
b := bb[0]
v |= int(b&mask) << shift
if b&flag == 0 {
break
}
}
datum := (int64(v>>1) ^ -int64(v&1))
return datum, nil
}
func floatDecoder(r io.Reader) (interface{}, error) {
buf := make([]byte, 4)
_, err := r.Read(buf)
if err != nil {
return nil, fmt.Errorf("cannot decode float: %v", err)
}
bits := binary.LittleEndian.Uint32(buf)
datum := math.Float32frombits(bits)
return datum, nil
}
func doubleDecoder(r io.Reader) (interface{}, error) {
buf := make([]byte, 8)
_, err := r.Read(buf)
if err != nil {
return nil, fmt.Errorf("cannot decode double: %v", err)
}
datum := math.Float64frombits(binary.LittleEndian.Uint64(buf))
return datum, nil
}
func bytesDecoder(r io.Reader) (interface{}, error) {
someValue, err := longDecoder(r)
if err != nil {
return nil, fmt.Errorf("cannot decode bytes: %v", err)
}
size, ok := someValue.(int64)
if !ok {
return nil, fmt.Errorf("cannot decode bytes: expected int64; actual: %T", someValue)
}
if size < 0 {
return nil, fmt.Errorf("cannot decode bytes: negative length: %d", size)
}
buf := make([]byte, size)
bytesRead, err := r.Read(buf)
if err != nil {
return nil, fmt.Errorf("cannot decode bytes: %v", err)
}
if int64(bytesRead) < size {
return nil, fmt.Errorf("cannot decode bytes: buffer underrun")
}
return buf, nil
}
func stringDecoder(r io.Reader) (interface{}, error) {
// NOTE: could have implemented in terms of makeBytesDecoder,
// but prefer to not have nested error messages
someValue, err := longDecoder(r)
if err != nil {
return nil, fmt.Errorf("cannot decode string: %v", err)
}
size, ok := someValue.(int64)
if !ok {
return nil, fmt.Errorf("cannot decode string: expected int64; actual: %T", someValue)
}
if size < 0 {
return nil, fmt.Errorf("cannot decode string: negative length: %d", size)
}
buf := make([]byte, size)
byteCount, err := r.Read(buf)
if err != nil {
return nil, fmt.Errorf("cannot decode string: %v", err)
}
if int64(byteCount) < size {
return nil, fmt.Errorf("cannot decode string: buffer underrun")
}
return string(buf), nil
}