-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refac(git/config): Use an iterator for ListRegexp (#382)
TODO from when ListRegexp was originally implemented. Use an iter.Seq now that we're using Go 1.23 to build.
- Loading branch information
Showing
6 changed files
with
120 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Package sliceutil contains utility functions for working with slices. | ||
// It's an extension of the std slices package. | ||
package sliceutil | ||
|
||
import "iter" | ||
|
||
// CollectErr collects items from a sequence of items and errors, | ||
// stopping at the first error and returning it. | ||
func CollectErr[T any](ents iter.Seq2[T, error]) ([]T, error) { | ||
var items []T | ||
for item, err := range ents { | ||
if err != nil { | ||
return nil, err | ||
} | ||
items = append(items, item) | ||
} | ||
return items, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package sliceutil_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.abhg.dev/gs/internal/sliceutil" | ||
) | ||
|
||
func TestCollectErr(t *testing.T) { | ||
type pair struct { | ||
val int | ||
err error | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
give []pair | ||
|
||
want []int | ||
wantErr error | ||
}{ | ||
{ | ||
name: "Empty", | ||
give: nil, | ||
want: nil, | ||
}, | ||
{ | ||
name: "NoErrors", | ||
give: []pair{ | ||
{val: 1}, | ||
{val: 2}, | ||
{val: 3}, | ||
}, | ||
want: []int{1, 2, 3}, | ||
}, | ||
{ | ||
name: "Error", | ||
give: []pair{ | ||
{val: 1}, | ||
{err: assert.AnError}, | ||
{val: 3}, | ||
}, | ||
wantErr: assert.AnError, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := sliceutil.CollectErr(func(yield func(int, error) bool) { | ||
for _, p := range tt.give { | ||
if !yield(p.val, p.err) { | ||
break | ||
} | ||
} | ||
}) | ||
|
||
if tt.wantErr != nil { | ||
require.Error(t, err) | ||
assert.ErrorIs(t, err, tt.wantErr) | ||
} else { | ||
require.NoError(t, err) | ||
assert.Equal(t, tt.want, got) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters