From 17e7e6076a5567e6c8ee61ac04316e3399be3155 Mon Sep 17 00:00:00 2001 From: Mateusz Gajewski Date: Sat, 3 Dec 2016 07:35:33 +0100 Subject: [PATCH] Fix for #1276 - support integers and floats as metadata in markdown (#1278) * Fix for #1276 * Use strconv.Format * Use map[string]interface{} as variables * One more file * Always run all tests before commit * Get rid of DocFlags --- caddyhttp/markdown/metadata/metadata.go | 18 ++----- caddyhttp/markdown/metadata/metadata_test.go | 54 ++++++++++++++------ caddyhttp/markdown/setup_test.go | 9 ++-- caddyhttp/markdown/template.go | 20 ++++---- 4 files changed, 54 insertions(+), 47 deletions(-) diff --git a/caddyhttp/markdown/metadata/metadata.go b/caddyhttp/markdown/metadata/metadata.go index cecdc11e..a5e1c370 100644 --- a/caddyhttp/markdown/metadata/metadata.go +++ b/caddyhttp/markdown/metadata/metadata.go @@ -27,17 +27,13 @@ type Metadata struct { Date time.Time // Variables to be used with Template - Variables map[string]string - - // Flags to be used with Template - Flags map[string]bool + Variables map[string]interface{} } // NewMetadata returns a new Metadata struct, loaded with the given map func NewMetadata(parsedMap map[string]interface{}) Metadata { md := Metadata{ - Variables: make(map[string]string), - Flags: make(map[string]bool), + Variables: make(map[string]interface{}), } md.load(parsedMap) @@ -63,15 +59,7 @@ func (m *Metadata) load(parsedMap map[string]interface{}) { } } - // Store everything as a flag or variable - for key, val := range parsedMap { - switch v := val.(type) { - case bool: - m.Flags[key] = v - case string: - m.Variables[key] = v - } - } + m.Variables = parsedMap } // Parser is a an interface that must be satisfied by each parser diff --git a/caddyhttp/markdown/metadata/metadata_test.go b/caddyhttp/markdown/metadata/metadata_test.go index d3b91ee0..a5663d55 100644 --- a/caddyhttp/markdown/metadata/metadata_test.go +++ b/caddyhttp/markdown/metadata/metadata_test.go @@ -2,6 +2,7 @@ package metadata import ( "bytes" + "fmt" "strings" "testing" ) @@ -18,6 +19,8 @@ template = "default" name = "value" positive = true negative = false +number = 1410 +float = 1410.07 `, `+++ title = "A title" @@ -25,6 +28,8 @@ template = "default" name = "value" positive = true negative = false +number = 1410 +float = 1410.07 +++ Page content `, @@ -34,6 +39,8 @@ template = "default" name = "value" positive = true negative = false +number = 1410 +float = 1410.07 `, `title = "A title" template = "default" [variables] name = "value"`, `+++ @@ -42,6 +49,8 @@ template = "default" name = "value" positive = true negative = false +number = 1410 +float = 1410.07 +++ `, } @@ -52,6 +61,8 @@ template : default name : value positive : true negative : false +number : 1410 +float : 1410.07 `, `--- title : A title @@ -59,6 +70,8 @@ template : default name : value positive : true negative : false +number : 1410 +float : 1410.07 --- Page content `, @@ -66,6 +79,8 @@ negative : false title : A title template : default name : value +number : 1410 +float : 1410.07 `, `title : A title template : default variables : name : value : positive : true : negative : false`, `--- @@ -74,6 +89,8 @@ template : default name : value positive : true negative : false +number : 1410 +float : 1410.07 --- `, } @@ -83,14 +100,18 @@ var JSON = [5]string{` "template" : "default", "name" : "value", "positive" : true, - "negative" : false + "negative" : false, + "number": 1410, + "float": 1410.07 `, `{ "title" : "A title", "template" : "default", "name" : "value", "positive" : true, - "negative" : false + "negative" : false, + "number" : 1410, + "float": 1410.07 } Page content `, @@ -100,7 +121,9 @@ Page content "template" : "default", "name" : "value", "positive" : true, - "negative" : false + "negative" : false, + "number" : 1410, + "float": 1410.07 `, ` { @@ -108,7 +131,9 @@ Page content "template" : "default", "name" : "value", "positive" : true, - "negative" : false + "negative" : false, + "number" : 1410, + "float": 1410.07 } `, `{ @@ -116,7 +141,9 @@ Page content "template" : "default", "name" : "value", "positive" : true, - "negative" : false + "negative" : false, + "number" : 1410, + "float": 1410.07 } `, } @@ -125,12 +152,12 @@ func TestParsers(t *testing.T) { expected := Metadata{ Title: "A title", Template: "default", - Variables: map[string]string{ + Variables: map[string]interface{}{ "name": "value", "title": "A title", "template": "default", - }, - Flags: map[string]bool{ + "number": 1410, + "float": 1410.07, "positive": true, "negative": false, }, @@ -143,18 +170,13 @@ func TestParsers(t *testing.T) { return false } for k, v := range m.Variables { - if v != expected.Variables[k] { - return false - } - } - for k, v := range m.Flags { - if v != expected.Flags[k] { + if fmt.Sprintf("%v", v) != fmt.Sprintf("%v", expected.Variables[k]) { return false } } + varLenOK := len(m.Variables) == len(expected.Variables) - flagLenOK := len(m.Flags) == len(expected.Flags) - return varLenOK && flagLenOK + return varLenOK } data := []struct { diff --git a/caddyhttp/markdown/setup_test.go b/caddyhttp/markdown/setup_test.go index 427e242c..a86f95e5 100644 --- a/caddyhttp/markdown/setup_test.go +++ b/caddyhttp/markdown/setup_test.go @@ -130,11 +130,10 @@ func equalTemplates(i, j *template.Template) (bool, string, string) { } md := Data{ - Context: ctx, - Doc: make(map[string]string), - DocFlags: make(map[string]bool), - Styles: []string{"style1"}, - Scripts: []string{"js1"}, + Context: ctx, + Doc: make(map[string]interface{}), + Styles: []string{"style1"}, + Scripts: []string{"js1"}, } md.Doc["title"] = "some title" md.Doc["body"] = "some body" diff --git a/caddyhttp/markdown/template.go b/caddyhttp/markdown/template.go index 34a14747..5427dd1c 100644 --- a/caddyhttp/markdown/template.go +++ b/caddyhttp/markdown/template.go @@ -12,11 +12,10 @@ import ( // Data represents a markdown document. type Data struct { httpserver.Context - Doc map[string]string - DocFlags map[string]bool - Styles []string - Scripts []string - Files []FileInfo + Doc map[string]interface{} + Styles []string + Scripts []string + Files []FileInfo } // Include "overrides" the embedded httpserver.Context's Include() @@ -29,12 +28,11 @@ func (d Data) Include(filename string) (string, error) { // execTemplate executes a template given a requestPath, template, and metadata func execTemplate(c *Config, mdata metadata.Metadata, files []FileInfo, ctx httpserver.Context) ([]byte, error) { mdData := Data{ - Context: ctx, - Doc: mdata.Variables, - DocFlags: mdata.Flags, - Styles: c.Styles, - Scripts: c.Scripts, - Files: files, + Context: ctx, + Doc: mdata.Variables, + Styles: c.Styles, + Scripts: c.Scripts, + Files: files, } b := new(bytes.Buffer)