mirror of
https://github.com/tendermint/tendermint.git
synced 2026-05-31 11:26:20 +00:00
panic wrapper functions
This commit is contained in:
@@ -5,6 +5,8 @@ import (
|
||||
"errors"
|
||||
"io"
|
||||
"reflect"
|
||||
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
)
|
||||
|
||||
// TODO document and maybe make it configurable.
|
||||
@@ -37,10 +39,10 @@ func ReadBinaryPtr(o interface{}, r io.Reader, n *int64, err *error) interface{}
|
||||
rv, rt := reflect.ValueOf(o), reflect.TypeOf(o)
|
||||
if rv.Kind() == reflect.Ptr {
|
||||
readReflectBinary(rv.Elem(), rt.Elem(), Options{}, r, n, err)
|
||||
return o
|
||||
} else {
|
||||
panic("ReadBinaryPtr expects o to be a pointer")
|
||||
PanicSanity("ReadBinaryPtr expects o to be a pointer")
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
func WriteBinary(o interface{}, w io.Writer, n *int64, err *error) {
|
||||
@@ -93,10 +95,10 @@ func ReadJSONObjectPtr(o interface{}, object interface{}, err *error) interface{
|
||||
rv, rt := reflect.ValueOf(o), reflect.TypeOf(o)
|
||||
if rv.Kind() == reflect.Ptr {
|
||||
readReflectJSON(rv.Elem(), rt.Elem(), object, err)
|
||||
return o
|
||||
} else {
|
||||
panic("ReadJSON(Object)Ptr expects o to be a pointer")
|
||||
PanicSanity("ReadJSON(Object)Ptr expects o to be a pointer")
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
func WriteJSON(o interface{}, w io.Writer, n *int64, err *error) {
|
||||
|
||||
@@ -40,8 +40,7 @@ const (
|
||||
func BasicCodecEncoder(o interface{}, w io.Writer, n *int64, err *error) {
|
||||
switch o := o.(type) {
|
||||
case nil:
|
||||
// SANITY CHECK
|
||||
panic("nil type unsupported")
|
||||
PanicSanity("nil type unsupported")
|
||||
case byte:
|
||||
WriteByte(typeByte, w, n, err)
|
||||
WriteByte(o, w, n, err)
|
||||
@@ -85,8 +84,7 @@ func BasicCodecEncoder(o interface{}, w io.Writer, n *int64, err *error) {
|
||||
WriteByte(typeTime, w, n, err)
|
||||
WriteTime(o, w, n, err)
|
||||
default:
|
||||
// SANITY CHECK
|
||||
panic(fmt.Sprintf("Unsupported type: %v", reflect.TypeOf(o)))
|
||||
PanicSanity(fmt.Sprintf("Unsupported type: %v", reflect.TypeOf(o)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,8 +159,7 @@ func BasicCodecComparator(o1 interface{}, o2 interface{}) int {
|
||||
case time.Time:
|
||||
return int(o1.(time.Time).UnixNano() - o2.(time.Time).UnixNano())
|
||||
default:
|
||||
// SANITY CHECK
|
||||
panic(Fmt("Unsupported type: %v", reflect.TypeOf(o1)))
|
||||
PanicSanity(Fmt("Unsupported type: %v", reflect.TypeOf(o1)))
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -70,8 +70,7 @@ func (info StructFieldInfo) unpack() (int, reflect.Type, Options) {
|
||||
func GetTypeFromStructDeclaration(o interface{}) reflect.Type {
|
||||
rt := reflect.TypeOf(o)
|
||||
if rt.NumField() != 1 {
|
||||
// SANITY CHECK
|
||||
panic("Unexpected number of fields in struct-wrapped declaration of type")
|
||||
PanicSanity("Unexpected number of fields in struct-wrapped declaration of type")
|
||||
}
|
||||
return rt.Field(0).Type
|
||||
}
|
||||
@@ -79,8 +78,7 @@ func GetTypeFromStructDeclaration(o interface{}) reflect.Type {
|
||||
func SetByteForType(typeByte byte, rt reflect.Type) {
|
||||
typeInfo := GetTypeInfo(rt)
|
||||
if typeInfo.Byte != 0x00 && typeInfo.Byte != typeByte {
|
||||
// SANITY CHECK
|
||||
panic(Fmt("Type %v already registered with type byte %X", rt, typeByte))
|
||||
PanicSanity(Fmt("Type %v already registered with type byte %X", rt, typeByte))
|
||||
}
|
||||
typeInfo.Byte = typeByte
|
||||
// If pointer, we need to set it for the concrete type as well.
|
||||
@@ -124,8 +122,7 @@ type ConcreteType struct {
|
||||
func RegisterInterface(o interface{}, ctypes ...ConcreteType) *TypeInfo {
|
||||
it := GetTypeFromStructDeclaration(o)
|
||||
if it.Kind() != reflect.Interface {
|
||||
// SANITY CHECK
|
||||
panic("RegisterInterface expects an interface")
|
||||
PanicSanity("RegisterInterface expects an interface")
|
||||
}
|
||||
toType := make(map[byte]reflect.Type, 0)
|
||||
toByte := make(map[reflect.Type]byte, 0)
|
||||
@@ -134,12 +131,10 @@ func RegisterInterface(o interface{}, ctypes ...ConcreteType) *TypeInfo {
|
||||
typeByte := ctype.Byte
|
||||
SetByteForType(typeByte, crt)
|
||||
if typeByte == 0x00 {
|
||||
// SANITY CHECK
|
||||
panic(Fmt("Byte of 0x00 is reserved for nil (%v)", ctype))
|
||||
PanicSanity(Fmt("Byte of 0x00 is reserved for nil (%v)", ctype))
|
||||
}
|
||||
if toType[typeByte] != nil {
|
||||
// SANITY CHECK
|
||||
panic(Fmt("Duplicate Byte for type %v and %v", ctype, toType[typeByte]))
|
||||
PanicSanity(Fmt("Duplicate Byte for type %v and %v", ctype, toType[typeByte]))
|
||||
}
|
||||
toType[typeByte] = crt
|
||||
toByte[crt] = typeByte
|
||||
@@ -398,8 +393,7 @@ func readReflectBinary(rv reflect.Value, rt reflect.Type, opts Options, r io.Rea
|
||||
rv.SetBool(num > 0)
|
||||
|
||||
default:
|
||||
// SANITY CHECK
|
||||
panic(Fmt("Unknown field type %v", rt.Kind()))
|
||||
PanicSanity(Fmt("Unknown field type %v", rt.Kind()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -567,8 +561,7 @@ func writeReflectBinary(rv reflect.Value, rt reflect.Type, opts Options, w io.Wr
|
||||
}
|
||||
|
||||
default:
|
||||
// SANITY CHECK
|
||||
panic(Fmt("Unknown field type %v", rt.Kind()))
|
||||
PanicSanity(Fmt("Unknown field type %v", rt.Kind()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -800,8 +793,7 @@ func readReflectJSON(rv reflect.Value, rt reflect.Type, o interface{}, err *erro
|
||||
rv.SetBool(bl)
|
||||
|
||||
default:
|
||||
// SANITY CHECK
|
||||
panic(Fmt("Unknown field type %v", rt.Kind()))
|
||||
PanicSanity(Fmt("Unknown field type %v", rt.Kind()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -949,8 +941,7 @@ func writeReflectJSON(rv reflect.Value, rt reflect.Type, w io.Writer, n *int64,
|
||||
WriteTo(jsonBytes, w, n, err)
|
||||
|
||||
default:
|
||||
// SANITY CHECK
|
||||
panic(Fmt("Unknown field type %v", rt.Kind()))
|
||||
PanicSanity(Fmt("Unknown field type %v", rt.Kind()))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,15 +4,15 @@ import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"github.com/tendermint/tendermint/Godeps/_workspace/src/code.google.com/p/go.crypto/ripemd160"
|
||||
)
|
||||
|
||||
// THESE PANICS ARE SANITY CHECKS
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
)
|
||||
|
||||
func BinaryBytes(o interface{}) []byte {
|
||||
w, n, err := new(bytes.Buffer), new(int64), new(error)
|
||||
WriteBinary(o, w, n, err)
|
||||
if *err != nil {
|
||||
panic(*err)
|
||||
PanicSanity(*err)
|
||||
}
|
||||
return w.Bytes()
|
||||
}
|
||||
@@ -21,7 +21,7 @@ func JSONBytes(o interface{}) []byte {
|
||||
w, n, err := new(bytes.Buffer), new(int64), new(error)
|
||||
WriteJSON(o, w, n, err)
|
||||
if *err != nil {
|
||||
panic(*err)
|
||||
PanicSanity(*err)
|
||||
}
|
||||
return w.Bytes()
|
||||
}
|
||||
@@ -45,7 +45,7 @@ func BinarySha256(o interface{}) []byte {
|
||||
hasher, n, err := sha256.New(), new(int64), new(error)
|
||||
WriteBinary(o, hasher, n, err)
|
||||
if *err != nil {
|
||||
panic(*err)
|
||||
PanicSanity(*err)
|
||||
}
|
||||
return hasher.Sum(nil)
|
||||
}
|
||||
@@ -55,7 +55,7 @@ func BinaryRipemd160(o interface{}) []byte {
|
||||
hasher, n, err := ripemd160.New(), new(int64), new(error)
|
||||
WriteBinary(o, hasher, n, err)
|
||||
if *err != nil {
|
||||
panic(*err)
|
||||
PanicSanity(*err)
|
||||
}
|
||||
return hasher.Sum(nil)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user