forked from sethwklein/gobert
/
encode.go
140 lines (115 loc) · 2.55 KB
/
encode.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
package bert
import (
"bytes";
"encoding/binary";
"fmt";
"io";
"os";
"reflect";
"strings";
)
func write1(w io.Writer, ui8 uint8) { w.Write([]byte{ui8}) }
func write2(w io.Writer, ui16 uint16) {
b := make([]byte, 2);
binary.BigEndian.PutUint16(b, ui16);
w.Write(b);
}
func write4(w io.Writer, ui32 uint32) {
b := make([]byte, 4);
binary.BigEndian.PutUint32(b, ui32);
w.Write(b);
}
func writeSmallInt(w io.Writer, n int) {
write1(w, SmallIntTag);
write1(w, uint8(n));
}
func writeInt(w io.Writer, n int) {
write1(w, IntTag);
write4(w, uint32(n));
}
func writeFloat(w io.Writer, f float) {
write1(w, FloatTag);
s := fmt.Sprintf("%.20e", float32(f));
w.Write(strings.Bytes(s));
pad := make([]byte, 31-len(s));
w.Write(pad);
}
func writeAtom(w io.Writer, a string) {
write1(w, AtomTag);
write2(w, uint16(len(a)));
w.Write(strings.Bytes(a));
}
func writeSmallTuple(w io.Writer, t *reflect.SliceValue) {
write1(w, SmallTupleTag);
size := t.Len();
write1(w, uint8(size));
for i := 0; i < size; i++ {
writeTag(w, t.Elem(i))
}
}
func writeNil(w io.Writer) { write1(w, NilTag) }
func writeString(w io.Writer, s string) {
write1(w, StringTag);
write2(w, uint16(len(s)));
w.Write(strings.Bytes(s));
}
func writeList(w io.Writer, l *reflect.ArrayValue) {
write1(w, ListTag);
size := l.Len();
write4(w, uint32(size));
for i := 0; i < size; i++ {
writeTag(w, l.Elem(i))
}
writeNil(w);
}
func writeTag(w io.Writer, val reflect.Value) (err os.Error) {
switch v := val.(type) {
case *reflect.IntValue:
n := v.Get();
if n >= 0 && n < 256 {
writeSmallInt(w, n)
} else {
writeInt(w, n)
}
case *reflect.FloatValue:
writeFloat(w, v.Get())
case *reflect.StringValue:
if v.Type().Name() == "Atom" {
writeAtom(w, v.Get())
} else {
writeString(w, v.Get())
}
case *reflect.SliceValue:
writeSmallTuple(w, v)
case *reflect.ArrayValue:
writeList(w, v)
case *reflect.InterfaceValue:
writeTag(w, v.Elem())
default:
if reflect.Indirect(val) == nil {
writeNil(w)
} else {
err = ErrUnknownType
}
}
return;
}
func EncodeTo(w io.Writer, val interface{}) (err os.Error) {
write1(w, VersionTag);
err = writeTag(w, reflect.NewValue(val));
return;
}
func Encode(val interface{}) ([]byte, os.Error) {
buf := bytes.NewBuffer([]byte{});
err := EncodeTo(buf, val);
return buf.Bytes(), err;
}
func Marshal(w io.Writer, val interface{}) os.Error {
return EncodeTo(w, val)
}
func MarshalResponse(w io.Writer, val interface{}) (err os.Error) {
resp, err := Encode(val);
write4(w, uint32(len(resp)));
w.Write(resp);
return;
}