| 1 |
// Copyright 2025 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package hstore |
| 15 |
|
| 16 |
import ( |
| 17 |
"reflect" |
| 18 |
"sort" |
| 19 |
"sync" |
| 20 |
|
| 21 |
"github.com/gohugoio/hugo/common/collections" |
| 22 |
"github.com/gohugoio/hugo/common/math" |
| 23 |
) |
| 24 |
|
| 25 |
type StoreProvider interface { |
| 26 |
// Store returns a Scratch that can be used to store temporary state. |
| 27 |
// Store is not reset on server rebuilds. |
| 28 |
Store() *Scratch |
| 29 |
} |
| 30 |
|
| 31 |
// Scratch is a writable context used for stateful build operations |
| 32 |
type Scratch struct { |
| 33 |
values map[string]any |
| 34 |
mu sync.RWMutex |
| 35 |
} |
| 36 |
|
| 37 |
// Add will, for single values, add (using the + operator) the addend to the existing addend (if found). |
| 38 |
// Supports numeric values and strings. |
| 39 |
// |
| 40 |
// If the first add for a key is an array or slice, then the next value(s) will be appended. |
| 41 |
func (c *Scratch) Add(key string, newAddend any) (string, error) { |
| 42 |
var newVal any |
| 43 |
c.mu.RLock() |
| 44 |
existingAddend, found := c.values[key] |
| 45 |
c.mu.RUnlock() |
| 46 |
if found { |
| 47 |
var err error |
| 48 |
|
| 49 |
addendV := reflect.TypeOf(existingAddend) |
| 50 |
|
| 51 |
if addendV.Kind() == reflect.Slice || addendV.Kind() == reflect.Array { |
| 52 |
newVal, err = collections.Append(existingAddend, newAddend) |
| 53 |
if err != nil { |
| 54 |
return "", err |
| 55 |
} |
| 56 |
} else { |
| 57 |
newVal, err = math.DoArithmetic(existingAddend, newAddend, '+') |
| 58 |
if err != nil { |
| 59 |
return "", err |
| 60 |
} |
| 61 |
} |
| 62 |
} else { |
| 63 |
newVal = newAddend |
| 64 |
} |
| 65 |
c.mu.Lock() |
| 66 |
c.values[key] = newVal |
| 67 |
c.mu.Unlock() |
| 68 |
return "", nil // have to return something to make it work with the Go templates |
| 69 |
} |
| 70 |
|
| 71 |
// Set stores a value with the given key in the Node context. |
| 72 |
// This value can later be retrieved with Get. |
| 73 |
func (c *Scratch) Set(key string, value any) string { |
| 74 |
c.mu.Lock() |
| 75 |
c.values[key] = value |
| 76 |
c.mu.Unlock() |
| 77 |
return "" |
| 78 |
} |
| 79 |
|
| 80 |
// Delete deletes the given key. |
| 81 |
func (c *Scratch) Delete(key string) string { |
| 82 |
c.mu.Lock() |
| 83 |
delete(c.values, key) |
| 84 |
c.mu.Unlock() |
| 85 |
return "" |
| 86 |
} |
| 87 |
|
| 88 |
// Get returns a value previously set by Add or Set. |
| 89 |
func (c *Scratch) Get(key string) any { |
| 90 |
c.mu.RLock() |
| 91 |
val := c.values[key] |
| 92 |
c.mu.RUnlock() |
| 93 |
|
| 94 |
return val |
| 95 |
} |
| 96 |
|
| 97 |
// Values returns the raw backing map. Note that you should just use |
| 98 |
// this method on the locally scoped Scratch instances you obtain via newScratch, not |
| 99 |
// .Page.Scratch etc., as that will lead to concurrency issues. |
| 100 |
func (c *Scratch) Values() map[string]any { |
| 101 |
c.mu.RLock() |
| 102 |
defer c.mu.RUnlock() |
| 103 |
return c.values |
| 104 |
} |
| 105 |
|
| 106 |
// SetInMap stores a value to a map with the given key in the Node context. |
| 107 |
// This map can later be retrieved with GetSortedMapValues. |
| 108 |
func (c *Scratch) SetInMap(key string, mapKey string, value any) string { |
| 109 |
c.mu.Lock() |
| 110 |
_, found := c.values[key] |
| 111 |
if !found { |
| 112 |
c.values[key] = make(map[string]any) |
| 113 |
} |
| 114 |
|
| 115 |
c.values[key].(map[string]any)[mapKey] = value |
| 116 |
c.mu.Unlock() |
| 117 |
return "" |
| 118 |
} |
| 119 |
|
| 120 |
// DeleteInMap deletes a value to a map with the given key in the Node context. |
| 121 |
func (c *Scratch) DeleteInMap(key string, mapKey string) string { |
| 122 |
c.mu.Lock() |
| 123 |
_, found := c.values[key] |
| 124 |
if found { |
| 125 |
delete(c.values[key].(map[string]any), mapKey) |
| 126 |
} |
| 127 |
c.mu.Unlock() |
| 128 |
return "" |
| 129 |
} |
| 130 |
|
| 131 |
// GetSortedMapValues returns a sorted map previously filled with SetInMap. |
| 132 |
func (c *Scratch) GetSortedMapValues(key string) any { |
| 133 |
c.mu.RLock() |
| 134 |
defer c.mu.RUnlock() |
| 135 |
|
| 136 |
if c.values[key] == nil { |
| 137 |
return nil |
| 138 |
} |
| 139 |
|
| 140 |
unsortedMap := c.values[key].(map[string]any) |
| 141 |
var keys []string |
| 142 |
for mapKey := range unsortedMap { |
| 143 |
keys = append(keys, mapKey) |
| 144 |
} |
| 145 |
|
| 146 |
sort.Strings(keys) |
| 147 |
|
| 148 |
sortedArray := make([]any, len(unsortedMap)) |
| 149 |
for i, mapKey := range keys { |
| 150 |
sortedArray[i] = unsortedMap[mapKey] |
| 151 |
} |
| 152 |
|
| 153 |
return sortedArray |
| 154 |
} |
| 155 |
|
| 156 |
// NewScratch returns a new instance of Scratch. |
| 157 |
func NewScratch() *Scratch { |
| 158 |
return &Scratch{values: make(map[string]any)} |
| 159 |
} |