panic wrapper functions

This commit is contained in:
Ethan Buchman
2015-07-19 23:42:52 +00:00
parent 5983088a32
commit 8e50bf15de
45 changed files with 229 additions and 275 deletions

View File

@ -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()))
}
}