Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authormeehawk <80167324+meehawk@users.noreply.github.com>2021-05-17 16:45:33 +0300
committerGitHub <noreply@github.com>2021-05-17 16:45:33 +0300
commitabbc99d4c60b102e2779e4362ceb433095719384 (patch)
treef567d55d5ba900a488d777cb5fb979f414d2e061 /common
parent76c95f55a5d18290baa7f23667161d4af9fb9b53 (diff)
common/maps: Add Scratch.DeleteInMap
Add Scratch.DeleteInMap method. This method works similar to Scratch.SetInMap. It takes in two string parameters, key and mapKey and deletes the value mapped to mapKey in key Closes #8504
Diffstat (limited to 'common')
-rw-r--r--common/maps/scratch.go11
-rw-r--r--common/maps/scratch_test.go14
2 files changed, 25 insertions, 0 deletions
diff --git a/common/maps/scratch.go b/common/maps/scratch.go
index ccd03ef32..26ffef7d2 100644
--- a/common/maps/scratch.go
+++ b/common/maps/scratch.go
@@ -129,6 +129,17 @@ func (c *Scratch) SetInMap(key string, mapKey string, value interface{}) string
return ""
}
+// DeleteInMap deletes a value to a map with the given key in the Node context.
+func (c *Scratch) DeleteInMap(key string, mapKey string) string {
+ c.mu.Lock()
+ _, found := c.values[key]
+ if found {
+ delete(c.values[key].(map[string]interface{}), mapKey)
+ }
+ c.mu.Unlock()
+ return ""
+}
+
// GetSortedMapValues returns a sorted map previously filled with SetInMap.
func (c *Scratch) GetSortedMapValues(key string) interface{} {
c.mu.RLock()
diff --git a/common/maps/scratch_test.go b/common/maps/scratch_test.go
index d893ccb03..96b352572 100644
--- a/common/maps/scratch_test.go
+++ b/common/maps/scratch_test.go
@@ -188,6 +188,20 @@ func TestScratchSetInMap(t *testing.T) {
c.Assert(scratch.GetSortedMapValues("key"), qt.DeepEquals, []interface{}{0: "Abc (updated)", 1: "Def", 2: "Lux", 3: "Zyx"})
}
+func TestScratchDeleteInMap(t *testing.T) {
+ t.Parallel()
+ c := qt.New(t)
+
+ scratch := NewScratch()
+ scratch.SetInMap("key", "lux", "Lux")
+ scratch.SetInMap("key", "abc", "Abc")
+ scratch.SetInMap("key", "zyx", "Zyx")
+ scratch.DeleteInMap("key", "abc")
+ scratch.SetInMap("key", "def", "Def")
+ scratch.DeleteInMap("key", "lmn") // Do nothing
+ c.Assert(scratch.GetSortedMapValues("key"), qt.DeepEquals, []interface{}{0: "Def", 1: "Lux", 2: "Zyx"})
+}
+
func TestScratchGetSortedMapValues(t *testing.T) {
t.Parallel()
scratch := NewScratch()