Skip to content

Commit

Permalink
hugolib: Add Reset method to delete key from Scratch
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcmal authored and bep committed Mar 16, 2018
1 parent 10fef32 commit e46ab29
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/content/functions/scratch.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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 %}}
Expand Down
8 changes: 8 additions & 0 deletions hugolib/scratch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 9 additions & 0 deletions hugolib/scratch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

2 comments on commit e46ab29

@kaushalmodi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit message says "Reset", but the actual method is "Delete". @bep can you force pushing the fix? Just curious.

@bep
Copy link
Member

@bep bep commented on e46ab29 Mar 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you force pushing the fix? Just curious.

Force push to master is a no no.

And certainly not to correct commit messages.

Please sign in to comment.