Skip to content

Commit b234658

Browse files
authored
Merge pull request #8501 from Roasbeef/tlv-fn-ergonomics
tlv: add UnwrapOrErrV+UnwrapOrFailV+Zero to OptionalRecord
2 parents 6d8f40c + 3dd01cd commit b234658

File tree

5 files changed

+42
-9
lines changed

5 files changed

+42
-9
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ require (
3939
github.com/lightningnetwork/lightning-onion v1.2.1-0.20230823005744-06182b1d7d2f
4040
github.com/lightningnetwork/lnd/cert v1.2.2
4141
github.com/lightningnetwork/lnd/clock v1.1.1
42-
github.com/lightningnetwork/lnd/fn v1.0.2
42+
github.com/lightningnetwork/lnd/fn v1.0.4
4343
github.com/lightningnetwork/lnd/healthcheck v1.2.3
4444
github.com/lightningnetwork/lnd/kvdb v1.4.4
4545
github.com/lightningnetwork/lnd/queue v1.1.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,8 @@ github.com/lightningnetwork/lnd/cert v1.2.2 h1:71YK6hogeJtxSxw2teq3eGeuy4rHGKcFf
442442
github.com/lightningnetwork/lnd/cert v1.2.2/go.mod h1:jQmFn/Ez4zhDgq2hnYSw8r35bqGVxViXhX6Cd7HXM6U=
443443
github.com/lightningnetwork/lnd/clock v1.1.1 h1:OfR3/zcJd2RhH0RU+zX/77c0ZiOnIMsDIBjgjWdZgA0=
444444
github.com/lightningnetwork/lnd/clock v1.1.1/go.mod h1:mGnAhPyjYZQJmebS7aevElXKTFDuO+uNFFfMXK1W8xQ=
445-
github.com/lightningnetwork/lnd/fn v1.0.2 h1:6u+DHMvpHj09KH2Uw39fsbjydq9JvG23Rc99i+mhI1A=
446-
github.com/lightningnetwork/lnd/fn v1.0.2/go.mod h1:K9gbvdl5z4XmRcqWUVqvvVcuRKtmq9BNQ+cWYlk+vjw=
445+
github.com/lightningnetwork/lnd/fn v1.0.4 h1:n4iGRRoS+XHqNbOrsXIvweps/QfWk+moO7FiYPPFI8k=
446+
github.com/lightningnetwork/lnd/fn v1.0.4/go.mod h1:K9gbvdl5z4XmRcqWUVqvvVcuRKtmq9BNQ+cWYlk+vjw=
447447
github.com/lightningnetwork/lnd/healthcheck v1.2.3 h1:oqhOOy8WmIEa6RBkYKC0mmYZkhl8T2kGD97n9jpML8o=
448448
github.com/lightningnetwork/lnd/healthcheck v1.2.3/go.mod h1:eDxH3dEwV9DeBW/6inrmlVh1qBOFV0AI14EEPnGt9gc=
449449
github.com/lightningnetwork/lnd/kvdb v1.4.4 h1:bCv63rVCvzqj1BkagN/EWTov6NDDgYEG/t0z2HepRMk=

tlv/go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ require (
55
github.com/btcsuite/btcd/btcec/v2 v2.1.3
66
github.com/davecgh/go-spew v1.1.1
77
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1
8-
github.com/lightningnetwork/lnd/fn v1.0.0
8+
github.com/lightningnetwork/lnd/fn v1.0.4
99
github.com/stretchr/testify v1.8.2
10-
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
10+
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b
1111
)
1212

1313
require (
1414
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 // indirect
1515
github.com/kr/pretty v0.3.0 // indirect
16+
github.com/lightninglabs/neutrino/cache v1.1.2 // indirect
1617
github.com/pmezard/go-difflib v1.0.0 // indirect
1718
github.com/rogpeppe/go-internal v1.9.0 // indirect
1819
golang.org/x/crypto v0.7.0 // indirect

tlv/go.sum

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
1919
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
2020
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
2121
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
22-
github.com/lightningnetwork/lnd/fn v1.0.0 h1:I5VG9AD63mOQ89RMQEu7HRI1r68wn8yz539LoylUIKM=
23-
github.com/lightningnetwork/lnd/fn v1.0.0/go.mod h1:XV+0vBXSnh3aUjskJUv58TOpsveiXQ+ac8rEnXZDGFc=
22+
github.com/lightninglabs/neutrino/cache v1.1.2 h1:C9DY/DAPaPxbFC+xNNEI/z1SJY9GS3shmlu5hIQ798g=
23+
github.com/lightninglabs/neutrino/cache v1.1.2/go.mod h1:XJNcgdOw1LQnanGjw8Vj44CvguYA25IMKjWFZczwZuo=
24+
github.com/lightningnetwork/lnd/fn v1.0.4 h1:n4iGRRoS+XHqNbOrsXIvweps/QfWk+moO7FiYPPFI8k=
25+
github.com/lightningnetwork/lnd/fn v1.0.4/go.mod h1:K9gbvdl5z4XmRcqWUVqvvVcuRKtmq9BNQ+cWYlk+vjw=
2426
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2527
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2628
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
@@ -35,8 +37,8 @@ github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ
3537
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
3638
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
3739
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
38-
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
39-
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
40+
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b h1:kLiC65FbiHWFAOu+lxwNPujcsl8VYyTYYEZnsOO1WK4=
41+
golang.org/x/exp v0.0.0-20231226003508-02704c960a9b/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
4042
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
4143
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4244
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

tlv/record_type.go

Lines changed: 30 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,34 @@ 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+
143+
// Zero returns a zero value of the record type.
144+
func (t *OptionalRecordT[T, V]) Zero() RecordT[T, V] {
145+
return ZeroRecordT[T, V]()
146+
}
147+
118148
// SomeRecordT creates a new OptionalRecordT type from a given RecordT type.
119149
func SomeRecordT[T TlvType, V any](record RecordT[T, V]) OptionalRecordT[T, V] {
120150
return OptionalRecordT[T, V]{

0 commit comments

Comments
 (0)