bencode: Handle encoding big.Ints
This commit is contained in:
parent
79a0e0f1fd
commit
74c5d425fb
@ -2,6 +2,7 @@ package bencode
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"math/big"
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sort"
|
"sort"
|
||||||
@ -110,6 +111,14 @@ func (e *Encoder) reflectValue(v reflect.Value) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch t := v.Interface().(type) {
|
||||||
|
case big.Int:
|
||||||
|
e.writeString("i")
|
||||||
|
e.writeString(t.String())
|
||||||
|
e.writeString("e")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
switch v.Kind() {
|
switch v.Kind() {
|
||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
if v.Bool() {
|
if v.Bool() {
|
||||||
|
@ -3,6 +3,7 @@ package bencode
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/big"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@ -68,6 +69,16 @@ var random_encode_tests = []random_encode_test{
|
|||||||
{struct {
|
{struct {
|
||||||
A *string `bencode:",omitempty"`
|
A *string `bencode:",omitempty"`
|
||||||
}{new(string)}, "d1:A0:e"},
|
}{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) {
|
func TestRandomEncode(t *testing.T) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user