Extract the bencode package to github.com/chihaya/bencode

This commit is contained in:
Justin Li 2014-07-06 18:04:05 -04:00
parent e08f745f6c
commit 86fead3056
4 changed files with 2 additions and 148 deletions

View file

@ -1,105 +0,0 @@
// Copyright 2014 The Chihaya Authors. All rights reserved.
// Use of this source code is governed by the BSD 2-Clause license,
// which can be found in the LICENSE file.
// Package bencode implements bencoding of objects as defined in BEP 3 using
// type assertion over reflection for performance.
package bencode
import (
"bytes"
"errors"
"fmt"
"io"
"strconv"
"time"
)
// An Encoder writes Bencoded objects to an output stream.
type Encoder struct {
w io.Writer
}
// NewEncoder returns a new encoder that writes to w.
func NewEncoder(w io.Writer) *Encoder {
return &Encoder{w: w}
}
// Encode writes the bencoding of v to the stream.
func (enc *Encoder) Encode(v interface{}) error {
return marshal(enc.w, v)
}
// Marshal returns the bencoding of v.
func Marshal(v interface{}) ([]byte, error) {
buf := &bytes.Buffer{}
err := marshal(buf, v)
return buf.Bytes(), err
}
// Marshaler is the interface implemented by objects that can marshal
// themselves.
type Marshaler interface {
MarshalBencode() ([]byte, error)
}
// Marshal writes types bencoded to an io.Writer
func marshal(w io.Writer, data interface{}) error {
switch v := data.(type) {
case Marshaler:
bencoded, err := v.MarshalBencode()
if err != nil {
return err
}
_, err = w.Write(bencoded)
if err != nil {
return err
}
case string:
fmt.Fprintf(w, "%d:%s", len(v), v)
case int:
fmt.Fprintf(w, "i%de", v)
case uint:
fmt.Fprintf(w, "i%se", strconv.FormatUint(uint64(v), 10))
case int64:
fmt.Fprintf(w, "i%se", strconv.FormatInt(v, 10))
case uint64:
fmt.Fprintf(w, "i%se", strconv.FormatUint(v, 10))
case time.Duration: // Assume seconds
fmt.Fprintf(w, "i%se", strconv.FormatInt(int64(v/time.Second), 10))
case map[string]interface{}:
fmt.Fprintf(w, "d")
for key, val := range v {
fmt.Fprintf(w, "%s:%s", strconv.Itoa(len(key)), key)
err := marshal(w, val)
if err != nil {
return err
}
}
fmt.Fprintf(w, "e")
case []string:
fmt.Fprintf(w, "l")
for _, val := range v {
err := marshal(w, val)
if err != nil {
return err
}
}
fmt.Fprintf(w, "e")
default:
// Although not currently necessary,
// should handle []interface{} manually; Go can't do it implicitly
return errors.New("bencode: attempted to marshal unsupported type")
}
return nil
}

View file

@ -1,41 +0,0 @@
// Copyright 2014 The Chihaya Authors. All rights reserved.
// Use of this source code is governed by the BSD 2-Clause license,
// which can be found in the LICENSE file.
package bencode
import (
"testing"
"time"
)
var tests = []struct {
input interface{}
expected string
}{
{int(42), "i42e"},
{int(-42), "i-42e"},
{uint(43), "i43e"},
{int64(44), "i44e"},
{uint64(45), "i45e"},
{"example", "7:example"},
{30 * time.Minute, "i1800e"},
{[]string{"one", "two"}, "l3:one3:twoe"},
{[]string{}, "le"},
{map[string]interface{}{"one": "aa", "two": "bb"}, "d3:one2:aa3:two2:bbe"},
{map[string]interface{}{}, "de"},
}
func TestMarshal(t *testing.T) {
for _, test := range tests {
got, err := Marshal(test.input)
if err != nil {
t.Error(err)
} else if string(got) != test.expected {
t.Errorf("\ngot: %s\nexpected: %s", got, test.expected)
}
}
}

View file

@ -11,7 +11,7 @@ import (
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
"github.com/chihaya/chihaya/bencode" "github.com/chihaya/bencode"
"github.com/chihaya/chihaya/drivers/tracker" "github.com/chihaya/chihaya/drivers/tracker"
"github.com/chihaya/chihaya/models" "github.com/chihaya/chihaya/models"
) )

View file

@ -11,7 +11,7 @@ import (
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
"github.com/chihaya/chihaya/bencode" "github.com/chihaya/bencode"
"github.com/chihaya/chihaya/drivers/tracker" "github.com/chihaya/chihaya/drivers/tracker"
"github.com/chihaya/chihaya/models" "github.com/chihaya/chihaya/models"
) )