typeError is nice

types
Jordan Orelli 8 years ago
parent c0c9f22fba
commit 36dbc9aaa9

@ -14,7 +14,10 @@ func (f *field) fromProto(flat *dota.ProtoFlattenedSerializerFieldT, env *Env) e
Debug.Printf("parse flat field: %s", prettyFlatField(flat, env)) Debug.Printf("parse flat field: %s", prettyFlatField(flat, env))
t := parseType(flat, env) t := parseType(flat, env)
if t == nil { 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 f.tÿpe = t

@ -50,16 +50,18 @@ func qFloatType(flat *dota.ProtoFlattenedSerializerFieldT, env *Env) tÿpe {
if t.flags > 0 { if t.flags > 0 {
t.special = new(float32) t.special = new(float32)
} switch t.flags {
case f_min:
switch {
case t.flags&f_min > 0:
*t.special = t.low *t.special = t.low
case t.flags&f_max > 0: case f_max:
*t.special = t.high *t.special = t.high
case t.flags&f_center > 0: case f_center:
*t.special = t.low + (t.high+t.low)*0.5 *t.special = t.low + (t.high+t.low)*0.5
default:
return typeError("dunno how to handle qfloat flag value: %d", t.flags)
} }
}
return t return t
} }

@ -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 // 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; // 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. // 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 error_t) Error() string { return string(e) }
func (e typeError) read(r bit.Reader) (value, error) { func (e error_t) read(r bit.Reader) (value, error) {
return nil, fmt.Errorf("type error: %s", string(e)) return nil, fmt.Errorf("type error: %s", string(e))
} }

Loading…
Cancel
Save