mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-09-15 02:55:37 +02:00
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:
@@ -102,6 +102,21 @@ func (t *OptionalRecordT[T, V]) TlvType() Type {
|
|||||||
return zeroRecord.TlvType()
|
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.
|
// ZeroRecordT returns a zero value of the RecordT type.
|
||||||
func ZeroRecordT[T TlvType, V any]() RecordT[T, V] {
|
func ZeroRecordT[T TlvType, V any]() RecordT[T, V] {
|
||||||
var v V
|
var v V
|
||||||
|
Reference in New Issue
Block a user