rlp: add functions for encoding

I'm reasonably confident that the encoding matches the output of
ethutil.Encode for values that it supports. Some of the tests have been
adpated from the Ethereum testing repository.

There are still TODOs in the code.
This commit is contained in:
Felix Lange
2014-12-30 11:40:32 +01:00
parent bb55307a9d
commit 552f5b2693
6 changed files with 861 additions and 18 deletions

View File

@ -5,17 +5,20 @@ import (
"sync"
)
type decoder func(*Stream, reflect.Value) error
type typeinfo struct {
decoder
}
var (
typeCacheMutex sync.RWMutex
typeCache = make(map[reflect.Type]*typeinfo)
)
type typeinfo struct {
decoder
writer
}
type decoder func(*Stream, reflect.Value) error
type writer func(reflect.Value, *encbuf) error
func cachedTypeInfo(typ reflect.Type) (*typeinfo, error) {
typeCacheMutex.RLock()
info := typeCache[typ]
@ -49,11 +52,27 @@ func cachedTypeInfo1(typ reflect.Type) (*typeinfo, error) {
return typeCache[typ], err
}
func structFields(typ reflect.Type) (fields []field, err error) {
for i := 0; i < typ.NumField(); i++ {
if f := typ.Field(i); f.PkgPath == "" { // exported
info, err := cachedTypeInfo1(f.Type)
if err != nil {
return nil, err
}
fields = append(fields, field{i, info})
}
}
return fields, nil
}
func genTypeInfo(typ reflect.Type) (info *typeinfo, err error) {
info = new(typeinfo)
if info.decoder, err = makeDecoder(typ); err != nil {
return nil, err
}
if info.writer, err = makeWriter(typ); err != nil {
return nil, err
}
return info, nil
}