Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
// Copyright 2019 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 maps
|
|
|
|
|
|
|
|
import (
|
2021-06-09 04:58:18 -04:00
|
|
|
"fmt"
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/spf13/cast"
|
|
|
|
)
|
|
|
|
|
2019-11-21 15:59:38 -05:00
|
|
|
// Params is a map where all keys are lower case.
|
|
|
|
type Params map[string]interface{}
|
|
|
|
|
|
|
|
// Get does a lower case and nested search in this map.
|
|
|
|
// It will return nil if none found.
|
|
|
|
func (p Params) Get(indices ...string) interface{} {
|
|
|
|
v, _, _ := getNested(p, indices)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2021-06-09 04:58:18 -04:00
|
|
|
// Set overwrites values in p with values in pp for common or new keys.
|
|
|
|
// This is done recursively.
|
|
|
|
func (p Params) Set(pp Params) {
|
|
|
|
for k, v := range pp {
|
|
|
|
vv, found := p[k]
|
|
|
|
if !found {
|
|
|
|
p[k] = v
|
|
|
|
} else {
|
|
|
|
switch vvv := vv.(type) {
|
|
|
|
case Params:
|
|
|
|
if pv, ok := v.(Params); ok {
|
|
|
|
vvv.Set(pv)
|
|
|
|
} else {
|
|
|
|
p[k] = v
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
p[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-27 07:24:49 -04:00
|
|
|
// IsZero returns true if p is considered empty.
|
|
|
|
func (p Params) IsZero() bool {
|
|
|
|
if p == nil || len(p) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p) > 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, _ := range p {
|
|
|
|
return k == mergeStrategyKey
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-09 04:58:18 -04:00
|
|
|
// Merge transfers values from pp to p for new keys.
|
|
|
|
// This is done recursively.
|
|
|
|
func (p Params) Merge(pp Params) {
|
|
|
|
p.merge("", pp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p Params) merge(ps ParamsMergeStrategy, pp Params) {
|
|
|
|
ns, found := p.GetMergeStrategy()
|
|
|
|
|
|
|
|
var ms = ns
|
|
|
|
if !found && ps != "" {
|
|
|
|
ms = ps
|
|
|
|
}
|
|
|
|
|
|
|
|
noUpdate := ms == ParamsMergeStrategyNone
|
|
|
|
noUpdate = noUpdate || (ps != "" && ps == ParamsMergeStrategyShallow)
|
|
|
|
|
|
|
|
for k, v := range pp {
|
|
|
|
|
|
|
|
if k == mergeStrategyKey {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
vv, found := p[k]
|
|
|
|
|
|
|
|
if found {
|
|
|
|
// Key matches, if both sides are Params, we try to merge.
|
|
|
|
if vvv, ok := vv.(Params); ok {
|
|
|
|
if pv, ok := v.(Params); ok {
|
|
|
|
vvv.merge(ms, pv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if !noUpdate {
|
|
|
|
p[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p Params) GetMergeStrategy() (ParamsMergeStrategy, bool) {
|
|
|
|
if v, found := p[mergeStrategyKey]; found {
|
|
|
|
if s, ok := v.(ParamsMergeStrategy); ok {
|
|
|
|
return s, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ParamsMergeStrategyShallow, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p Params) DeleteMergeStrategy() bool {
|
|
|
|
if _, found := p[mergeStrategyKey]; found {
|
|
|
|
delete(p, mergeStrategyKey)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p Params) SetDefaultMergeStrategy(s ParamsMergeStrategy) {
|
|
|
|
switch s {
|
|
|
|
case ParamsMergeStrategyDeep, ParamsMergeStrategyNone, ParamsMergeStrategyShallow:
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("invalid merge strategy %q", s))
|
|
|
|
}
|
|
|
|
p[mergeStrategyKey] = s
|
|
|
|
}
|
|
|
|
|
2019-11-21 15:59:38 -05:00
|
|
|
func getNested(m map[string]interface{}, indices []string) (interface{}, string, map[string]interface{}) {
|
|
|
|
if len(indices) == 0 {
|
|
|
|
return nil, "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
first := indices[0]
|
|
|
|
v, found := m[strings.ToLower(cast.ToString(first))]
|
|
|
|
if !found {
|
2020-10-29 11:22:35 -04:00
|
|
|
if len(indices) == 1 {
|
|
|
|
return nil, first, m
|
|
|
|
}
|
2019-11-21 15:59:38 -05:00
|
|
|
return nil, "", nil
|
2020-10-29 11:22:35 -04:00
|
|
|
|
2019-11-21 15:59:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(indices) == 1 {
|
|
|
|
return v, first, m
|
|
|
|
}
|
|
|
|
|
|
|
|
switch m2 := v.(type) {
|
|
|
|
case Params:
|
|
|
|
return getNested(m2, indices[1:])
|
|
|
|
case map[string]interface{}:
|
|
|
|
return getNested(m2, indices[1:])
|
|
|
|
default:
|
|
|
|
return nil, "", nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
// GetNestedParam gets the first match of the keyStr in the candidates given.
|
|
|
|
// It will first try the exact match and then try to find it as a nested map value,
|
|
|
|
// using the given separator, e.g. "mymap.name".
|
|
|
|
// It assumes that all the maps given have lower cased keys.
|
2019-11-21 15:59:38 -05:00
|
|
|
func GetNestedParam(keyStr, separator string, candidates ...Params) (interface{}, error) {
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
keyStr = strings.ToLower(keyStr)
|
|
|
|
|
2019-11-21 15:59:38 -05:00
|
|
|
// Try exact match first
|
|
|
|
for _, m := range candidates {
|
|
|
|
if v, ok := m[keyStr]; ok {
|
|
|
|
return v, nil
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
keySegments := strings.Split(keyStr, separator)
|
2019-11-21 15:59:38 -05:00
|
|
|
for _, m := range candidates {
|
|
|
|
if v := m.Get(keySegments...); v != nil {
|
|
|
|
return v, nil
|
|
|
|
}
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
}
|
|
|
|
|
2019-11-21 15:59:38 -05:00
|
|
|
return nil, nil
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
}
|
|
|
|
|
2019-11-21 15:59:38 -05:00
|
|
|
func GetNestedParamFn(keyStr, separator string, lookupFn func(key string) interface{}) (interface{}, string, map[string]interface{}, error) {
|
2020-11-24 08:11:42 -05:00
|
|
|
keySegments := strings.Split(keyStr, separator)
|
2019-11-21 15:59:38 -05:00
|
|
|
if len(keySegments) == 0 {
|
|
|
|
return nil, "", nil, nil
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
}
|
|
|
|
|
2019-11-21 15:59:38 -05:00
|
|
|
first := lookupFn(keySegments[0])
|
|
|
|
if first == nil {
|
2019-07-28 06:31:32 -04:00
|
|
|
return nil, "", nil, nil
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
}
|
|
|
|
|
2019-11-21 15:59:38 -05:00
|
|
|
if len(keySegments) == 1 {
|
|
|
|
return first, keySegments[0], nil, nil
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
}
|
|
|
|
|
2019-11-21 15:59:38 -05:00
|
|
|
switch m := first.(type) {
|
|
|
|
case map[string]interface{}:
|
|
|
|
v, key, owner := getNested(m, keySegments[1:])
|
|
|
|
return v, key, owner, nil
|
|
|
|
case Params:
|
|
|
|
v, key, owner := getNested(m, keySegments[1:])
|
|
|
|
return v, key, owner, nil
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
}
|
|
|
|
|
2019-11-21 15:59:38 -05:00
|
|
|
return nil, "", nil, nil
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
}
|
2021-06-09 04:58:18 -04:00
|
|
|
|
|
|
|
// ParamsMergeStrategy tells what strategy to use in Params.Merge.
|
|
|
|
type ParamsMergeStrategy string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Do not merge.
|
|
|
|
ParamsMergeStrategyNone ParamsMergeStrategy = "none"
|
|
|
|
// Only add new keys.
|
|
|
|
ParamsMergeStrategyShallow ParamsMergeStrategy = "shallow"
|
|
|
|
// Add new keys, merge existing.
|
|
|
|
ParamsMergeStrategyDeep ParamsMergeStrategy = "deep"
|
|
|
|
|
|
|
|
mergeStrategyKey = "_merge"
|
|
|
|
)
|
|
|
|
|
|
|
|
func toMergeStrategy(v interface{}) ParamsMergeStrategy {
|
|
|
|
s := ParamsMergeStrategy(cast.ToString(v))
|
|
|
|
switch s {
|
|
|
|
case ParamsMergeStrategyDeep, ParamsMergeStrategyNone, ParamsMergeStrategyShallow:
|
|
|
|
return s
|
|
|
|
default:
|
|
|
|
return ParamsMergeStrategyDeep
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrepareParams
|
|
|
|
// * makes all the keys in the given map lower cased and will do so
|
|
|
|
// * This will modify the map given.
|
2021-06-22 03:53:37 -04:00
|
|
|
// * Any nested map[interface{}]interface{}, map[string]interface{},map[string]string will be converted to Params.
|
2021-06-09 04:58:18 -04:00
|
|
|
// * Any _merge value will be converted to proper type and value.
|
|
|
|
func PrepareParams(m Params) {
|
|
|
|
for k, v := range m {
|
|
|
|
var retyped bool
|
|
|
|
lKey := strings.ToLower(k)
|
|
|
|
if lKey == mergeStrategyKey {
|
|
|
|
v = toMergeStrategy(v)
|
|
|
|
retyped = true
|
|
|
|
} else {
|
2021-06-22 03:53:37 -04:00
|
|
|
switch vv := v.(type) {
|
2021-06-09 04:58:18 -04:00
|
|
|
case map[interface{}]interface{}:
|
|
|
|
var p Params = cast.ToStringMap(v)
|
|
|
|
v = p
|
|
|
|
PrepareParams(p)
|
|
|
|
retyped = true
|
|
|
|
case map[string]interface{}:
|
|
|
|
var p Params = v.(map[string]interface{})
|
|
|
|
v = p
|
|
|
|
PrepareParams(p)
|
|
|
|
retyped = true
|
2021-06-22 03:53:37 -04:00
|
|
|
case map[string]string:
|
|
|
|
p := make(Params)
|
|
|
|
for k, v := range vv {
|
|
|
|
p[k] = v
|
|
|
|
}
|
|
|
|
v = p
|
|
|
|
PrepareParams(p)
|
|
|
|
retyped = true
|
2021-06-09 04:58:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if retyped || k != lKey {
|
|
|
|
delete(m, k)
|
|
|
|
m[lKey] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|