From 015875e455f1a71b0dcf1319ec08316511f78d84 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Mon, 4 Aug 2025 15:54:59 -0700 Subject: [PATCH] fn: add Collect+CollertErr function for iterators In this commit, we introduce a new utility function `Collect` to the fn package. This function drains all elements from an iterator and returns them as a slice. This is particularly useful when transitioning from iterator-based APIs to code that expects slices, allowing for gradual migration to the new iterator patterns. The fn module's go.mod is also updated to require Go 1.23, which is necessary for the built-in iter.Seq type support. The replace directive will be removed once the fn package changes are merged and a new version is tagged. --- fn/go.mod | 2 +- fn/iter.go | 29 +++++++++++++++++++++++++++++ go.mod | 3 +++ go.sum | 2 -- 4 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 fn/iter.go diff --git a/fn/go.mod b/fn/go.mod index 89dc280df..abf0cdf4c 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 000000000..e6636aadb --- /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 d1064da1d..9898b4d7f 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 189b81c6e..ae6f41da1 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=