shithub: hugo

Download patch

ref: 4402c077754991df19c3bbab0c4a671dcfdc192c
parent: 4743de0d3c7564fc06972074e903d5502d204353
author: Vas Sudanagunta <[email protected]>
date: Thu Feb 1 20:35:26 EST 2018

Fix JSON array-based data file handling regression

This bug was introduced in Hugo 0.35.

Fixes #4361

--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -804,7 +804,7 @@
 
 	data, err := s.readData(r)
 	if err != nil {
-		s.Log.WARN.Printf("Failed to read data from %s: %s", filepath.Join(r.Path(), r.LogicalName()), err)
+		s.Log.ERROR.Printf("Failed to read data from %s: %s", filepath.Join(r.Path(), r.LogicalName()), err)
 		return nil
 	}
 
@@ -846,7 +846,7 @@
 	case "yaml", "yml":
 		return parser.HandleYAMLMetaData(content)
 	case "json":
-		return parser.HandleJSONMetaData(content)
+		return parser.HandleJSONData(content)
 	case "toml":
 		return parser.HandleTOMLMetaData(content)
 	default:
--- a/parser/frontmatter.go
+++ b/parser/frontmatter.go
@@ -207,14 +207,30 @@
 // HandleJSONMetaData unmarshals JSON-encoded datum and returns a Go interface
 // representing the encoded data structure.
 func HandleJSONMetaData(datum []byte) (map[string]interface{}, error) {
+	m := make(map[string]interface{})
+
 	if datum == nil {
 		// Package json returns on error on nil input.
 		// Return an empty map to be consistent with our other supported
 		// formats.
+		return m, nil
+	}
+
+	err := json.Unmarshal(datum, &m)
+	return m, err
+}
+
+// HandleJSONData unmarshals JSON-encoded datum and returns a Go interface
+// representing the encoded data structure.
+func HandleJSONData(datum []byte) (interface{}, error) {
+	if datum == nil {
+		// Package json returns on error on nil input.
+		// Return an empty map to be consistent with our other supported
+		// formats.
 		return make(map[string]interface{}), nil
 	}
 
-	var f map[string]interface{}
+	var f interface{}
 	err := json.Unmarshal(datum, &f)
 	return f, err
 }