diff options
author | Gareth Watts <gareth@omnipotent.net> | 2020-10-22 20:14:14 +0300 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2020-10-23 00:00:19 +0300 |
commit | 3400aff2588cbf9dd4629c05537d16b019d0fdf5 (patch) | |
tree | e3714b454b2366f891cdfc746c5e9d9549d3a973 /common | |
parent | fdfa4a5fe62232f65f1dd8d6fe0c500374228788 (diff) |
Allow cascade _target to work with non toml fm
The TOML lib unmarshals slices of string maps to []map[string]interface{}
whereas YAML and JSON decode to []interface{}
The existing tests only check for TOML working correctly, and _target
with cascade did not work at all for frontmatter defined in other formats.
Add a function to normalize those slices
Fixes #7874
Diffstat (limited to 'common')
-rw-r--r-- | common/maps/maps.go | 18 | ||||
-rw-r--r-- | common/maps/maps_test.go | 33 |
2 files changed, 51 insertions, 0 deletions
diff --git a/common/maps/maps.go b/common/maps/maps.go index 8b42ca764..41d9b6e15 100644 --- a/common/maps/maps.go +++ b/common/maps/maps.go @@ -14,6 +14,7 @@ package maps import ( + "fmt" "strings" "github.com/gobwas/glob" @@ -64,6 +65,23 @@ func ToStringMap(in interface{}) map[string]interface{} { return m } +func ToSliceStringMap(in interface{}) ([]map[string]interface{}, error) { + switch v := in.(type) { + case []map[string]interface{}: + return v, nil + case []interface{}: + var s []map[string]interface{} + for _, entry := range v { + if vv, ok := entry.(map[string]interface{}); ok { + s = append(s, vv) + } + } + return s, nil + default: + return nil, fmt.Errorf("unable to cast %#v of type %T to []map[string]interface{}", in, in) + } +} + type keyRename struct { pattern glob.Glob newKey string diff --git a/common/maps/maps_test.go b/common/maps/maps_test.go index 6e4947adb..bde77071d 100644 --- a/common/maps/maps_test.go +++ b/common/maps/maps_test.go @@ -75,6 +75,39 @@ func TestToLower(t *testing.T) { } } +func TestToSliceStringMap(t *testing.T) { + c := qt.New(t) + + tests := []struct { + input interface{} + expected []map[string]interface{} + }{ + { + input: []map[string]interface{}{ + {"abc": 123}, + }, + expected: []map[string]interface{}{ + {"abc": 123}, + }, + }, { + input: []interface{}{ + map[string]interface{}{ + "def": 456, + }, + }, + expected: []map[string]interface{}{ + {"def": 456}, + }, + }, + } + + for _, test := range tests { + v, err := ToSliceStringMap(test.input) + c.Assert(err, qt.IsNil) + c.Assert(v, qt.DeepEquals, test.expected) + } +} + func TestRenameKeys(t *testing.T) { c := qt.New(t) |