Skip to content

Commit

Permalink
chore(util): add DeepCopyIntToStringMap to util
Browse files Browse the repository at this point in the history
  • Loading branch information
luomingmeng committed Feb 10, 2025
1 parent ef280c4 commit a17e1d1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/util/general/deepcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,15 @@ func DeepCopyIntToInt64Map(origin map[int]int64) map[int]int64 {
}
return res
}

func DeepCopyIntToStringMap(origin map[int]string) map[int]string {
if origin == nil {
return nil
}

res := make(map[int]string, len(origin))
for key, val := range origin {
res[key] = val
}
return res
}
38 changes: 38 additions & 0 deletions pkg/util/general/deepcopy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package general
import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
)

func TestDeepCopyIntToIntMap(t *testing.T) {
Expand Down Expand Up @@ -58,3 +60,39 @@ func TestDeepCopyIntToIntMap(t *testing.T) {
})
}
}

func TestDeepCopyIntToStringMap(t *testing.T) {
t.Parallel()
type args struct {
origin map[int]string
}
tests := []struct {
name string
args args
want map[int]string
}{
{
name: "deep copy int to string map normally",
args: args{
origin: map[int]string{1: "1"},
},
want: map[int]string{
1: "1",
},
},
{
name: "deep copy nil int to int map",
args: args{
origin: nil,
},
want: nil,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
assert.Equalf(t, tt.want, DeepCopyIntToStringMap(tt.args.origin), "DeepCopyIntToStringMap(%v)", tt.args.origin)
})
}
}

0 comments on commit a17e1d1

Please sign in to comment.