From f66f5af5621d136436853925b2d507034d4ef552 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sat, 9 Jan 2016 21:19:27 +0000 Subject: [PATCH] Fix #36 and reduce code repetitions --- frontmatter/frontmatter.go | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/frontmatter/frontmatter.go b/frontmatter/frontmatter.go index d6062319..32ccc512 100644 --- a/frontmatter/frontmatter.go +++ b/frontmatter/frontmatter.go @@ -42,28 +42,28 @@ func rawToPretty(config interface{}, parent *frontmatter) interface{} { arrays := []*frontmatter{} fields := []*frontmatter{} - if parent.Type == "array" { - for index, element := range config.([]interface{}) { - if utils.IsMap(element) { - objects = append(objects, handleObjects(element, parent, string(index))) - } else if utils.IsSlice(element) { - arrays = append(arrays, handleArrays(element, parent, string(index))) - } else { - fields = append(fields, handleFlatValues(element, parent, string(index))) - } + cnf := map[string]interface{}{} + + if reflect.TypeOf(config) == reflect.TypeOf(map[interface{}]interface{}{}) { + for key, value := range config.(map[interface{}]interface{}) { + cnf[key.(string)] = value } - } else if parent.Type == "object" { - for name, element := range config.(map[string]interface{}) { - if utils.IsMap(element) { - objects = append(objects, handleObjects(element, parent, name)) - } else if utils.IsSlice(element) { - arrays = append(arrays, handleArrays(element, parent, name)) - } else { - fields = append(fields, handleFlatValues(element, parent, name)) - } + } else if reflect.TypeOf(config) == reflect.TypeOf([]interface{}{}) { + for key, value := range config.([]interface{}) { + cnf[string(key)] = value } } else { - log.Panic("Parent type not allowed.") + cnf = config.(map[string]interface{}) + } + + for name, element := range cnf { + if utils.IsMap(element) { + objects = append(objects, handleObjects(element, parent, name)) + } else if utils.IsSlice(element) { + arrays = append(arrays, handleArrays(element, parent, name)) + } else { + fields = append(fields, handleFlatValues(element, parent, name)) + } } sort.Sort(sortByTitle(objects))