Debian Codegraph @

common/hmaps/maps.go

raw
1 // Copyright 2026 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 hmaps
15
16 import (
17 "fmt"
18 "strings"
19
20 "github.com/gohugoio/hugo/common/types"
21
22 "github.com/gobwas/glob"
23 "github.com/spf13/cast"
24 )
25
26 // ToStringMapE converts in to map[string]interface{}.
27 func ToStringMapE(in any) (map[string]any, error) {
28 switch vv := in.(type) {
29 case Params:
30 return vv, nil
31 case map[string]string:
32 m := map[string]any{}
33 for k, v := range vv {
34 m[k] = v
35 }
36 return m, nil
37
38 default:
39 return cast.ToStringMapE(in)
40 }
41 }
42
43 // ToParamsAndPrepare converts in to Params and prepares it for use.
44 // If in is nil, an empty map is returned.
45 // See PrepareParams.
46 func ToParamsAndPrepare(in any) (Params, error) {
47 if types.IsNil(in) {
48 return Params{}, nil
49 }
50 m, err := ToStringMapE(in)
51 if err != nil {
52 return nil, err
53 }
54 PrepareParams(m)
55 return m, nil
56 }
57
58 // MustToParamsAndPrepare calls ToParamsAndPrepare and panics if it fails.
59 func MustToParamsAndPrepare(in any) Params {
60 p, err := ToParamsAndPrepare(in)
61 if err != nil {
62 panic(fmt.Sprintf("cannot convert %T to maps.Params: %s", in, err))
63 }
64 return p
65 }
66
67 // ToStringMap converts in to map[string]interface{}.
68 func ToStringMap(in any) map[string]any {
69 m, _ := ToStringMapE(in)
70 return m
71 }
72
73 // ToStringMapStringE converts in to map[string]string.
74 func ToStringMapStringE(in any) (map[string]string, error) {
75 m, err := ToStringMapE(in)
76 if err != nil {
77 return nil, err
78 }
79 return cast.ToStringMapStringE(m)
80 }
81
82 // ToStringMapString converts in to map[string]string.
83 func ToStringMapString(in any) map[string]string {
84 m, _ := ToStringMapStringE(in)
85 return m
86 }
87
88 // ToStringMapBool converts in to bool.
89 func ToStringMapBool(in any) map[string]bool {
90 m, _ := ToStringMapE(in)
91 return cast.ToStringMapBool(m)
92 }
93
94 // ToSliceStringMap converts in to []map[string]interface{}.
95 func ToSliceStringMap(in any) ([]map[string]any, error) {
96 switch v := in.(type) {
97 case []map[string]any:
98 return v, nil
99 case Params:
100 return []map[string]any{v}, nil
101 case map[string]any:
102 return []map[string]any{v}, nil
103 case []any:
104 var s []map[string]any
105 for _, entry := range v {
106 if vv, ok := entry.(map[string]any); ok {
107 s = append(s, vv)
108 }
109 }
110 return s, nil
111 default:
112 return nil, fmt.Errorf("unable to cast %#v of type %T to []map[string]interface{}", in, in)
113 }
114 }
115
116 // LookupEqualFold finds key in m with case insensitive equality checks.
117 func LookupEqualFold[T any | string](m map[string]T, key string) (T, string, bool) {
118 if v, found := m[key]; found {
119 return v, key, true
120 }
121 for k, v := range m {
122 if strings.EqualFold(k, key) {
123 return v, k, true
124 }
125 }
126 var s T
127 return s, "", false
128 }
129
130 // MergeShallow merges src into dst, but only if the key does not already exist in dst.
131 // The keys are compared case insensitively.
132 func MergeShallow(dst, src map[string]any) {
133 for k, v := range src {
134 found := false
135 for dk := range dst {
136 if strings.EqualFold(dk, k) {
137 found = true
138 break
139 }
140 }
141 if !found {
142 dst[k] = v
143 }
144 }
145 }
146
147 type keyRename struct {
148 pattern glob.Glob
149 newKey string
150 }
151
152 // KeyRenamer supports renaming of keys in a map.
153 type KeyRenamer struct {
154 renames []keyRename
155 }
156
157 // NewKeyRenamer creates a new KeyRenamer given a list of pattern and new key
158 // value pairs.
159 func NewKeyRenamer(patternKeys ...string) (KeyRenamer, error) {
160 var renames []keyRename
161 for i := 0; i < len(patternKeys); i += 2 {
162 g, err := glob.Compile(strings.ToLower(patternKeys[i]), '/')
163 if err != nil {
164 return KeyRenamer{}, err
165 }
166 renames = append(renames, keyRename{pattern: g, newKey: patternKeys[i+1]})
167 }
168
169 return KeyRenamer{renames: renames}, nil
170 }
171
172 func (r KeyRenamer) getNewKey(keyPath string) string {
173 for _, matcher := range r.renames {
174 if matcher.pattern.Match(keyPath) {
175 return matcher.newKey
176 }
177 }
178
179 return ""
180 }
181
182 // Rename renames the keys in the given map according
183 // to the patterns in the current KeyRenamer.
184 func (r KeyRenamer) Rename(m map[string]any) {
185 r.renamePath("", m)
186 }
187
188 func (KeyRenamer) keyPath(k1, k2 string) string {
189 k1, k2 = strings.ToLower(k1), strings.ToLower(k2)
190 if k1 == "" {
191 return k2
192 }
193 return k1 + "/" + k2
194 }
195
196 func (r KeyRenamer) renamePath(parentKeyPath string, m map[string]any) {
197 for k, v := range m {
198 keyPath := r.keyPath(parentKeyPath, k)
199 switch vv := v.(type) {
200 case map[any]any:
201 r.renamePath(keyPath, cast.ToStringMap(vv))
202 case map[string]any:
203 r.renamePath(keyPath, vv)
204 }
205
206 newKey := r.getNewKey(keyPath)
207
208 if newKey != "" {
209 delete(m, k)
210 m[newKey] = v
211 }
212 }
213 }
214
215 // ConvertFloat64WithNoDecimalsToInt converts float64 values with no decimals to int recursively.
216 func ConvertFloat64WithNoDecimalsToInt(m map[string]any) {
217 for k, v := range m {
218 switch vv := v.(type) {
219 case float64:
220 if v == float64(int64(vv)) {
221 m[k] = int64(vv)
222 }
223 case map[string]any:
224 ConvertFloat64WithNoDecimalsToInt(vv)
225 case []any:
226 for i, vvv := range vv {
227 switch vvvv := vvv.(type) {
228 case float64:
229 if vvv == float64(int64(vvvv)) {
230 vv[i] = int64(vvvv)
231 }
232 case map[string]any:
233 ConvertFloat64WithNoDecimalsToInt(vvvv)
234 }
235 }
236 }
237 }
238 }