fn: add Collect+CollertErr function for iterators
What changed, and why it matters
This commit adds two small helper functions, Collect and CollectErr, to a utility package. They simply turn Go 1.23 iterators into ordinary slices. There is no user-facing behavior change, no bug fix, and no security-related change.
No security action required. Review the temporary replace directive for build/release hygiene before the next tagged fn/v2 release.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change introduces iter.Seq/iter.Seq2 helpers in fn/iter.go, bumps fn/go.mod to Go 1.23, and temporarily replaces the published fn/v2 module with the local ./fn copy in the root go.mod. The functions are pure utilities that append iterator values to a slice; CollectErr stops at the first error. No existing code paths are modified.
Changed components
fn/iter.gofn/go.modgo.modgo.sumInspect captured patch +33 / −3
diff --git a/fn/go.mod b/fn/go.mod
index 89dc280..abf0cdf 100644
--- a/fn/go.mod
+++ b/fn/go.mod
@@ -1,6 +1,6 @@
module github.com/lightningnetwork/lnd/fn/v2
-go 1.19
+go 1.23
require (
github.com/stretchr/testify v1.8.1
diff --git a/fn/iter.go b/fn/iter.go
new file mode 100644
index 0000000..e6636aa
--- /dev/null
+++ b/fn/iter.go
@@ -0,0 +1,29 @@
+package fn
+
+import "iter"
+
+// Collect drains all of the elements from the passed iterator, returning a
+// slice of the contents.
+func Collect[T any](seq iter.Seq[T]) []T {
+
+ var ret []T
+ for i := range seq {
+ ret = append(ret, i)
+ }
+
+ return ret
+}
+
+// CollectErr drains all of the elements from the passed iterator with error
+// handling, returning a slice of the contents and the first error encountered.
+func CollectErr[T any](seq iter.Seq2[T, error]) ([]T, error) {
+ var ret []T
+ for val, err := range seq {
+ if err != nil {
+ return ret, err
+ }
+ ret = append(ret, val)
+ }
+
+ return ret, nil
+}
diff --git a/go.mod b/go.mod
index d1064da..9898b4d 100644
--- a/go.mod
+++ b/go.mod
@@ -206,6 +206,9 @@ require (
// store have been included in a tagged sqldb version.
replace github.com/lightningnetwork/lnd/sqldb => ./sqldb
+// Replace fn package to use local version with iterator Collect function.
+replace github.com/lightningnetwork/lnd/fn/v2 => ./fn
+
// This replace is for https://github.com/advisories/GHSA-25xm-hr59-7c27
replace github.com/ulikunitz/xz => github.com/ulikunitz/xz v0.5.11
diff --git a/go.sum b/go.sum
index 189b81c..ae6f41d 100644
--- a/go.sum
+++ b/go.sum
@@ -374,8 +374,6 @@ github.com/lightningnetwork/lnd/cert v1.2.2 h1:71YK6hogeJtxSxw2teq3eGeuy4rHGKcFf
github.com/lightningnetwork/lnd/cert v1.2.2/go.mod h1:jQmFn/Ez4zhDgq2hnYSw8r35bqGVxViXhX6Cd7HXM6U=
github.com/lightningnetwork/lnd/clock v1.1.1 h1:OfR3/zcJd2RhH0RU+zX/77c0ZiOnIMsDIBjgjWdZgA0=
github.com/lightningnetwork/lnd/clock v1.1.1/go.mod h1:mGnAhPyjYZQJmebS7aevElXKTFDuO+uNFFfMXK1W8xQ=
-github.com/lightningnetwork/lnd/fn/v2 v2.0.8 h1:r2SLz7gZYQPVc3IZhU82M66guz3Zk2oY+Rlj9QN5S3g=
-github.com/lightningnetwork/lnd/fn/v2 v2.0.8/go.mod h1:TOzwrhjB/Azw1V7aa8t21ufcQmdsQOQMDtxVOQWNl8s=
github.com/lightningnetwork/lnd/healthcheck v1.2.6 h1:1sWhqr93GdkWy4+6U7JxBfcyZIE78MhIHTJZfPx7qqI=
github.com/lightningnetwork/lnd/healthcheck v1.2.6/go.mod h1:Mu02um4CWY/zdTOvFje7WJgJcHyX2zq/FG3MhOAiGaQ=
github.com/lightningnetwork/lnd/kvdb v1.4.16 h1:9BZgWdDfjmHRHLS97cz39bVuBAqMc4/p3HX1xtUdbDI=
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.