Skip to content

Commit 6a54aaa

Browse files
chore: use slices.Contains to simplify code (#24499)
Signed-off-by: closeobserve <[email protected]> Co-authored-by: Alex | Interchain Labs <[email protected]>
1 parent 3b90ba4 commit 6a54aaa

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

store/snapshots/manager.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io"
99
"math"
1010
"os"
11+
"slices"
1112
"sort"
1213
"sync"
1314

@@ -493,12 +494,7 @@ func (m *Manager) sortedExtensionNames() []string {
493494

494495
// IsFormatSupported returns if the snapshotter supports restoration from given format.
495496
func IsFormatSupported(snapshotter types.ExtensionSnapshotter, format uint32) bool {
496-
for _, i := range snapshotter.SupportedFormats() {
497-
if i == format {
498-
return true
499-
}
500-
}
501-
return false
497+
return slices.Contains(snapshotter.SupportedFormats(), format)
502498
}
503499

504500
// SnapshotIfApplicable takes a snapshot of the current state if we are on a snapshot height.

store/snapshots/manager_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,30 @@ func TestManager_TakeError(t *testing.T) {
256256
_, err = manager.Create(1)
257257
require.Error(t, err)
258258
}
259+
260+
type mockExtensionSnapshotter struct {
261+
types.ExtensionSnapshotter
262+
formats []uint32
263+
}
264+
265+
func (m *mockExtensionSnapshotter) SnapshotName() string { return "mock" }
266+
func (m *mockExtensionSnapshotter) SupportedFormats() []uint32 { return m.formats }
267+
268+
func TestIsFormatSupported(t *testing.T) {
269+
mockExtension := &mockExtensionSnapshotter{
270+
formats: []uint32{1, 2},
271+
}
272+
273+
t.Run("supported format", func(t *testing.T) {
274+
require.True(t, snapshots.IsFormatSupported(mockExtension, 1))
275+
})
276+
277+
t.Run("unsupported format", func(t *testing.T) {
278+
require.False(t, snapshots.IsFormatSupported(mockExtension, 3))
279+
})
280+
281+
t.Run("empty supported formats", func(t *testing.T) {
282+
emptyExtension := &mockExtensionSnapshotter{formats: []uint32{}}
283+
require.False(t, snapshots.IsFormatSupported(emptyExtension, 1))
284+
})
285+
}

0 commit comments

Comments
 (0)