channledb/codec: add net.Addr and []net.Addr to codec

This commit is contained in:
Conner Fromknecht 2018-06-27 18:06:03 -07:00
parent 4cb7953f66
commit 75d7436f7a
No known key found for this signature in database
GPG Key ID: 39DE78FBE6ACB0EF

@ -4,6 +4,7 @@ import (
"encoding/binary"
"fmt"
"io"
"net"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lnwire"
@ -176,6 +177,22 @@ func WriteElement(w io.Writer, element interface{}) error {
return err
}
case net.Addr:
if err := serializeAddr(w, e); err != nil {
return err
}
case []net.Addr:
if err := WriteElement(w, uint32(len(e))); err != nil {
return err
}
for _, addr := range e {
if err := serializeAddr(w, addr); err != nil {
return err
}
}
default:
return UnknownElementType{"WriteElement", e}
}
@ -355,6 +372,28 @@ func ReadElement(r io.Reader, element interface{}) error {
return err
}
case *net.Addr:
addr, err := deserializeAddr(r)
if err != nil {
return err
}
*e = addr
case *[]net.Addr:
var numAddrs uint32
if err := ReadElement(r, &numAddrs); err != nil {
return err
}
*e = make([]net.Addr, numAddrs)
for i := uint32(0); i < numAddrs; i++ {
addr, err := deserializeAddr(r)
if err != nil {
return err
}
(*e)[i] = addr
}
default:
return UnknownElementType{"ReadElement", e}
}