Fix case issue Viper vs Blackfriday config

There are still work to be done in the case department, but that will have to be another day.

Fixes #2581
See https://github.com/spf13/viper/issues/261
This commit is contained in:
Bjørn Erik Pedersen
2016-10-16 19:28:21 +02:00
committed by GitHub
parent 4d6cd3cb2a
commit 40b1b8f703
7 changed files with 102 additions and 16 deletions

View File

@@ -27,7 +27,6 @@ import (
"github.com/miekg/mmark"
"github.com/mitchellh/mapstructure"
"github.com/russross/blackfriday"
"github.com/spf13/cast"
bp "github.com/spf13/hugo/bufferpool"
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"
@@ -60,7 +59,8 @@ type Blackfriday struct {
// NewBlackfriday creates a new Blackfriday filled with site config or some sane defaults.
func NewBlackfriday(c ConfigProvider) *Blackfriday {
combinedParam := map[string]interface{}{
defaultParam := map[string]interface{}{
"smartypants": true,
"angledQuotes": false,
"fractions": true,
@@ -73,17 +73,24 @@ func NewBlackfriday(c ConfigProvider) *Blackfriday {
"sourceRelativeLinksProjectFolder": "/docs/content",
}
siteParam := c.GetStringMap("blackfriday")
if siteParam != nil {
siteConfig := cast.ToStringMap(siteParam)
ToLowerMap(defaultParam)
for key, value := range siteConfig {
combinedParam[key] = value
siteParam := c.GetStringMap("blackfriday")
siteConfig := make(map[string]interface{})
for k, v := range defaultParam {
siteConfig[k] = v
}
if siteParam != nil {
for k, v := range siteParam {
siteConfig[k] = v
}
}
combinedConfig := &Blackfriday{}
if err := mapstructure.Decode(combinedParam, combinedConfig); err != nil {
if err := mapstructure.Decode(siteConfig, combinedConfig); err != nil {
jww.FATAL.Printf("Failed to get site rendering config\n%s", err.Error())
}

View File

@@ -119,6 +119,29 @@ func ReaderToBytes(lines io.Reader) []byte {
return bc
}
// ToLowerMap makes all the keys in the given map lower cased and will do so
// recursively.
// Notes:
// * This will modify the map given.
// * Any nested map[interface{}]interface{} will be converted to map[string]interface{}.
func ToLowerMap(m map[string]interface{}) {
for k, v := range m {
switch v.(type) {
case map[interface{}]interface{}:
v = cast.ToStringMap(v)
ToLowerMap(v.(map[string]interface{}))
case map[string]interface{}:
ToLowerMap(v.(map[string]interface{}))
}
lKey := strings.ToLower(k)
if k != lKey {
delete(m, k)
}
m[lKey] = v
}
}
// ReaderToString is the same as ReaderToBytes, but returns a string.
func ReaderToString(lines io.Reader) string {
if lines == nil {

View File

@@ -291,3 +291,56 @@ func TestDoArithmetic(t *testing.T) {
}
}
}
func TestToLowerMap(t *testing.T) {
tests := []struct {
input map[string]interface{}
expected map[string]interface{}
}{
{
map[string]interface{}{
"abC": 32,
},
map[string]interface{}{
"abc": 32,
},
},
{
map[string]interface{}{
"abC": 32,
"deF": map[interface{}]interface{}{
23: "A value",
24: map[string]interface{}{
"AbCDe": "A value",
"eFgHi": "Another value",
},
},
"gHi": map[string]interface{}{
"J": 25,
},
},
map[string]interface{}{
"abc": 32,
"def": map[string]interface{}{
"23": "A value",
"24": map[string]interface{}{
"abcde": "A value",
"efghi": "Another value",
},
},
"ghi": map[string]interface{}{
"j": 25,
},
},
},
}
for i, test := range tests {
// ToLowerMap modifies input.
ToLowerMap(test.input)
if !reflect.DeepEqual(test.expected, test.input) {
t.Errorf("[%d] Expected\n%#v, got\n%#v\n", i, test.expected, test.input)
}
}
}