Skip to content

Commit

Permalink
common/maps: Add Scratch.Values
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed May 29, 2020
1 parent 6a3e897 commit 3337f37
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
11 changes: 10 additions & 1 deletion common/maps/scratch.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ func (c *Scratch) Get(key string) interface{} {
return val
}

// Values returns the raw backing map. Note that you should just use
// this method on the locally scoped Scratch instances you obtain via newScratch, not
// .Page.Scratch etc., as that will lead to concurrency issues.
func (c *Scratch) Values() map[string]interface{} {
c.mu.RLock()
defer c.mu.RUnlock()
return c.values
}

// SetInMap stores a value to a map with the given key in the Node context.
// This map can later be retrieved with GetSortedMapValues.
func (c *Scratch) SetInMap(key string, mapKey string, value interface{}) string {
Expand Down Expand Up @@ -147,7 +156,7 @@ func (c *Scratch) GetSortedMapValues(key string) interface{} {
return sortedArray
}

// NewScratch returns a new instance Scratch.
// NewScratch returns a new instance of Scratch.
func NewScratch() *Scratch {
return &Scratch{values: make(map[string]interface{})}
}
3 changes: 3 additions & 0 deletions common/maps/scratch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ func TestScratchAdd(t *testing.T) {
scratch.Add("scratch", scratch)
_, err := scratch.Add("scratch", scratch)

m := scratch.Values()
c.Assert(m, qt.HasLen, 5)

if err == nil {
t.Errorf("Expected error from invalid arithmetic")
}
Expand Down
5 changes: 5 additions & 0 deletions docs/content/en/functions/scratch.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ Removes the given key
{{ .Scratch.Delete "greetings" }}
```

#### .Values

`Values` returns the raw backing map. Note that you should just use this method on the locally scoped `Scratch` instances you obtain via `newScratch`, not
`.Page.Scratch` etc., as that will lead to concurrency issues.

## Scope
The scope of the backing data is global for the given `Page` or `Shortcode`, and spans partial and shortcode includes.

Expand Down

0 comments on commit 3337f37

Please sign in to comment.