2021-06-09 04:58:18 -04:00
|
|
|
// Copyright 2021 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
xmaps "golang.org/x/exp/maps"
|
|
|
|
|
2021-06-09 04:58:18 -04:00
|
|
|
"github.com/spf13/cast"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/common/maps"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
|
|
|
// ConfigRootKeysSet contains all of the config map root keys.
|
|
|
|
ConfigRootKeysSet = map[string]bool{
|
|
|
|
"build": true,
|
|
|
|
"caches": true,
|
2021-07-09 05:52:03 -04:00
|
|
|
"cascade": true,
|
2021-06-09 04:58:18 -04:00
|
|
|
"frontmatter": true,
|
|
|
|
"languages": true,
|
|
|
|
"imaging": true,
|
|
|
|
"markup": true,
|
|
|
|
"mediatypes": true,
|
|
|
|
"menus": true,
|
|
|
|
"minify": true,
|
|
|
|
"module": true,
|
|
|
|
"outputformats": true,
|
|
|
|
"params": true,
|
|
|
|
"permalinks": true,
|
|
|
|
"related": true,
|
|
|
|
"sitemap": true,
|
2021-12-12 06:11:11 -05:00
|
|
|
"privacy": true,
|
|
|
|
"security": true,
|
2021-06-09 04:58:18 -04:00
|
|
|
"taxonomies": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigRootKeys is a sorted version of ConfigRootKeysSet.
|
|
|
|
ConfigRootKeys []string
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
for k := range ConfigRootKeysSet {
|
|
|
|
ConfigRootKeys = append(ConfigRootKeys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(ConfigRootKeys)
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a Provider backed by an empty maps.Params.
|
|
|
|
func New() Provider {
|
|
|
|
return &defaultConfigProvider{
|
|
|
|
root: make(maps.Params),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFrom creates a Provider backed by params.
|
|
|
|
func NewFrom(params maps.Params) Provider {
|
|
|
|
maps.PrepareParams(params)
|
|
|
|
return &defaultConfigProvider{
|
|
|
|
root: params,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// defaultConfigProvider is a Provider backed by a map where all keys are lower case.
|
|
|
|
// All methods are thread safe.
|
|
|
|
type defaultConfigProvider struct {
|
|
|
|
mu sync.RWMutex
|
|
|
|
root maps.Params
|
|
|
|
|
|
|
|
keyCache sync.Map
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func (c *defaultConfigProvider) Get(k string) any {
|
2021-06-09 04:58:18 -04:00
|
|
|
if k == "" {
|
|
|
|
return c.root
|
|
|
|
}
|
|
|
|
c.mu.RLock()
|
|
|
|
key, m := c.getNestedKeyAndMap(strings.ToLower(k), false)
|
|
|
|
if m == nil {
|
2021-07-26 05:21:11 -04:00
|
|
|
c.mu.RUnlock()
|
2021-06-09 04:58:18 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
v := m[key]
|
|
|
|
c.mu.RUnlock()
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *defaultConfigProvider) GetBool(k string) bool {
|
|
|
|
v := c.Get(k)
|
|
|
|
return cast.ToBool(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *defaultConfigProvider) GetInt(k string) int {
|
|
|
|
v := c.Get(k)
|
|
|
|
return cast.ToInt(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *defaultConfigProvider) IsSet(k string) bool {
|
|
|
|
var found bool
|
|
|
|
c.mu.RLock()
|
|
|
|
key, m := c.getNestedKeyAndMap(strings.ToLower(k), false)
|
|
|
|
if m != nil {
|
|
|
|
_, found = m[key]
|
|
|
|
}
|
|
|
|
c.mu.RUnlock()
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *defaultConfigProvider) GetString(k string) string {
|
|
|
|
v := c.Get(k)
|
|
|
|
return cast.ToString(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *defaultConfigProvider) GetParams(k string) maps.Params {
|
|
|
|
v := c.Get(k)
|
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return v.(maps.Params)
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func (c *defaultConfigProvider) GetStringMap(k string) map[string]any {
|
2021-06-09 04:58:18 -04:00
|
|
|
v := c.Get(k)
|
|
|
|
return maps.ToStringMap(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *defaultConfigProvider) GetStringMapString(k string) map[string]string {
|
|
|
|
v := c.Get(k)
|
|
|
|
return maps.ToStringMapString(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *defaultConfigProvider) GetStringSlice(k string) []string {
|
|
|
|
v := c.Get(k)
|
|
|
|
return cast.ToStringSlice(v)
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func (c *defaultConfigProvider) Set(k string, v any) {
|
2021-06-09 04:58:18 -04:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
k = strings.ToLower(k)
|
|
|
|
|
|
|
|
if k == "" {
|
2023-01-04 12:24:36 -05:00
|
|
|
if p, err := maps.ToParamsAndPrepare(v); err == nil {
|
2021-06-09 04:58:18 -04:00
|
|
|
// Set the values directly in root.
|
2023-01-04 12:24:36 -05:00
|
|
|
maps.SetParams(c.root, p)
|
2021-06-09 04:58:18 -04:00
|
|
|
} else {
|
|
|
|
c.root[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch vv := v.(type) {
|
2022-03-17 17:03:27 -04:00
|
|
|
case map[string]any, map[any]any, map[string]string:
|
2021-06-22 03:53:37 -04:00
|
|
|
p := maps.MustToParamsAndPrepare(vv)
|
2021-06-09 04:58:18 -04:00
|
|
|
v = p
|
|
|
|
}
|
|
|
|
|
|
|
|
key, m := c.getNestedKeyAndMap(k, true)
|
2021-06-29 03:38:05 -04:00
|
|
|
if m == nil {
|
|
|
|
return
|
|
|
|
}
|
2021-06-09 04:58:18 -04:00
|
|
|
|
|
|
|
if existing, found := m[key]; found {
|
|
|
|
if p1, ok := existing.(maps.Params); ok {
|
|
|
|
if p2, ok := v.(maps.Params); ok {
|
2023-01-04 12:24:36 -05:00
|
|
|
maps.SetParams(p1, p2)
|
2021-06-09 04:58:18 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m[key] = v
|
|
|
|
}
|
|
|
|
|
2021-06-22 03:53:37 -04:00
|
|
|
// SetDefaults will set values from params if not already set.
|
|
|
|
func (c *defaultConfigProvider) SetDefaults(params maps.Params) {
|
|
|
|
maps.PrepareParams(params)
|
|
|
|
for k, v := range params {
|
|
|
|
if _, found := c.root[k]; !found {
|
|
|
|
c.root[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func (c *defaultConfigProvider) Merge(k string, v any) {
|
2021-06-09 04:58:18 -04:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
k = strings.ToLower(k)
|
|
|
|
|
|
|
|
if k == "" {
|
|
|
|
rs, f := c.root.GetMergeStrategy()
|
|
|
|
if f && rs == maps.ParamsMergeStrategyNone {
|
|
|
|
// The user has set a "no merge" strategy on this,
|
|
|
|
// nothing more to do.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
if p, err := maps.ToParamsAndPrepare(v); err == nil {
|
2021-06-09 04:58:18 -04:00
|
|
|
// As there may be keys in p not in root, we need to handle
|
|
|
|
// those as a special case.
|
2021-06-27 07:24:49 -04:00
|
|
|
var keysToDelete []string
|
2021-06-09 04:58:18 -04:00
|
|
|
for kk, vv := range p {
|
|
|
|
if pp, ok := vv.(maps.Params); ok {
|
2021-06-22 12:17:49 -04:00
|
|
|
if pppi, ok := c.root[kk]; ok {
|
|
|
|
ppp := pppi.(maps.Params)
|
2023-01-04 12:24:36 -05:00
|
|
|
maps.MergeParamsWithStrategy("", ppp, pp)
|
2021-06-09 04:58:18 -04:00
|
|
|
} else {
|
|
|
|
// We need to use the default merge strategy for
|
|
|
|
// this key.
|
|
|
|
np := make(maps.Params)
|
2023-01-04 12:24:36 -05:00
|
|
|
strategy := c.determineMergeStrategy(maps.KeyParams{Key: "", Params: c.root}, maps.KeyParams{Key: kk, Params: np})
|
|
|
|
np.SetMergeStrategy(strategy)
|
|
|
|
maps.MergeParamsWithStrategy("", np, pp)
|
2021-06-27 07:24:49 -04:00
|
|
|
c.root[kk] = np
|
|
|
|
if np.IsZero() {
|
|
|
|
// Just keep it until merge is done.
|
|
|
|
keysToDelete = append(keysToDelete, kk)
|
2021-06-09 04:58:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Merge the rest.
|
2023-01-04 12:24:36 -05:00
|
|
|
maps.MergeParams(c.root, p)
|
2021-06-27 07:24:49 -04:00
|
|
|
for _, k := range keysToDelete {
|
|
|
|
delete(c.root, k)
|
|
|
|
}
|
2021-06-09 04:58:18 -04:00
|
|
|
} else {
|
|
|
|
panic(fmt.Sprintf("unsupported type %T received in Merge", v))
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch vv := v.(type) {
|
2022-03-17 17:03:27 -04:00
|
|
|
case map[string]any, map[any]any, map[string]string:
|
2021-06-22 03:53:37 -04:00
|
|
|
p := maps.MustToParamsAndPrepare(vv)
|
2021-06-09 04:58:18 -04:00
|
|
|
v = p
|
|
|
|
}
|
|
|
|
|
|
|
|
key, m := c.getNestedKeyAndMap(k, true)
|
2021-06-29 03:38:05 -04:00
|
|
|
if m == nil {
|
|
|
|
return
|
|
|
|
}
|
2021-06-09 04:58:18 -04:00
|
|
|
|
|
|
|
if existing, found := m[key]; found {
|
|
|
|
if p1, ok := existing.(maps.Params); ok {
|
|
|
|
if p2, ok := v.(maps.Params); ok {
|
2023-01-04 12:24:36 -05:00
|
|
|
maps.MergeParamsWithStrategy("", p1, p2)
|
2021-06-09 04:58:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
m[key] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (c *defaultConfigProvider) Keys() []string {
|
|
|
|
c.mu.RLock()
|
|
|
|
defer c.mu.RUnlock()
|
|
|
|
return xmaps.Keys(c.root)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *defaultConfigProvider) WalkParams(walkFn func(params ...maps.KeyParams) bool) {
|
|
|
|
var walk func(params ...maps.KeyParams)
|
|
|
|
walk = func(params ...maps.KeyParams) {
|
2021-06-09 04:58:18 -04:00
|
|
|
if walkFn(params...) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p1 := params[len(params)-1]
|
|
|
|
i := len(params)
|
|
|
|
for k, v := range p1.Params {
|
|
|
|
if p2, ok := v.(maps.Params); ok {
|
2023-01-04 12:24:36 -05:00
|
|
|
paramsplus1 := make([]maps.KeyParams, i+1)
|
2021-06-09 04:58:18 -04:00
|
|
|
copy(paramsplus1, params)
|
2023-01-04 12:24:36 -05:00
|
|
|
paramsplus1[i] = maps.KeyParams{Key: k, Params: p2}
|
2021-06-09 04:58:18 -04:00
|
|
|
walk(paramsplus1...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
walk(maps.KeyParams{Key: "", Params: c.root})
|
2021-06-09 04:58:18 -04:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (c *defaultConfigProvider) determineMergeStrategy(params ...maps.KeyParams) maps.ParamsMergeStrategy {
|
2021-06-09 04:58:18 -04:00
|
|
|
if len(params) == 0 {
|
|
|
|
return maps.ParamsMergeStrategyNone
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
strategy maps.ParamsMergeStrategy
|
|
|
|
prevIsRoot bool
|
|
|
|
curr = params[len(params)-1]
|
|
|
|
)
|
|
|
|
|
|
|
|
if len(params) > 1 {
|
|
|
|
prev := params[len(params)-2]
|
|
|
|
prevIsRoot = prev.Key == ""
|
|
|
|
|
|
|
|
// Inherit from parent (but not from the root unless it's set by user).
|
|
|
|
s, found := prev.Params.GetMergeStrategy()
|
|
|
|
if !prevIsRoot && !found {
|
|
|
|
panic("invalid state, merge strategy not set on parent")
|
|
|
|
}
|
|
|
|
if found || !prevIsRoot {
|
|
|
|
strategy = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch curr.Key {
|
|
|
|
case "":
|
|
|
|
// Don't set a merge strategy on the root unless set by user.
|
|
|
|
// This will be handled as a special case.
|
|
|
|
case "params":
|
|
|
|
strategy = maps.ParamsMergeStrategyDeep
|
|
|
|
case "outputformats", "mediatypes":
|
|
|
|
if prevIsRoot {
|
|
|
|
strategy = maps.ParamsMergeStrategyShallow
|
|
|
|
}
|
|
|
|
case "menus":
|
|
|
|
isMenuKey := prevIsRoot
|
|
|
|
if !isMenuKey {
|
|
|
|
// Can also be set below languages.
|
|
|
|
// root > languages > en > menus
|
|
|
|
if len(params) == 4 && params[1].Key == "languages" {
|
|
|
|
isMenuKey = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isMenuKey {
|
|
|
|
strategy = maps.ParamsMergeStrategyShallow
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if strategy == "" {
|
|
|
|
strategy = maps.ParamsMergeStrategyNone
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return strategy
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *defaultConfigProvider) SetDefaultMergeStrategy() {
|
2023-01-04 12:24:36 -05:00
|
|
|
c.WalkParams(func(params ...maps.KeyParams) bool {
|
2021-06-09 04:58:18 -04:00
|
|
|
if len(params) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
p := params[len(params)-1].Params
|
|
|
|
var found bool
|
|
|
|
if _, found = p.GetMergeStrategy(); found {
|
|
|
|
// Set by user.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
strategy := c.determineMergeStrategy(params...)
|
|
|
|
if strategy != "" {
|
2023-01-04 12:24:36 -05:00
|
|
|
p.SetMergeStrategy(strategy)
|
2021-06-09 04:58:18 -04:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *defaultConfigProvider) getNestedKeyAndMap(key string, create bool) (string, maps.Params) {
|
|
|
|
var parts []string
|
|
|
|
v, ok := c.keyCache.Load(key)
|
|
|
|
if ok {
|
|
|
|
parts = v.([]string)
|
|
|
|
} else {
|
|
|
|
parts = strings.Split(key, ".")
|
|
|
|
c.keyCache.Store(key, parts)
|
|
|
|
}
|
|
|
|
current := c.root
|
|
|
|
for i := 0; i < len(parts)-1; i++ {
|
|
|
|
next, found := current[parts[i]]
|
|
|
|
if !found {
|
|
|
|
if create {
|
|
|
|
next = make(maps.Params)
|
|
|
|
current[parts[i]] = next
|
|
|
|
} else {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
}
|
2021-06-29 03:38:05 -04:00
|
|
|
var ok bool
|
|
|
|
current, ok = next.(maps.Params)
|
|
|
|
if !ok {
|
|
|
|
// E.g. a string, not a map that we can store values in.
|
|
|
|
return "", nil
|
|
|
|
}
|
2021-06-09 04:58:18 -04:00
|
|
|
}
|
|
|
|
return parts[len(parts)-1], current
|
|
|
|
}
|