bencode: Handle encoding big.Ints

This commit is contained in:
Matt Joiner 2017-11-08 21:34:24 +11:00
parent 79a0e0f1fd
commit 74c5d425fb
2 changed files with 20 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package bencode
import (
"io"
"math/big"
"reflect"
"runtime"
"sort"
@ -110,6 +111,14 @@ func (e *Encoder) reflectValue(v reflect.Value) {
return
}
switch t := v.Interface().(type) {
case big.Int:
e.writeString("i")
e.writeString(t.String())
e.writeString("e")
return
}
switch v.Kind() {
case reflect.Bool:
if v.Bool() {

View File

@ -3,6 +3,7 @@ package bencode
import (
"bytes"
"fmt"
"math/big"
"testing"
"github.com/stretchr/testify/assert"
@ -68,6 +69,16 @@ var random_encode_tests = []random_encode_test{
{struct {
A *string `bencode:",omitempty"`
}{new(string)}, "d1:A0:e"},
{bigIntFromString("62208002200000000000"), "i62208002200000000000e"},
{*bigIntFromString("62208002200000000000"), "i62208002200000000000e"},
}
func bigIntFromString(s string) *big.Int {
bi, ok := new(big.Int).SetString(s, 10)
if !ok {
panic(s)
}
return bi
}
func TestRandomEncode(t *testing.T) {