bencode: add missing error handling

In addition, this PR attempts to simplify some functions according to
the output of `gocyclo -n 15`.
This commit is contained in:
Jimmy Zelinskie 2016-09-06 23:40:41 -04:00
parent 6b376e3522
commit 2415bc71c6
3 changed files with 198 additions and 136 deletions
frontend/http/bencode

View file

@ -2,6 +2,11 @@
// type assertion over reflection for performance.
package bencode
import "bytes"
// Enforce that Dict implements the Marshaler interface.
var _ Marshaler = Dict{}
// Dict represents a bencode dictionary.
type Dict map[string]interface{}
@ -10,9 +15,26 @@ func NewDict() Dict {
return make(Dict)
}
// MarshalBencode implements the Marshaler interface for Dict.
func (d Dict) MarshalBencode() ([]byte, error) {
var buf bytes.Buffer
err := marshalMap(&buf, map[string]interface{}(d))
return buf.Bytes(), err
}
// Enforce that List implements the Marshaler interface.
var _ Marshaler = List{}
// List represents a bencode list.
type List []interface{}
// MarshalBencode implements the Marshaler interface for List.
func (l List) MarshalBencode() ([]byte, error) {
var buf bytes.Buffer
err := marshalList(&buf, []interface{}(l))
return buf.Bytes(), err
}
// NewList allocates the memory for a List.
func NewList() List {
return make(List, 0)