From aea529d9f6d1a0ffbd34a7a36d1fc7db41dc8fbc Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Tue, 30 Jul 2019 21:33:07 -0700 Subject: [PATCH] tlv: add new Type() and Encode() methods to Record In this commit, we add two new method so the `Record` struct: Type() and Encode(). These are useful when a caller is handling a record and may not know its underlying type and may need to encode a record in isolation. --- tlv/record.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tlv/record.go b/tlv/record.go index ae21c050..923d70fa 100644 --- a/tlv/record.go +++ b/tlv/record.go @@ -57,6 +57,20 @@ func (f *Record) Size() uint64 { return f.sizeFunc() } +// Type returns the type of the underlying TLV record. +func (f *Record) Type() Type { + return f.typ +} + +// Encode writes out the TLV record to the passed writer. This is useful when a +// caller wants to obtain the raw encoding of a *single* TLV record, outside +// the context of the Stream struct. +func (f *Record) Encode(w io.Writer) error { + var b [8]byte + + return f.encoder(w, f.value, &b) +} + // MakePrimitiveRecord creates a record for common types. func MakePrimitiveRecord(typ Type, val interface{}) Record { var (