Skip to content

Commit 369ac83

Browse files
authored
Fix bug introduced by mapstr optimization. (#65)
* Fix bug introduced by mapstr optimization. The bug was introduced by #64. Add a test to catch the bug that slipped through. * Update changelog for 0.2.7
1 parent 5bdd25e commit 369ac83

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1414

1515
### Fixed
1616

17+
## [0.2.7]
18+
19+
### Changed
20+
21+
- Fix bug introduced by mapstr optimization: #65
22+
1723
## [0.2.6]
1824

1925
### Added

mapstr/mapstr.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,9 @@ func (m M) Clone() M {
157157
for k := range m {
158158
if innerMap, ok := (m[k]).(M); ok {
159159
result[k] = innerMap.Clone()
160+
} else {
161+
result[k] = m[k]
160162
}
161-
result[k] = m[k]
162163
}
163164

164165
return result

mapstr/mapstr_test.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ func TestMapStrGetValue(t *testing.T) {
343343
func TestClone(t *testing.T) {
344344
assert := assert.New(t)
345345

346-
m := M{
346+
original := M{
347347
"c1": 1,
348348
"c2": 2,
349349
"c3": M{
@@ -352,8 +352,36 @@ func TestClone(t *testing.T) {
352352
},
353353
}
354354

355-
c := m.Clone()
356-
assert.Equal(M{"c31": 1, "c32": 2}, c["c3"])
355+
// Clone the original mapstr and then increment every value in it. Ensures the test will fail if
356+
// the cloned mapstr kept a reference to any part of the original.
357+
cloned := original.Clone()
358+
incrementMapstrValues(original)
359+
360+
// Ensure that the cloned copy is as expected and no longer matches the original mapstr.
361+
assert.Equal(
362+
M{
363+
"c1": 1,
364+
"c2": 2,
365+
"c3": M{
366+
"c31": 1,
367+
"c32": 2,
368+
},
369+
},
370+
cloned,
371+
)
372+
assert.NotEqual(cloned, original)
373+
}
374+
375+
func incrementMapstrValues(m M) {
376+
for k := range m {
377+
switch v := m[k].(type) {
378+
case int:
379+
m[k] = v + 1
380+
case M:
381+
incrementMapstrValues(m[k].(M))
382+
}
383+
384+
}
357385
}
358386

359387
func BenchmarkClone(b *testing.B) {

0 commit comments

Comments
 (0)