mirror of
				https://github.com/gohugoio/hugo.git
				synced 2024-05-11 05:54:58 +00:00 
			
		
		
		
	hugolib: Add Reset method to delete key from Scratch
This commit is contained in:
		
				
					committed by
					
						 Bjørn Erik Pedersen
						Bjørn Erik Pedersen
					
				
			
			
				
	
			
			
			
						parent
						
							10fef32de6
						
					
				
				
					commit
					e46ab29bd2
				
			| @@ -32,6 +32,7 @@ See [this Go issue](https://github.com/golang/go/issues/10608) for the main moti | ||||
| * `Get` returns the `value` for the `key` given. | ||||
| * `SetInMap` takes a `key`, `mapKey` and `value` | ||||
| * `GetSortedMapValues` returns array of values from `key` sorted by `mapKey` | ||||
| * `Delete` takes a `key` to remove | ||||
|  | ||||
| `Set` and `SetInMap` can store values of any type. | ||||
|  | ||||
| @@ -69,6 +70,11 @@ The usage is best illustrated with some samples: | ||||
| {{ $.Scratch.SetInMap "a3" "c" "CC" }} | ||||
| {{ $.Scratch.SetInMap "a3" "b" "BB" }} | ||||
| {{ $.Scratch.GetSortedMapValues "a3" }} {{/* => []interface {}{"AA", "BB", "CC"} */}} | ||||
|  | ||||
| {{ $.Scratch.Add "a" 1 }} | ||||
| {{ $.Scratch.Delete "a" }} | ||||
| {{ $.Scratch.Add "a" 2 }} | ||||
| {{ $.Scratch.Get "a" }} {{/* => 2 */}} | ||||
| ``` | ||||
|  | ||||
| {{% note %}} | ||||
|   | ||||
| @@ -73,6 +73,14 @@ func (c *Scratch) Set(key string, value interface{}) string { | ||||
| 	return "" | ||||
| } | ||||
|  | ||||
| // Reset deletes the given key | ||||
| func (c *Scratch) Delete(key string) string { | ||||
| 	c.mu.Lock() | ||||
| 	delete(c.values, key) | ||||
| 	c.mu.Unlock() | ||||
| 	return "" | ||||
| } | ||||
|  | ||||
| // Get returns a value previously set by Add or Set | ||||
| func (c *Scratch) Get(key string) interface{} { | ||||
| 	c.mu.RLock() | ||||
|   | ||||
| @@ -87,6 +87,15 @@ func TestScratchSet(t *testing.T) { | ||||
| 	assert.Equal(t, "val", scratch.Get("key")) | ||||
| } | ||||
|  | ||||
| func TestScratchDelete(t *testing.T) { | ||||
| 	t.Parallel() | ||||
| 	scratch := newScratch() | ||||
| 	scratch.Set("key", "val") | ||||
| 	scratch.Delete("key") | ||||
| 	scratch.Add("key", "Lucy Parsons") | ||||
| 	assert.Equal(t, "Lucy Parsons", scratch.Get("key")) | ||||
| } | ||||
|  | ||||
| // Issue #2005 | ||||
| func TestScratchInParallel(t *testing.T) { | ||||
| 	var wg sync.WaitGroup | ||||
|   | ||||
		Reference in New Issue
	
	Block a user