rlp: move decoder type switch to decode.go
This commit is contained in:
parent
195b2d2ebd
commit
4f12f0697e
@ -81,6 +81,39 @@ func (err decodeError) Error() string {
|
|||||||
return fmt.Sprintf("rlp: %s for %v", err.msg, err.typ)
|
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 {
|
func makeNumDecoder(typ reflect.Type) decoder {
|
||||||
kind := typ.Kind()
|
kind := typ.Kind()
|
||||||
switch {
|
switch {
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package rlp
|
package rlp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"math/big"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
@ -51,39 +49,12 @@ func cachedTypeInfo1(typ reflect.Type) (*typeinfo, error) {
|
|||||||
return typeCache[typ], err
|
return typeCache[typ], err
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
|
||||||
decoderInterface = reflect.TypeOf(new(Decoder)).Elem()
|
|
||||||
bigInt = reflect.TypeOf(big.Int{})
|
|
||||||
)
|
|
||||||
|
|
||||||
func genTypeInfo(typ reflect.Type) (info *typeinfo, err error) {
|
func genTypeInfo(typ reflect.Type) (info *typeinfo, err error) {
|
||||||
info = new(typeinfo)
|
info = new(typeinfo)
|
||||||
kind := typ.Kind()
|
if info.decoder, err = makeDecoder(typ); err != nil {
|
||||||
switch {
|
return nil, err
|
||||||
case typ.Implements(decoderInterface):
|
|
||||||
info.decoder = decodeDecoder
|
|
||||||
case kind != reflect.Ptr && reflect.PtrTo(typ).Implements(decoderInterface):
|
|
||||||
info.decoder = decodeDecoderNoPtr
|
|
||||||
case typ.AssignableTo(reflect.PtrTo(bigInt)):
|
|
||||||
info.decoder = decodeBigInt
|
|
||||||
case typ.AssignableTo(bigInt):
|
|
||||||
info.decoder = decodeBigIntNoPtr
|
|
||||||
case isInteger(kind):
|
|
||||||
info.decoder = makeNumDecoder(typ)
|
|
||||||
case kind == reflect.String:
|
|
||||||
info.decoder = decodeString
|
|
||||||
case kind == reflect.Slice || kind == reflect.Array:
|
|
||||||
info.decoder, err = makeListDecoder(typ)
|
|
||||||
case kind == reflect.Struct:
|
|
||||||
info.decoder, err = makeStructDecoder(typ)
|
|
||||||
case kind == reflect.Ptr:
|
|
||||||
info.decoder, err = makePtrDecoder(typ)
|
|
||||||
case kind == reflect.Interface && typ.NumMethod() == 0:
|
|
||||||
info.decoder = decodeInterface
|
|
||||||
default:
|
|
||||||
err = fmt.Errorf("rlp: type %v is not RLP-serializable", typ)
|
|
||||||
}
|
}
|
||||||
return info, err
|
return info, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func isInteger(k reflect.Kind) bool {
|
func isInteger(k reflect.Kind) bool {
|
||||||
|
Loading…
Reference in New Issue
Block a user