moved err check

This commit is contained in:
obscuren
2014-12-17 12:57:35 +01:00
parent ef4135eabe
commit b1c58b76a9
6 changed files with 102 additions and 17 deletions

View File

@ -2,8 +2,10 @@ package ethutil
import (
"bytes"
"encoding/binary"
"fmt"
"math/big"
"reflect"
)
type RlpEncode interface {
@ -97,6 +99,14 @@ var (
zeroRlp = big.NewInt(0x0)
)
func intlen(i int64) (length int) {
for i > 0 {
i = i >> 8
length++
}
return
}
func Encode(object interface{}) []byte {
var buff bytes.Buffer
@ -168,6 +178,26 @@ func Encode(object interface{}) []byte {
}
WriteSliceHeader(len(b.Bytes()))
buff.Write(b.Bytes())
default:
// This is how it should have been from the start
// needs refactoring (@fjl)
v := reflect.ValueOf(t)
switch v.Kind() {
case reflect.Slice:
var b bytes.Buffer
for i := 0; i < v.Len(); i++ {
b.Write(Encode(v.Index(i).Interface()))
}
blen := b.Len()
if blen < 56 {
buff.WriteByte(byte(blen) + 0xc0)
} else {
buff.WriteByte(byte(intlen(int64(blen))) + 0xf7)
binary.Write(&buff, binary.BigEndian, int64(blen))
}
buff.ReadFrom(&b)
}
}
} else {
// Empty list for nil

View File

@ -7,6 +7,16 @@ import (
"testing"
)
func TestNonInterfaceSlice(t *testing.T) {
vala := []string{"value1", "value2", "value3"}
valb := []interface{}{"value1", "value2", "value3"}
resa := Encode(vala)
resb := Encode(valb)
if !bytes.Equal(resa, resb) {
t.Errorf("expected []string & []interface{} to be equal")
}
}
func TestRlpValueEncoding(t *testing.T) {
val := EmptyValue()
val.AppendList().Append(1).Append(2).Append(3)