tlv: simplify encoding functions

The typeless functions can directly call the typed functions after the
type check.
This commit is contained in:
Matt Morehouse
2023-11-13 11:30:17 -06:00
parent 9937b1d6de
commit e796922a82
2 changed files with 8 additions and 31 deletions

View File

@ -32,10 +32,7 @@ func SizeTUint16(v uint16) uint64 {
// be omitted. An error is returned if val is not a *uint16.
func ETUint16(w io.Writer, val interface{}, buf *[8]byte) error {
if t, ok := val.(*uint16); ok {
binary.BigEndian.PutUint16(buf[:2], *t)
numZeros := numLeadingZeroBytes16(*t)
_, err := w.Write(buf[numZeros:2])
return err
return ETUint16T(w, *t, buf)
}
return NewTypeForEncodingErr(val, "uint16")
}
@ -93,10 +90,7 @@ func SizeTUint32(v uint32) uint64 {
// be omitted. An error is returned if val is not a *uint32.
func ETUint32(w io.Writer, val interface{}, buf *[8]byte) error {
if t, ok := val.(*uint32); ok {
binary.BigEndian.PutUint32(buf[:4], *t)
numZeros := numLeadingZeroBytes32(*t)
_, err := w.Write(buf[numZeros:4])
return err
return ETUint32T(w, *t, buf)
}
return NewTypeForEncodingErr(val, "uint32")
}
@ -164,10 +158,7 @@ func SizeTUint64(v uint64) uint64 {
// be omitted. An error is returned if val is not a *uint64.
func ETUint64(w io.Writer, val interface{}, buf *[8]byte) error {
if t, ok := val.(*uint64); ok {
binary.BigEndian.PutUint64(buf[:], *t)
numZeros := numLeadingZeroBytes64(*t)
_, err := w.Write(buf[numZeros:])
return err
return ETUint64T(w, *t, buf)
}
return NewTypeForEncodingErr(val, "uint64")
}