Skip to content

Commit afde716

Browse files
committed
tlv: add UnwrapOrErrV and UnwrapOrFailV
These two methods mirror the recently added `fn.Option` methods but allow us to unwrap on level deeper into the underlying value of the Record.
1 parent d69e111 commit afde716

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

tlv/record_type.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package tlv
22

33
import (
4+
"testing"
5+
46
"github.com/btcsuite/btcd/btcec/v2"
57
"github.com/lightningnetwork/lnd/fn"
68
"golang.org/x/exp/constraints"
@@ -115,6 +117,29 @@ func (t *OptionalRecordT[T, V]) WhenSomeV(f func(V)) {
115117
})
116118
}
117119

120+
// UnwrapOrFailV is used to extract a value from an option within a test
121+
// context. If the option is None, then the test fails. This gives the
122+
// underlying value of the record, rather then the record itself.
123+
func (o *OptionalRecordT[T, V]) UnwrapOrFailV(t *testing.T) V {
124+
inner := o.Option.UnwrapOrFail(t)
125+
126+
return inner.Val
127+
}
128+
129+
// UnwrapOrErr is used to extract a value from an option, if the option is
130+
// empty, then the specified error is returned directly. This gives the
131+
// underlying value of the record, instead of the record itself.
132+
func (o *OptionalRecordT[T, V]) UnwrapOrErrV(err error) (V, error) {
133+
var zero V
134+
135+
inner, err := o.Option.UnwrapOrErr(err)
136+
if err != nil {
137+
return zero, err
138+
}
139+
140+
return inner.Val, nil
141+
}
142+
118143
// SomeRecordT creates a new OptionalRecordT type from a given RecordT type.
119144
func SomeRecordT[T TlvType, V any](record RecordT[T, V]) OptionalRecordT[T, V] {
120145
return OptionalRecordT[T, V]{

0 commit comments

Comments
 (0)