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:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2022-04-11 11:34:08 +0300
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2022-04-13 10:18:17 +0300
commit2dbdf38a5411c554146ddd915a0aea3b2eaf4407 (patch)
tree333aaaac81aff606b1e6d39adc1663cec0fa8a32 /common
parentf8c4e1690a462a2dcafa05aeaab65d1fad6df61e (diff)
resources: Add `key` to reources.GetRemote options map
If set, `key` will be used as the only cache key element for the resource. The default behaviour is to calculate the key based on the URL and all the options. This means that you can now do: ``` {{ $cacheKey := print $url (now.Format "2006-01-02") }} {{ $resource := resource.GetRemote $url (dict "key" $cacheKey) }} ``` Fixes #9755
Diffstat (limited to 'common')
-rw-r--r--common/maps/maps.go14
-rw-r--r--common/maps/maps_test.go23
2 files changed, 37 insertions, 0 deletions
diff --git a/common/maps/maps.go b/common/maps/maps.go
index 5552da55d..2d8a122ca 100644
--- a/common/maps/maps.go
+++ b/common/maps/maps.go
@@ -109,6 +109,20 @@ func ToSliceStringMap(in any) ([]map[string]any, error) {
}
}
+// LookupEqualFold finds key in m with case insensitive equality checks.
+func LookupEqualFold[T any | string](m map[string]T, key string) (T, bool) {
+ if v, found := m[key]; found {
+ return v, true
+ }
+ for k, v := range m {
+ if strings.EqualFold(k, key) {
+ return v, true
+ }
+ }
+ var s T
+ return s, false
+}
+
type keyRename struct {
pattern glob.Glob
newKey string
diff --git a/common/maps/maps_test.go b/common/maps/maps_test.go
index 53120dce7..0b84d2dd7 100644
--- a/common/maps/maps_test.go
+++ b/common/maps/maps_test.go
@@ -171,3 +171,26 @@ func TestRenameKeys(t *testing.T) {
t.Errorf("Expected\n%#v, got\n%#v\n", expected, m)
}
}
+
+func TestLookupEqualFold(t *testing.T) {
+ c := qt.New(t)
+
+ m1 := map[string]any{
+ "a": "av",
+ "B": "bv",
+ }
+
+ v, found := LookupEqualFold(m1, "b")
+ c.Assert(found, qt.IsTrue)
+ c.Assert(v, qt.Equals, "bv")
+
+ m2 := map[string]string{
+ "a": "av",
+ "B": "bv",
+ }
+
+ v, found = LookupEqualFold(m2, "b")
+ c.Assert(found, qt.IsTrue)
+ c.Assert(v, qt.Equals, "bv")
+
+}