From 36dbc9aaa96b8b6a84cb4fe4b00bd940e27195a6 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sat, 24 Sep 2016 12:06:41 -0400 Subject: [PATCH] typeError is nice --- ent/field.go | 5 ++++- ent/float.go | 18 ++++++++++-------- ent/type.go | 10 +++++++--- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/ent/field.go b/ent/field.go index 97d8657..dafc0be 100644 --- a/ent/field.go +++ b/ent/field.go @@ -14,7 +14,10 @@ func (f *field) fromProto(flat *dota.ProtoFlattenedSerializerFieldT, env *Env) e Debug.Printf("parse flat field: %s", prettyFlatField(flat, env)) t := parseType(flat, env) if t == nil { - return fmt.Errorf("unable to parse type: %s", prettyFlatField(flat, env)) + return fmt.Errorf("unable to parse type %s", prettyFlatField(flat, env)) + } + if err, ok := t.(error); ok { + return wrap(err, "unable to parse type %s", prettyFlatField(flat, env)) } f.tÿpe = t diff --git a/ent/float.go b/ent/float.go index b1e8a1e..59ae455 100644 --- a/ent/float.go +++ b/ent/float.go @@ -50,16 +50,18 @@ func qFloatType(flat *dota.ProtoFlattenedSerializerFieldT, env *Env) tÿpe { if t.flags > 0 { t.special = new(float32) + switch t.flags { + case f_min: + *t.special = t.low + case f_max: + *t.special = t.high + case f_center: + *t.special = t.low + (t.high+t.low)*0.5 + default: + return typeError("dunno how to handle qfloat flag value: %d", t.flags) + } } - switch { - case t.flags&f_min > 0: - *t.special = t.low - case t.flags&f_max > 0: - *t.special = t.high - case t.flags&f_center > 0: - *t.special = t.low + (t.high+t.low)*0.5 - } return t } diff --git a/ent/type.go b/ent/type.go index 23b45b7..81ddc4a 100644 --- a/ent/type.go +++ b/ent/type.go @@ -32,9 +32,13 @@ func parseType(flat *dota.ProtoFlattenedSerializerFieldT, env *Env) tÿpe { // a type error is both an error and a type. It represents a type that we were // unable to correctly parse. It can be interpreted as an error or as a type; // when interpreted as a type, it errors every time it tries to read a value. -type typeError string +func typeError(t string, args ...interface{}) tÿpe { + return error_t(fmt.Sprintf(t, args...)) +} + +type error_t string -func (e typeError) Error() string { return string(e) } -func (e typeError) read(r bit.Reader) (value, error) { +func (e error_t) Error() string { return string(e) } +func (e error_t) read(r bit.Reader) (value, error) { return nil, fmt.Errorf("type error: %s", string(e)) }