From d1eb2ea9e2c3ec400c9f69bb66fd97b4ad40f566 Mon Sep 17 00:00:00 2001 From: Maxime Date: Sun, 26 Jul 2015 18:32:34 +0200 Subject: [PATCH] Changed metadata variables type: from map[string]interface{} to map[string]string --- middleware/markdown/metadata.go | 34 ++++++++++++++++++++++------ middleware/markdown/metadata_test.go | 2 +- middleware/markdown/process.go | 6 ++--- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/middleware/markdown/metadata.go b/middleware/markdown/metadata.go index 1420f97b2..9dc2cf1ba 100644 --- a/middleware/markdown/metadata.go +++ b/middleware/markdown/metadata.go @@ -13,9 +13,9 @@ import ( var ( parsers = []MetadataParser{ - &JSONMetadataParser{metadata: Metadata{Variables: make(map[string]interface{})}}, - &TOMLMetadataParser{metadata: Metadata{Variables: make(map[string]interface{})}}, - &YAMLMetadataParser{metadata: Metadata{Variables: make(map[string]interface{})}}, + &JSONMetadataParser{metadata: Metadata{Variables: make(map[string]string)}}, + &TOMLMetadataParser{metadata: Metadata{Variables: make(map[string]string)}}, + &YAMLMetadataParser{metadata: Metadata{Variables: make(map[string]string)}}, } ) @@ -28,7 +28,7 @@ type Metadata struct { Template string // Variables to be used with Template - Variables map[string]interface{} + Variables map[string]string } // load loads parsed values in parsedMap into Metadata @@ -40,7 +40,7 @@ func (m *Metadata) load(parsedMap map[string]interface{}) { m.Template, _ = template.(string) } if variables, ok := parsedMap["variables"]; ok { - m.Variables, _ = variables.(map[string]interface{}) + m.Variables, _ = variables.(map[string]string) } } @@ -76,6 +76,15 @@ func (j *JSONMetadataParser) Parse(b []byte) ([]byte, error) { if err := decoder.Decode(&m); err != nil { return b, err } + if vars, ok := m["variables"].(map[string]interface{}); ok { + vars1 := make(map[string]string) + for k, v := range vars { + if val, ok := v.(string); ok { + vars1[k] = val + } + } + m["variables"] = vars1 + } j.metadata.load(m) @@ -120,6 +129,15 @@ func (t *TOMLMetadataParser) Parse(b []byte) ([]byte, error) { if err := toml.Unmarshal(b, &m); err != nil { return markdown, err } + if vars, ok := m["variables"].(map[string]interface{}); ok { + vars1 := make(map[string]string) + for k, v := range vars { + if val, ok := v.(string); ok { + vars1[k] = val + } + } + m["variables"] = vars1 + } t.metadata.load(m) return markdown, nil } @@ -160,10 +178,12 @@ func (y *YAMLMetadataParser) Parse(b []byte) ([]byte, error) { // convert variables (if present) to map[string]interface{} // to match expected type if vars, ok := m["variables"].(map[interface{}]interface{}); ok { - vars1 := make(map[string]interface{}) + vars1 := make(map[string]string) for k, v := range vars { if key, ok := k.(string); ok { - vars1[key] = v + if val, ok := v.(string); ok { + vars1[key] = val + } } } m["variables"] = vars1 diff --git a/middleware/markdown/metadata_test.go b/middleware/markdown/metadata_test.go index 861e485ba..c1028f293 100644 --- a/middleware/markdown/metadata_test.go +++ b/middleware/markdown/metadata_test.go @@ -98,7 +98,7 @@ func TestParsers(t *testing.T) { expected := Metadata{ Title: "A title", Template: "default", - Variables: map[string]interface{}{"name": "value"}, + Variables: map[string]string{"name": "value"}, } compare := func(m Metadata) bool { if m.Title != expected.Title { diff --git a/middleware/markdown/process.go b/middleware/markdown/process.go index 81e2f4ec8..b97c205a5 100644 --- a/middleware/markdown/process.go +++ b/middleware/markdown/process.go @@ -20,13 +20,13 @@ const ( type MarkdownData struct { middleware.Context - Doc map[string]interface{} + Doc map[string]string } // Process processes the contents of a page in b. It parses the metadata // (if any) and uses the template (if found). func (md Markdown) Process(c Config, requestPath string, b []byte, ctx middleware.Context) ([]byte, error) { - var metadata = Metadata{Variables: make(map[string]interface{})} + var metadata = Metadata{Variables: make(map[string]string)} var markdown []byte var err error @@ -166,7 +166,7 @@ func defaultTemplate(c Config, metadata Metadata, requestPath string) []byte { } // Title is first line (length-limited), otherwise filename - title, _ := metadata.Variables["title"].(string) + title, _ := metadata.Variables["title"] html := []byte(htmlTemplate) html = bytes.Replace(html, []byte("{{title}}"), []byte(title), 1)