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.
This commit is contained in:
Olaoluwa Osuntokun 2019-07-30 21:33:07 -07:00
parent ac6f56566e
commit aea529d9f6
No known key found for this signature in database
GPG Key ID: CE58F7F8E20FD9A2

@ -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 (