/
bert.go
239 lines (202 loc) · 5.37 KB
/
bert.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package bert
import (
"fmt";
"io";
"math";
"os";
"reflect";
"strings";
"encoding/binary";
)
const (
SMALL_INT = 97;
INT = 98;
FLOAT = 99;
ATOM = 100;
SMALL_TUPLE = 104;
LARGE_TUPLE = 105;
NIL = 106;
STRING = 107;
LIST = 108;
BIN = 109;
SMALL_BIGNUM = 110;
LARGE_BIGNUM = 111;
MAGIC = 131;
)
func writeByte(w io.Writer, data byte) os.Error {
_, err := w.Write([]byte {data});
return err;
}
func writeAtom(w io.Writer, atom string) os.Error {
writeByte(w, ATOM);
writeByte(w, uint8(len(atom)));
io.WriteString(w, atom);
return nil;
}
func writeTuple(w io.Writer, tuple []interface{}) os.Error {
if len(tuple) <= 256 {
writeByte(w, SMALL_TUPLE);
} else {
writeByte(w, LARGE_TUPLE);
}
binary.Write(w, binary.BigEndian, len(tuple));
for i := 0; i < len(tuple); i++ {
if err := writeValue(w, reflect.NewValue(tuple[i])); err != nil {
return err
}
}
return nil;
}
func writeNil(w io.Writer) os.Error {
w.Write([]byte {SMALL_TUPLE, 2});
writeAtom(w, "bert");
writeAtom(w, "nil");
return nil
}
func writeBool(w io.Writer, v bool) os.Error {
w.Write([]byte {SMALL_TUPLE, 2});
writeAtom(w, "bert");
if v {
writeAtom(w, "true")
} else {
writeAtom(w, "false")
}
return nil
}
func writeUint8(w io.Writer, v uint8) (err os.Error) {
_, err = w.Write([]byte {SMALL_INT, v});
return
}
func writeUint16(w io.Writer, v uint16) (err os.Error) {
_, err = w.Write([] byte { SMALL_INT,
byte(v >> 8),
byte(v)
});
return
}
func writeUint32(w io.Writer, v uint32) (err os.Error) {
_, err = w.Write([] byte {
SMALL_INT,
byte(v >> 24),
byte(v >> 16),
byte(v >> 8),
byte(v)
});
return
}
func writeUint64(w io.Writer, v uint64) (err os.Error) {
_, err = w.Write([] byte {
SMALL_BIGNUM,
8,
0,
byte(v >> 56),
byte(v >> 48),
byte(v >> 40),
byte(v >> 32),
byte(v >> 24),
byte(v >> 16),
byte(v >> 8),
byte(v)
});
return
}
func writeInt64(w io.Writer, v int64) (err os.Error) {
var sign byte;
if v > 0 {
sign = 0
} else {
sign = 1;
v = -v
};
_, err = w.Write([] byte {
SMALL_BIGNUM,
8,
sign,
byte(v),
byte(v >> 8),
byte(v >> 16),
byte(v >> 24),
byte(v >> 32),
byte(v >> 40),
byte(v >> 48),
byte(v >> 56)
});
return
}
func writeBinary(w io.Writer, data []byte) os.Error {
l := len(data);
if l > math.MaxInt32 {
return os.NewError(fmt.Sprintf("bert.Encode: binary too large: %d (max is %d)", l, math.MaxInt32));
}
writeByte(w, BIN);
binary.Write(w, binary.BigEndian, uint32(l));
w.Write(data);
return nil
}
func writeFloat(w io.Writer, v string) os.Error {
writeByte(w, FLOAT);
fmt.Fprintf(w, v);
return nil
}
func writeArrayOrSlice(w io.Writer, val reflect.ArrayOrSliceValue) os.Error {
writeByte(w, LIST);
binary.Write(w, binary.BigEndian, uint32(val.Len()));
for i := 0; i < val.Len(); i++ {
if err := writeValue(w, val.Elem(i)); err != nil {
return err
}
}
writeByte(w, NIL);
return nil;
}
func writeMap(w io.Writer, val *reflect.MapValue) os.Error {
writeByte(w, SMALL_TUPLE);
writeByte(w, 3);
writeAtom(w, "bert");
writeAtom(w, "dict");
keys := val.Keys();
writeByte(w, LIST);
binary.Write(w, binary.BigEndian, uint32(len(keys)));
for i := 0; i < len(keys); i++ {
w.Write([]byte {SMALL_TUPLE, 2});
writeValue(w, keys[i]);
writeValue(w, val.Elem(keys[i]));
}
writeByte(w, NIL);
return nil;
}
// [ag] Peculiarity: binary.Write(io.Writer, interface{}) can only write "fixed-
// size values". A fixed-size value is either a fixed-size integer (int8, uint8,
// int16, uint16, ...) or an array or struct containing only fixed-size values.
// Thus in the big switch, matching on reflect.IntValue is no good. Workaround?
func writeValue(w io.Writer, val reflect.Value) (err os.Error) {
switch v := val.(type) {
case nil: return writeNil(w)
case *reflect.BoolValue: return writeBool(w, v.Get())
case *reflect.Uint8Value: return writeUint8(w, v.Get())
case *reflect.Int8Value: return writeUint8(w, uint8(v.Get()))
case *reflect.Uint16Value: return writeUint16(w, v.Get())
case *reflect.Int16Value: return writeUint16(w, uint16(v.Get()))
case *reflect.Uint32Value: return writeUint32(w, v.Get())
case *reflect.Int32Value: return writeUint32(w, uint32(v.Get()))
case *reflect.Uint64Value: return writeUint64(w, v.Get())
case *reflect.Int64Value: return writeInt64(w, v.Get())
case *reflect.FloatValue: return writeFloat(w, fmt.Sprintf("%.20e", v.Get()))
case *reflect.Float32Value: return writeFloat(w, fmt.Sprintf("%.20e", v.Get()))
case *reflect.Float64Value: return writeFloat(w, fmt.Sprintf("%.20e", v.Get()))
case *reflect.StringValue: return writeBinary(w, strings.Bytes(v.Get()))
case *reflect.ArrayValue: return writeArrayOrSlice(w, v)
case *reflect.SliceValue: return writeArrayOrSlice(w, v)
case *reflect.MapValue: return writeMap(w, v)
default:
return os.NewError("bert.Encode: invalid type " + v.Type().String())
}
return nil;
}
func Encode(w io.Writer, val interface{}) os.Error {
writeByte(w, MAGIC);
return writeValue(w, reflect.NewValue(val))
}
func Decode(r io.Reader) (data interface{}, err os.Error) {
return nil, nil;
}