Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(auto-salvage): v2 support #70

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/shirou/gopsutil/v3 v3.24.5
github.com/sirupsen/logrus v1.9.3
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc
golang.org/x/sys v0.26.0
google.golang.org/grpc v1.67.1
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
Expand Down
20 changes: 20 additions & 0 deletions utils/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"path/filepath"
"reflect"
"runtime"
"sort"
"strconv"
"strings"

"github.com/google/uuid"
"github.com/pkg/errors"
"golang.org/x/exp/constraints"

"github.com/longhorn/go-common-libs/types"
)
Expand Down Expand Up @@ -119,3 +121,21 @@ func ConvertTypeToString[T any](value T) string {
return fmt.Sprintf("Unsupported type: %v", v.Kind())
}
}

// SortKeys sorts the keys of a map in ascending order.
func SortKeys[K constraints.Ordered, V any](mapObj map[K]V) ([]K, error) {
if mapObj == nil {
return nil, fmt.Errorf("input object cannot be nil")
}

keys := make([]K, 0, len(mapObj))
for key := range mapObj {
keys = append(keys, key)
}

sort.Slice(keys, func(i, j int) bool {
return keys[i] < keys[j]
})

return keys, nil
}
63 changes: 63 additions & 0 deletions utils/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"strings"

"golang.org/x/exp/constraints"
. "gopkg.in/check.v1"

"github.com/longhorn/go-common-libs/test"
Expand Down Expand Up @@ -243,3 +244,65 @@ func (s *TestSuite) TestConvertTypeToString(c *C) {
c.Assert(result, Equals, testCase.expected, Commentf(test.ErrResultFmt, testName))
}
}

func (s *TestSuite) TestSortKeys(c *C) {
// Test cases for base cases
baseTestCases := map[string]testCaseSortKeys[string, any]{
"SortKeys(...): nil map": {
inputMap: nil,
expectError: true,
},
"SortKeys(...): empty map": {
inputMap: map[string]any{},
expected: []string{},
},
}

// Test cases for string keys
stringTestCases := map[string]testCaseSortKeys[string, any]{
"SortKeys(...): string": {
inputMap: map[string]any{
"b": "",
"c": "",
"a": "",
},
expected: []string{"a", "b", "c"},
},
}

// Test cases for uint64 keys
uint64TestCases := map[string]testCaseSortKeys[uint64, any]{
"SortKeys(...): uint64": {
inputMap: map[uint64]any{
2: "",
1: "",
3: "",
},
expected: []uint64{1, 2, 3},
},
}

runTestSortKeys(c, baseTestCases)
runTestSortKeys(c, stringTestCases)
runTestSortKeys(c, uint64TestCases)
}

type testCaseSortKeys[K constraints.Ordered, V any] struct {
inputMap map[K]V
expected []K
expectError bool
}

func runTestSortKeys[K constraints.Ordered, V any](c *C, testCases map[string]testCaseSortKeys[K, V]) {
for testName, tc := range testCases {
c.Logf("Testing utils.%v", testName)
result, err := SortKeys(tc.inputMap)

if tc.expectError {
c.Assert(err, NotNil, Commentf("Expected error in %v", testName))
continue
}
c.Assert(err, IsNil, Commentf("Unexpected error in %v", testName))
c.Assert(result, DeepEquals, tc.expected, Commentf("Unexpected result in %v", testName))
}
}
27 changes: 27 additions & 0 deletions vendor/golang.org/x/exp/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/golang.org/x/exp/PATENTS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions vendor/golang.org/x/exp/constraints/constraints.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ github.com/x448/float16
# github.com/yusufpapurcu/wmi v1.2.4
## explicit; go 1.16
github.com/yusufpapurcu/wmi
# golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc
## explicit; go 1.20
golang.org/x/exp/constraints
# golang.org/x/net v0.28.0
## explicit; go 1.18
golang.org/x/net/http/httpguts
Expand Down