shithub: hugo

Download patch

ref: e46ab29bd24caa9e2cfa51f24ba15037750850d6
parent: 10fef32de6e6cf20f8054e9833b1ad09fba6d5cb
author: cmal <[email protected]>
date: Fri Mar 16 20:13:23 EDT 2018

hugolib: Add Reset method to delete key from Scratch


--- a/docs/content/functions/scratch.md
+++ b/docs/content/functions/scratch.md
@@ -32,6 +32,7 @@
 * `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 @@
 {{ $.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 %}}
--- a/hugolib/scratch.go
+++ b/hugolib/scratch.go
@@ -73,6 +73,14 @@
 	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()
--- a/hugolib/scratch_test.go
+++ b/hugolib/scratch_test.go
@@ -87,6 +87,15 @@
 	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