summaryrefslogtreecommitdiff
path: root/rrdformat/rrdbinary
diff options
context:
space:
mode:
Diffstat (limited to 'rrdformat/rrdbinary')
-rw-r--r--rrdformat/rrdbinary/decode.go (renamed from rrdformat/rrdbinary/unmarshal.go)38
1 files changed, 26 insertions, 12 deletions
diff --git a/rrdformat/rrdbinary/unmarshal.go b/rrdformat/rrdbinary/decode.go
index 00f1222..f3ae80e 100644
--- a/rrdformat/rrdbinary/unmarshal.go
+++ b/rrdformat/rrdbinary/decode.go
@@ -15,21 +15,27 @@ type Decoder struct {
data []byte
}
+func NewDecoder(arch Architecture, data []byte) *Decoder {
+ return &Decoder{
+ arch: arch,
+ pos: 0,
+ data: data,
+ }
+}
+
type unmarshaler interface {
unmarshalRRD(d *Decoder, tag string) error
}
func Unmarshal(arch Architecture, data []byte, ptr interface{}) error {
- ptrValue := reflect.ValueOf(ptr)
- if ptrValue.Kind() != reflect.Ptr {
- return typeErrorf("ptr is a %v, not a pointer", ptrValue.Kind())
+ decoder := NewDecoder(arch, data)
+ if err := decoder.Decode(ptr, ""); err != nil {
+ return err
}
- decoder := &Decoder{
- arch: arch,
- pos: 0,
- data: data,
+ if err := decoder.Decode(&EOF{}, ""); err != nil {
+ return err
}
- return decoder.Decode(ptrValue, "")
+ return nil
}
func (d *Decoder) binError(ctxLen int, msg string) error {
@@ -40,7 +46,15 @@ func (d *Decoder) binErrorf(ctxLen int, format string, a ...interface{}) error {
return d.binError(ctxLen, fmt.Sprintf(format, a...))
}
-func (d *Decoder) Decode(v reflect.Value, tag string) error {
+func (d *Decoder) Decode(ptr interface{}, tag string) error {
+ ptrValue := reflect.ValueOf(ptr)
+ if ptrValue.Kind() != reflect.Ptr {
+ return typeErrorf("ptr is a %v, not a pointer", ptrValue.Kind())
+ }
+ return d.decode(ptrValue, tag)
+}
+
+func (d *Decoder) decode(v reflect.Value, tag string) error {
if v.CanInterface() {
if u, ok := v.Interface().(unmarshaler); ok {
return u.unmarshalRRD(d, tag)
@@ -52,7 +66,7 @@ func (d *Decoder) Decode(v reflect.Value, tag string) error {
case reflect.Array, reflect.Slice:
return d.decodeList(v, tag)
default:
- return typeErrorf("invalid type for rrdbinary.Decoder.Decode: %v", v.Type())
+ return typeErrorf("invalid type for rrdbinary.Decoder: %v", v.Type())
}
}
@@ -70,7 +84,7 @@ func (d *Decoder) decodeStruct(v reflect.Value, tag string) error {
if tag == "-" {
continue
}
- if err := d.Decode(v.Field(i), tag); err != nil {
+ if err := d.decode(v.Field(i), tag); err != nil {
return fmt.Errorf("field %s: %w", fieldInfo.Name, err)
}
}
@@ -82,7 +96,7 @@ func (d *Decoder) decodeList(v reflect.Value, tag string) error {
panicUnless(v.CanSet())
for i := 0; i < v.Len(); i++ {
- if err := d.Decode(v.Index(i), tag); err != nil {
+ if err := d.decode(v.Index(i), tag); err != nil {
return fmt.Errorf("index %d: %w", i, err)
}
}