diff options
author | Felix Lange <fjl@twurst.com> | 2014-12-30 18:40:32 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2015-01-15 18:00:19 +0800 |
commit | 552f5b2693a5d19c126a8116d36ba1f4f6cb76a1 (patch) | |
tree | a28efc122113ab7bb48df40ad3215af304cada2c /rlp/typecache.go | |
parent | bb55307a9d8fa73b0fbc0727f8b80925a87627b7 (diff) | |
download | dexon-552f5b2693a5d19c126a8116d36ba1f4f6cb76a1.tar dexon-552f5b2693a5d19c126a8116d36ba1f4f6cb76a1.tar.gz dexon-552f5b2693a5d19c126a8116d36ba1f4f6cb76a1.tar.bz2 dexon-552f5b2693a5d19c126a8116d36ba1f4f6cb76a1.tar.lz dexon-552f5b2693a5d19c126a8116d36ba1f4f6cb76a1.tar.xz dexon-552f5b2693a5d19c126a8116d36ba1f4f6cb76a1.tar.zst dexon-552f5b2693a5d19c126a8116d36ba1f4f6cb76a1.zip |
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.
Diffstat (limited to 'rlp/typecache.go')
-rw-r--r-- | rlp/typecache.go | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/rlp/typecache.go b/rlp/typecache.go index 52e68a3c5..398f25d90 100644 --- a/rlp/typecache.go +++ b/rlp/typecache.go @@ -5,16 +5,19 @@ import ( "sync" ) -type decoder func(*Stream, reflect.Value) error +var ( + typeCacheMutex sync.RWMutex + typeCache = make(map[reflect.Type]*typeinfo) +) type typeinfo struct { decoder + writer } -var ( - typeCacheMutex sync.RWMutex - typeCache = make(map[reflect.Type]*typeinfo) -) +type decoder func(*Stream, reflect.Value) error + +type writer func(reflect.Value, *encbuf) error func cachedTypeInfo(typ reflect.Type) (*typeinfo, error) { typeCacheMutex.RLock() @@ -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 } |