diff options
author | Felix Lange <fjl@twurst.com> | 2014-12-09 17:39:39 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2014-12-09 17:52:05 +0800 |
commit | 4f12f0697e645a567fac9f13ed8cdb25e1218b84 (patch) | |
tree | 92972f699f6236e09aee3ec7e197f31d60108298 /rlp/decode.go | |
parent | 195b2d2ebdfe1e794c096c9b6c9fe91e9df7af1d (diff) | |
download | dexon-4f12f0697e645a567fac9f13ed8cdb25e1218b84.tar.gz dexon-4f12f0697e645a567fac9f13ed8cdb25e1218b84.tar.zst dexon-4f12f0697e645a567fac9f13ed8cdb25e1218b84.zip |
rlp: move decoder type switch to decode.go
Diffstat (limited to 'rlp/decode.go')
-rw-r--r-- | rlp/decode.go | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/rlp/decode.go b/rlp/decode.go index 7d95af02b..7acbbfa0d 100644 --- a/rlp/decode.go +++ b/rlp/decode.go @@ -81,6 +81,39 @@ func (err decodeError) Error() string { return fmt.Sprintf("rlp: %s for %v", err.msg, err.typ) } +var ( + decoderInterface = reflect.TypeOf(new(Decoder)).Elem() + bigInt = reflect.TypeOf(big.Int{}) +) + +func makeDecoder(typ reflect.Type) (dec decoder, err error) { + kind := typ.Kind() + switch { + case typ.Implements(decoderInterface): + return decodeDecoder, nil + case kind != reflect.Ptr && reflect.PtrTo(typ).Implements(decoderInterface): + return decodeDecoderNoPtr, nil + case typ.AssignableTo(reflect.PtrTo(bigInt)): + return decodeBigInt, nil + case typ.AssignableTo(bigInt): + return decodeBigIntNoPtr, nil + case isInteger(kind): + return makeNumDecoder(typ), nil + case kind == reflect.String: + return decodeString, nil + case kind == reflect.Slice || kind == reflect.Array: + return makeListDecoder(typ) + case kind == reflect.Struct: + return makeStructDecoder(typ) + case kind == reflect.Ptr: + return makePtrDecoder(typ) + case kind == reflect.Interface && typ.NumMethod() == 0: + return decodeInterface, nil + default: + return nil, fmt.Errorf("rlp: type %v is not RLP-serializable", typ) + } +} + func makeNumDecoder(typ reflect.Type) decoder { kind := typ.Kind() switch { |