-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.go
157 lines (133 loc) · 2.74 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package bencode
import (
"bytes"
"errors"
"sort"
"strconv"
)
var ErrUnsupportedType = errors.New("unsupported type")
// Encode accepts either string/[]byte, int/int8/int16/int32/int64, []interface{} or map[string]interface{},
// interface{} being anything from mentioned types
// TODO: consider using io.Writer and writing directly to it
func Encode(value interface{}) ([]byte, error) {
buf := &bytes.Buffer{}
err := encodeObject(buf, value)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func encodeObject(buf *bytes.Buffer, i interface{}) error {
switch val := i.(type) {
case int:
return encodeInt(buf, int64(val))
case int8:
return encodeInt(buf, int64(val))
case int16:
return encodeInt(buf, int64(val))
case int32:
return encodeInt(buf, int64(val))
case int64:
return encodeInt(buf, val)
case []byte:
return encodeBytes(buf, val)
case string:
return encodeString(buf, val)
case []interface{}:
return encodeList(buf, val)
case map[string]interface{}:
return encodeDictionary(buf, val)
}
return ErrUnsupportedType
}
func encodeInt(buf *bytes.Buffer, i int64) error {
// i
err := buf.WriteByte(IntegerStart)
if err != nil {
return err
}
// actual digits
_, err = buf.WriteString(strconv.FormatInt(i, 10))
if err != nil {
return err
}
// e
return buf.WriteByte(IntegerEnd)
}
func encodeString(buf *bytes.Buffer, s string) error {
// length
_, err := buf.WriteString(strconv.Itoa(len(s)))
if err != nil {
return err
}
// :
_, err = buf.Write(StringDelimiter)
if err != nil {
return err
}
// actual string
_, err = buf.WriteString(s)
return err
}
// TODO: try to merge with encodeString
// but without conversion []byte(str)
func encodeBytes(buf *bytes.Buffer, b []byte) error {
// length
_, err := buf.WriteString(strconv.Itoa(len(b)))
if err != nil {
return err
}
// :
_, err = buf.Write(StringDelimiter)
if err != nil {
return err
}
// actual bytes
_, err = buf.Write(b)
return err
}
func encodeList(buf *bytes.Buffer, l []interface{}) error {
// l
err := buf.WriteByte(ListStart)
if err != nil {
return err
}
// actual members
for _, m := range l {
err = encodeObject(buf, m)
if err != nil {
return err
}
}
// e
return buf.WriteByte(ListEnd)
}
func encodeDictionary(buf *bytes.Buffer, d map[string]interface{}) error {
// d
err := buf.WriteByte(DictionaryStart)
if err != nil {
return err
}
// gather all keys
keys := make([]string, len(d))
i := 0
for k := range d {
keys[i] = k
i++
}
// order them
sort.Strings(keys)
// encode KVs
for _, k := range keys {
err = encodeString(buf, k)
if err != nil {
return err
}
err = encodeObject(buf, d[k])
if err != nil {
return err
}
}
// e
return buf.WriteByte(DictionaryEnd)
}