aboutsummaryrefslogtreecommitdiffstats
path: root/rlp/typecache.go
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2014-12-30 18:40:32 +0800
committerFelix Lange <fjl@twurst.com>2015-01-15 18:00:19 +0800
commit552f5b2693a5d19c126a8116d36ba1f4f6cb76a1 (patch)
treea28efc122113ab7bb48df40ad3215af304cada2c /rlp/typecache.go
parentbb55307a9d8fa73b0fbc0727f8b80925a87627b7 (diff)
downloadgo-tangerine-552f5b2693a5d19c126a8116d36ba1f4f6cb76a1.tar
go-tangerine-552f5b2693a5d19c126a8116d36ba1f4f6cb76a1.tar.gz
go-tangerine-552f5b2693a5d19c126a8116d36ba1f4f6cb76a1.tar.bz2
go-tangerine-552f5b2693a5d19c126a8116d36ba1f4f6cb76a1.tar.lz
go-tangerine-552f5b2693a5d19c126a8116d36ba1f4f6cb76a1.tar.xz
go-tangerine-552f5b2693a5d19c126a8116d36ba1f4f6cb76a1.tar.zst
go-tangerine-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.go29
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
}