From fcf9302ae191059fc6e174de29d87b4c7a2737ea Mon Sep 17 00:00:00 2001 From: Alex Sergeyev Date: Sat, 20 Sep 2014 18:13:51 -0400 Subject: [PATCH] Changed logic to typeswitch and added comments. And also added my name to contrib list as promised before. --- CONTRIBUTORS | 1 + msg.go | 14 ++++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 9c7cdcf0..f77e8a89 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -6,3 +6,4 @@ Dusty Wilson Marek Majkowski Peter van Dijk Omri Bahumi +Alex Sergeyev diff --git a/msg.go b/msg.go index f1b0e8a2..b7cc6859 100644 --- a/msg.go +++ b/msg.go @@ -577,13 +577,16 @@ func packStructValue(val reflect.Value, msg []byte, off int, compression map[str default: return lenmsg, &Error{err: "bad kind packing"} case reflect.Interface: - if data, ok := fv.Interface().(PrivateRdata); ok { + // PrivateRR is the only RR implementation that has interface field. + // therefore it's expected that this interface would be PrivateRdata + switch data := fv.Interface().(type) { + case PrivateRdata: n, err := data.WriteByteSlice(msg[off:]) if err != nil { return lenmsg, err } off += n - } else { + default: return lenmsg, &Error{err: "bad kind interface packing"} } case reflect.Slice: @@ -880,13 +883,16 @@ func unpackStructValue(val reflect.Value, msg []byte, off int) (off1 int, err er default: return lenmsg, &Error{err: "bad kind unpacking"} case reflect.Interface: - if data, ok := fv.Interface().(PrivateRdata); ok { + // PrivateRR is the only RR implementation that has interface field. + // therefore it's expected that this interface would be PrivateRdata + switch data := fv.Interface().(type) { + case PrivateRdata: n, err := data.ParseByteSlice(msg[off:rdend]) if err != nil { return lenmsg, err } off += n - } else { + default: return lenmsg, &Error{err: "bad kind interface unpacking"} } case reflect.Slice: