Skip to content

Commit

Permalink
BREAKING CHANGE: fix mapz func (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
ginokent authored Aug 21, 2023
2 parents 8a8d036 + 2db4b4d commit f320c98
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 69 deletions.
2 changes: 1 addition & 1 deletion archive/zip/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func AddFileToZip(dstZipFile string, entryName string, src io.Reader, opts ...Ad
defer zr.Close()

// NOTE: 2. Create a buffer
buf := new(bytes.Buffer)
buf := bytes.NewBuffer(nil)
zw := zip.NewWriter(buf)

// NOTE: 3. Copy all entries from the existing zip file to the buffer
Expand Down
56 changes: 18 additions & 38 deletions maps/maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,59 +13,39 @@ var (
ErrValueTypeIsNotMatch = errors.New(`mapz: value type is not match`)
)

func SortMapStringKey[V any](m map[string]V) (sorted []struct {
Key string
Value V
},
) {
sorted = make([]struct {
Key string
Value V
}, len(m))

sortedKeys := make([]string, len(m))
i := 0
for key := range m {
sortedKeys[i] = key
i++
}
sort.Strings(sortedKeys)

for i, key := range sortedKeys {
sorted[i] = struct {
Key string
Value V
}{
Key: key,
Value: m[key],
}
}

return sorted
type ordered interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
~float32 | ~float64 |
~string
}

func SortMapIntKey[V any](m map[int]V) (sorted []struct {
Key int
Value V
func SortMapByKey[Key ordered, Value any](m map[Key]Value) (sorted []struct {
Key Key
Value Value
},
) {
sorted = make([]struct {
Key int
Value V
Key Key
Value Value
}, len(m))

sortedKeys := make([]int, len(m))
sortedKeys := make([]Key, len(m))
i := 0
for key := range m {
sortedKeys[i] = key
i++
}
sort.Ints(sortedKeys)
sort.SliceStable(sortedKeys, func(i, j int) bool {
return func(k1, k2 Key) bool {
return k1 < k2
}(sortedKeys[i], sortedKeys[j])
})

for i, key := range sortedKeys {
sorted[i] = struct {
Key int
Value V
Key Key
Value Value
}{
Key: key,
Value: m[key],
Expand Down
90 changes: 60 additions & 30 deletions maps/maps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,47 @@ import (
"testing"

mapz "github.com/kunitsucom/util.go/maps"
slicez "github.com/kunitsucom/util.go/slices"
)

func TestSortStringKey(t *testing.T) {
func TestSortMapByKey(t *testing.T) {
t.Parallel()

t.Run("success()", func(t *testing.T) {
t.Run("success(int)", func(t *testing.T) {
t.Parallel()

src := map[int]string{
2: "2",
4: "4",
10: "a",
12: "c",
11: "b",
5: "5",
3: "3",
1: "1",
}

expect := []struct {
Key int
Value string
}{
{Key: 1, Value: "1"},
{Key: 2, Value: "2"},
{Key: 3, Value: "3"},
{Key: 4, Value: "4"},
{Key: 5, Value: "5"},
{Key: 10, Value: "a"},
{Key: 11, Value: "b"},
{Key: 12, Value: "c"},
}

actual := mapz.SortMapByKey(src)

if !reflect.DeepEqual(expect, actual) {
t.Errorf("❌: expect(%v) != actual(%v)", expect, actual)
}
})

t.Run("success(string)", func(t *testing.T) {
t.Parallel()

src := map[string]int{
Expand Down Expand Up @@ -41,48 +75,44 @@ func TestSortStringKey(t *testing.T) {
{Key: "c", Value: 12},
}

actual := mapz.SortMapStringKey(src)
actual := mapz.SortMapByKey(src)

if !slicez.Equal(expect, actual) {
if !reflect.DeepEqual(expect, actual) {
t.Errorf("❌: expect(%v) != actual(%v)", expect, actual)
}
})
}

func TestSortIntKey(t *testing.T) {
t.Parallel()

t.Run("success()", func(t *testing.T) {
t.Run("success(float64)", func(t *testing.T) {
t.Parallel()

src := map[int]string{
2: "2",
4: "4",
10: "a",
12: "c",
11: "b",
5: "5",
3: "3",
1: "1",
src := map[float64]string{
2.111111: "2",
4.111111: "4",
10.111111: "a",
12.111111: "c",
11.111111: "b",
5.111111: "5",
3.111111: "3",
1.111111: "1",
}

expect := []struct {
Key int
Key float64
Value string
}{
{Key: 1, Value: "1"},
{Key: 2, Value: "2"},
{Key: 3, Value: "3"},
{Key: 4, Value: "4"},
{Key: 5, Value: "5"},
{Key: 10, Value: "a"},
{Key: 11, Value: "b"},
{Key: 12, Value: "c"},
{Key: 1.111111, Value: "1"},
{Key: 2.111111, Value: "2"},
{Key: 3.111111, Value: "3"},
{Key: 4.111111, Value: "4"},
{Key: 5.111111, Value: "5"},
{Key: 10.111111, Value: "a"},
{Key: 11.111111, Value: "b"},
{Key: 12.111111, Value: "c"},
}

actual := mapz.SortMapIntKey(src)
actual := mapz.SortMapByKey(src)

if !slicez.Equal(expect, actual) {
if !reflect.DeepEqual(expect, actual) {
t.Errorf("❌: expect(%v) != actual(%v)", expect, actual)
}
})
Expand Down

0 comments on commit f320c98

Please sign in to comment.