diff --git a/mapstr/mapstr.go b/mapstr/mapstr.go index af272765..ede3f37b 100644 --- a/mapstr/mapstr.go +++ b/mapstr/mapstr.go @@ -152,13 +152,13 @@ func (m M) CopyFieldsTo(to M, key string) error { // Clone returns a copy of the M. It recursively makes copies of inner // maps. func (m M) Clone() M { - result := M{} + result := make(M, len(m)) - for k, v := range m { - if innerMap, ok := tryToMapStr(v); ok { - v = innerMap.Clone() + for k := range m { + if innerMap, ok := (m[k]).(M); ok { + result[k] = innerMap.Clone() } - result[k] = v + result[k] = m[k] } return result diff --git a/mapstr/mapstr_test.go b/mapstr/mapstr_test.go index 35eeb45c..27b94bb9 100644 --- a/mapstr/mapstr_test.go +++ b/mapstr/mapstr_test.go @@ -356,6 +356,37 @@ func TestClone(t *testing.T) { assert.Equal(M{"c31": 1, "c32": 2}, c["c3"]) } +func BenchmarkClone(b *testing.B) { + assert := assert.New(b) + + m := M{ + "c1": 1, + "c2": 2, + "c3": M{ + "c31": 1, + "c32": 2, + "c33": 3, + "c34": 4, + "c35": 5, + "c36": 6, + "c37": 7, + "c38": 8, + "c39": 9, + }, + "c4": 4, + "c5": 5, + "c6": 6, + "c7": 7, + "c8": 8, + "c9": 9, + } + + for i := 0; i < b.N; i++ { + c := m.Clone() + assert.Equal(m, c) + } +} + func TestString(t *testing.T) { type io struct { Input M