chore: use slices.Contains to simplify code (#24499)

Signed-off-by: closeobserve <pingcap@yahoo.com>
Co-authored-by: Alex | Interchain Labs <alex@interchainlabs.io>
This commit is contained in:
closeobserve 2025-04-23 16:54:37 +02:00 committed by GitHub
parent 3b90ba4ef3
commit 6a54aaa90e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 6 deletions

View File

@ -8,6 +8,7 @@ import (
"io"
"math"
"os"
"slices"
"sort"
"sync"
@ -493,12 +494,7 @@ func (m *Manager) sortedExtensionNames() []string {
// IsFormatSupported returns if the snapshotter supports restoration from given format.
func IsFormatSupported(snapshotter types.ExtensionSnapshotter, format uint32) bool {
for _, i := range snapshotter.SupportedFormats() {
if i == format {
return true
}
}
return false
return slices.Contains(snapshotter.SupportedFormats(), format)
}
// SnapshotIfApplicable takes a snapshot of the current state if we are on a snapshot height.

View File

@ -256,3 +256,30 @@ func TestManager_TakeError(t *testing.T) {
_, err = manager.Create(1)
require.Error(t, err)
}
type mockExtensionSnapshotter struct {
types.ExtensionSnapshotter
formats []uint32
}
func (m *mockExtensionSnapshotter) SnapshotName() string { return "mock" }
func (m *mockExtensionSnapshotter) SupportedFormats() []uint32 { return m.formats }
func TestIsFormatSupported(t *testing.T) {
mockExtension := &mockExtensionSnapshotter{
formats: []uint32{1, 2},
}
t.Run("supported format", func(t *testing.T) {
require.True(t, snapshots.IsFormatSupported(mockExtension, 1))
})
t.Run("unsupported format", func(t *testing.T) {
require.False(t, snapshots.IsFormatSupported(mockExtension, 3))
})
t.Run("empty supported formats", func(t *testing.T) {
emptyExtension := &mockExtensionSnapshotter{formats: []uint32{}}
require.False(t, snapshots.IsFormatSupported(emptyExtension, 1))
})
}