tlv: add utility funcs/methods to make tlv.OptionalRecord easier to use

We add a WhenSome that'll pass in the actual underlying value, and not
just the record.

We also add `SomeRecord` to make it easier to create TLV records w/
`Some` set.
This commit is contained in:
Olaoluwa Osuntokun committed 2024-01-02 18:51:41 -08:00
1 parent aec2f9bf08
commit 85046997c9
1 file changed
+15
+15
View File
@@ -102,6 +102,21 @@ func (t *OptionalRecordT[T, V]) TlvType() Type {
return zeroRecord.TlvType()
}
// WhenSomeV executes the given function if the optional record is present.
// This operates on the inner most type, V, which is the value of the record.
func (t *OptionalRecordT[T, V]) WhenSomeV(f func(V)) {
t.Option.WhenSome(func(r RecordT[T, V]) {
f(r.Val)
})
}
// SomeRecordT creates a new OptionalRecordT type from a given RecordT type.
func SomeRecordT[T TlvType, V any](record RecordT[T, V]) OptionalRecordT[T, V] {
return OptionalRecordT[T, V]{
Option: fn.Some(record),
}
}
// ZeroRecordT returns a zero value of the RecordT type.
func ZeroRecordT[T TlvType, V any]() RecordT[T, V] {
var v V