File tree Expand file tree Collapse file tree 3 files changed +39
-4
lines changed Expand file tree Collapse file tree 3 files changed +39
-4
lines changed Original file line number Diff line number Diff 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  
Original file line number Diff line number Diff 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 
Original file line number Diff line number Diff line change @@ -343,7 +343,7 @@ func TestMapStrGetValue(t *testing.T) {
343343func  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
359387func  BenchmarkClone (b  * testing.B ) {
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments