-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.go
168 lines (151 loc) · 3.96 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
158
159
160
161
162
163
164
165
166
167
168
package bencode
import (
"bufio"
"fmt"
"io"
"reflect"
"strconv"
"strings"
)
type countWriter struct {
*bufio.Writer
count int
}
func (w *countWriter) WriteByte(b byte) error {
w.count++
return w.Writer.WriteByte(b)
}
func (w *countWriter) Write(b []byte) (int, error) {
n, err := w.Writer.Write(b)
w.count += n
return n, err
}
func encode(obj any, w io.Writer) error {
cw := &countWriter{
Writer: bufio.NewWriter(w),
count: 0,
}
return encodeItem(obj, cw)
}
func encodeItem(obj any, w *countWriter) error {
switch reflect.TypeOf(obj).Kind() {
case reflect.Int:
return encodeInt(obj.(int), w)
case reflect.String:
return encodeString(obj.(string), w)
case reflect.Slice:
return encodeList(obj.([]any), w)
case reflect.Map:
// TODO
case reflect.Struct:
return encodeStruct(obj, w)
default:
return fmt.Errorf("unsupported type %s", reflect.TypeOf(obj).Kind())
}
return nil
}
func encodeInt(integer int, w *countWriter) error {
if integer == 0 {
return nil
}
n, err := w.Write([]byte("i"))
if err != nil {
return fmt.Errorf("failed writing int initial delimiter. Write('i') -> %w", err)
}
if n != 1 {
return fmt.Errorf("failed writing int initial delimiter -> n != 1")
}
str := strconv.Itoa(integer)
n, err = w.Write([]byte(str))
if err != nil {
return fmt.Errorf("failed writing int value. Write(str) -> %w", err)
}
if n != len(str) {
return fmt.Errorf("partial int value write")
}
n, err = w.Write([]byte("e"))
if err != nil {
return fmt.Errorf("failed writing int end delimiter. Write('e') -> %w", err)
}
if n != 1 {
return fmt.Errorf("failed writing int end delimiter -> n != 1")
}
return nil
}
func encodeString(str string, w *countWriter) error {
if len(str) == 0 {
return nil
}
lengthStr := strconv.Itoa(len(str))
n, err := w.Write([]byte(lengthStr))
if err != nil {
return fmt.Errorf("failed writing string length. Write(lengthStr) -> %w", err)
}
if n != len(lengthStr) {
return fmt.Errorf("partial write of string length -> n != len(lengthStr)")
}
n, err = w.WriteString(":" + str)
if err != nil {
return fmt.Errorf("failed writing string. Write(':' + str) -> %w", err)
}
if n != len(str)+1 {
return fmt.Errorf("partial write of string -> n != len(str) + 1")
}
return nil
}
func encodeList(list []any, w *countWriter) error {
if len(list) == 0 {
return nil
}
n, err := w.Write([]byte("l"))
if err != nil {
return fmt.Errorf("failed writing list initial delimiter. Write('l') -> %w", err)
}
if n != 1 {
return fmt.Errorf("failed writing list initial delimiter -> n != 1")
}
for _, el := range list {
if err := encodeItem(el, w); err != nil {
return fmt.Errorf("failed encoding list item. encodeItem(el) -> %w", err)
}
}
n, err = w.Write([]byte("e"))
if err != nil {
return fmt.Errorf("failed writing list initial delimiter. Write('e') -> %w", err)
}
if n != 1 {
return fmt.Errorf("failed writing list initial delimiter -> n != 1")
}
return nil
}
func encodeStruct(obj any, w *countWriter) error {
n, err := w.Write([]byte("d"))
if err != nil {
return fmt.Errorf("failed writing dict initial delimiter. Write('d') -> %w", err)
}
if n != 1 {
return fmt.Errorf("failed writing dict start delimiter -> n != 1")
}
objVal := reflect.ValueOf(obj)
for i := 0; i < objVal.NumField(); i++ {
field := objVal.Type().Field(i)
tag, _ := field.Tag.Lookup("bencode")
if tag == "" {
continue
}
if err := encodeString(strings.Split(tag, ",")[0], w); err != nil {
return fmt.Errorf("failed encoding key '%s' for struct field '%s'. encodeStr() -> %w", tag, field.Name, err)
}
if err := encodeItem(objVal.Field(i).Interface(), w); err != nil {
return fmt.Errorf("failed encoding key '%s' for struct field '%s'. encodeItem() -> %w", tag, field.Name, err)
}
}
n, err = w.Write([]byte("e"))
if err != nil {
return fmt.Errorf("failed writing dict end delimiter. Write('e') -> %w", err)
}
if n != 1 {
return fmt.Errorf("failed writing dict end delimiter -> n != 1")
}
return nil
}