Skip to content

Commit

Permalink
refactor!: delete MergeMap in favor of stdlib maps.Copy() (#55)
Browse files Browse the repository at this point in the history
## Description
delete `MergeMap`in favor of stdlib `maps.Copy()`

## Related Issue

Relates to #50
  • Loading branch information
Lucas Rodriguez authored Mar 29, 2024
1 parent db6790d commit 3b8a6cd
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 33 deletions.
21 changes: 3 additions & 18 deletions helpers/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package helpers

import (
"fmt"
"maps"
"math"
"reflect"
"regexp"
Expand Down Expand Up @@ -37,21 +38,6 @@ func Retry(fn func() error, retries int, delay time.Duration, logger func(format
return err
}

// MergeMap merges map m2 with m1 overwriting common values with m2's values.
func MergeMap[T any](m1, m2 map[string]T) (r map[string]T) {
r = map[string]T{}

for key, value := range m1 {
r[key] = value
}

for key, value := range m2 {
r[key] = value
}

return r
}

// TransformMapKeys takes a map and transforms its keys using the provided function.
func TransformMapKeys[T any](m map[string]T, transform func(string) string) (r map[string]T) {
r = map[string]T{}
Expand All @@ -65,10 +51,9 @@ func TransformMapKeys[T any](m map[string]T, transform func(string) string) (r m

// TransformAndMergeMap transforms keys in both maps then merges map m2 with m1 overwriting common values with m2's values.
func TransformAndMergeMap[T any](m1, m2 map[string]T, transform func(string) string) (r map[string]T) {
mt1 := TransformMapKeys(m1, transform)
r = TransformMapKeys(m1, transform)
mt2 := TransformMapKeys(m2, transform)
r = MergeMap(mt1, mt2)

maps.Copy(r, mt2)
return r
}

Expand Down
15 changes: 0 additions & 15 deletions helpers/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,6 @@ func (suite *TestMiscSuite) TestRetry() {
suite.Equal(3, logCount)
}

func (suite *TestMiscSuite) TestMergeMap() {
expected := map[string]interface{}{
"different": "value",
"hello": "it's me",
"unique": "value",
"nested": map[string]interface{}{
"values": "doggo",
"different": "value",
},
}

result := MergeMap(suite.map1, suite.map2)
suite.Equal(expected, result)
}

func (suite *TestMiscSuite) TestTransformMapKeys() {
expected := map[string]interface{}{
"HELLO": "world",
Expand Down

0 comments on commit 3b8a6cd

Please sign in to comment.