Adding support for more front matter types (int, float & date)

This commit is contained in:
spf13 2013-10-24 14:31:04 -07:00
parent 3558e3d6f0
commit f3c816eabd
3 changed files with 84 additions and 4 deletions

View file

@ -70,10 +70,40 @@ func interfaceArrayToStringArray(i interface{}) []string {
return a
}
func interfaceToFloat64(i interface{}) float64 {
switch s := i.(type) {
case float64:
return s
case float32:
return float64(s)
case string:
v, err := strconv.ParseFloat(s, 64)
if err == nil {
return float64(v)
} else {
errorf("Only Floats are supported for this key\nErr:", err)
}
default:
errorf("Only Floats are supported for this key")
}
return 0.0
}
func interfaceToInt(i interface{}) int {
switch s := i.(type) {
case int:
return s
case int64:
return int(s)
case int32:
return int(s)
case int16:
return int(s)
case int8:
return int(s)
case string:
v, err := strconv.ParseInt(s, 0, 0)
if err == nil {
@ -88,12 +118,23 @@ func interfaceToInt(i interface{}) int {
return 0
}
func interfaceToTime(i interface{}) time.Time {
switch s := i.(type) {
case time.Time:
return s
default:
errorf("Only Time is supported for this key")
}
return *new(time.Time)
}
func interfaceToString(i interface{}) string {
switch s := i.(type) {
case string:
return s
default:
errorf("Only Strings are supported for this YAML key")
errorf("Only Strings are supported for this key")
}
return ""

View file

@ -330,7 +330,8 @@ func (page *Page) update(f interface{}) error {
m := f.(map[string]interface{})
for k, v := range m {
switch strings.ToLower(k) {
loki := strings.ToLower(k)
switch loki {
case "title":
page.Title = interfaceToString(v)
case "description":
@ -368,8 +369,14 @@ func (page *Page) update(f interface{}) error {
default:
// If not one of the explicit values, store in Params
switch vv := v.(type) {
case string: // handle string values
page.Params[strings.ToLower(k)] = vv
case string:
page.Params[loki] = vv
case int64, int32, int16, int8, int:
page.Params[loki] = vv
case float64, float32:
page.Params[loki] = vv
case time.Time:
page.Params[loki] = vv
default: // handle array of strings as well
switch vvv := vv.(type) {
case []interface{}:
@ -396,6 +403,12 @@ func (page *Page) GetParam(key string) interface{} {
switch v.(type) {
case string:
return interfaceToString(v)
case int64, int32, int16, int8, int:
return interfaceToInt(v)
case float64, float32:
return interfaceToFloat64(v)
case time.Time:
return interfaceToTime(v)
case []string:
return v
}

View file

@ -141,6 +141,14 @@ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
`
)
var PAGE_WITH_VARIOUS_FRONTMATTER_TYPES = `+++
a_string = "bar"
an_integer = 1
a_float = 1.3
a_date = 1979-05-27T07:32:00Z
+++
Front Matter with various frontmatter types`
func checkError(t *testing.T, err error, expected string) {
if err == nil {
t.Fatalf("err is nil. Expected: %s", expected)
@ -332,6 +340,24 @@ func TestShouldRenderContent(t *testing.T) {
}
}
func TestDifferentFrontMatterVarTypes(t *testing.T) {
page, _ := ReadFrom(strings.NewReader(PAGE_WITH_VARIOUS_FRONTMATTER_TYPES), "test/file1.md")
dateval, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z")
if page.GetParam("a_string") != "bar" {
t.Errorf("frontmatter not handling strings correctly should be %s, got: %s", "bar", page.GetParam("a_string"))
}
if page.GetParam("an_integer") != 1 {
t.Errorf("frontmatter not handling ints correctly should be %s, got: %s", "1", page.GetParam("an_integer"))
}
if page.GetParam("a_float") != 1.3 {
t.Errorf("frontmatter not handling floats correctly should be %s, got: %s", 1.3, page.GetParam("a_float"))
}
if page.GetParam("a_date") != dateval {
t.Errorf("frontmatter not handling dates correctly should be %s, got: %s", dateval, page.GetParam("a_date"))
}
}
func TestDegenerateInvalidFrontMatterLeadingWhitespace(t *testing.T) {
_, err := ReadFrom(strings.NewReader(INVALID_FRONT_MATTER_LEADING_WS), "invalid/front/matter/leading/ws")
if err != nil {