diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2020-01-26 23:13:22 -0500 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2020-01-26 23:13:22 -0500 |
commit | 629cb407c10743b0033fdc648a3c876f244cf558 (patch) | |
tree | 016ff4bfde9e5f12c6b96a22ef2640b95c08bbaa /rrdformat/rrdbinary | |
parent | 72c4b2518a77ee9952a0fa25ae671f06d8d85570 (diff) |
UnmarshalBinary, EOF
Diffstat (limited to 'rrdformat/rrdbinary')
-rw-r--r-- | rrdformat/rrdbinary/types.go | 1 | ||||
-rw-r--r-- | rrdformat/rrdbinary/unmarshal.go | 21 |
2 files changed, 18 insertions, 4 deletions
diff --git a/rrdformat/rrdbinary/types.go b/rrdformat/rrdbinary/types.go index a036d5d..006af3c 100644 --- a/rrdformat/rrdbinary/types.go +++ b/rrdformat/rrdbinary/types.go @@ -27,6 +27,7 @@ type Uint uint64 // 4 or 8 bytes type Int int64 // 4 or 8 bytes type Unival uint64 // 8 bytes type Time int64 // 4 or 8 bytes, only has second-precision +type EOF struct{} // 0 bytes func (u Unival) AsUint64() uint64 { return uint64(u) } func (u Unival) AsFloat64() float64 { return math.Float64frombits(uint64(u)) } diff --git a/rrdformat/rrdbinary/unmarshal.go b/rrdformat/rrdbinary/unmarshal.go index 1cfee34..142ee9f 100644 --- a/rrdformat/rrdbinary/unmarshal.go +++ b/rrdformat/rrdbinary/unmarshal.go @@ -50,12 +50,14 @@ func (d *unmarshaler) unmarshal(v reflect.Value, tag string) error { return d.unmarshalUnival(v, tag) case reflect.TypeOf(Time(0)): return d.unmarshalTime(v, tag) + case reflect.TypeOf(EOF{}): + return d.unmarshalEOF(v, tag) default: switch v.Type().Kind() { case reflect.Struct: return d.unmarshalStruct(v, tag) - case reflect.Array: - return d.unmarshalArray(v, tag) + case reflect.Array, reflect.Slice: + return d.unmarshalList(v, tag) default: return typeErrorf("invalid type for rrdbinary.Unmarshal: %v", v.Type()) } @@ -83,8 +85,8 @@ func (d *unmarshaler) unmarshalStruct(v reflect.Value, tag string) error { return nil } -func (d *unmarshaler) unmarshalArray(v reflect.Value, tag string) error { - panicUnless(v.Kind() == reflect.Array) +func (d *unmarshaler) unmarshalList(v reflect.Value, tag string) error { + panicUnless(v.Kind() == reflect.Array || v.Kind() == reflect.Slice) panicUnless(v.CanSet()) for i := 0; i < v.Len(); i++ { @@ -314,3 +316,14 @@ func (d *unmarshaler) unmarshalTime(v reflect.Value, tag string) error { d.pos += padding + d.arch.TimeWidth return nil } + +func (d *unmarshaler) unmarshalEOF(v reflect.Value, tag string) error { + panicUnless(v.Type() == reflect.TypeOf(EOF{})) + + data := d.data[d.pos:] + if len(data) > 0 { + return d.binErrorf(16, "extra %d bytes of data after expected end-of-file", len(data)) + } + + return nil +} |