ref: 2919a6a503f7b369154d6eb787023a1fe58a9ad4
parent: 432885c499849efb29d3e50196f377fe0e908333
author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
date: Fri May 29 14:50:12 EDT 2020
common/maps: Add Scratch.Values Fixes #7335
--- a/common/maps/scratch.go
+++ b/common/maps/scratch.go
@@ -107,6 +107,15 @@
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 {
@@ -147,7 +156,7 @@
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{})}
}
--- a/common/maps/scratch_test.go
+++ b/common/maps/scratch_test.go
@@ -47,6 +47,9 @@
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")
}
--- a/docs/content/en/functions/scratch.md
+++ b/docs/content/en/functions/scratch.md
@@ -113,6 +113,11 @@
{{ .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.