Add CSV support to transform.Unmarshal

Fixes #5555
This commit is contained in:
Bjørn Erik Pedersen
2018-12-23 10:40:32 +01:00
parent 822dc627a1
commit a574469797
16 changed files with 238 additions and 44 deletions

View File

@@ -31,6 +31,7 @@ const (
JSON Format = "json"
TOML Format = "toml"
YAML Format = "yaml"
CSV Format = "csv"
)
// FormatFromString turns formatStr, typically a file extension without any ".",
@@ -51,6 +52,8 @@ func FormatFromString(formatStr string) Format {
return TOML
case "org":
return ORG
case "csv":
return CSV
}
return ""
@@ -88,11 +91,16 @@ func FormatFromFrontMatterType(typ pageparser.ItemType) Format {
// FormatFromContentString tries to detect the format (JSON, YAML or TOML)
// in the given string.
// It return an empty string if no format could be detected.
func FormatFromContentString(data string) Format {
func (d Decoder) FormatFromContentString(data string) Format {
csvIdx := strings.IndexRune(data, d.Comma)
jsonIdx := strings.Index(data, "{")
yamlIdx := strings.Index(data, ":")
tomlIdx := strings.Index(data, "=")
if isLowerIndexThan(csvIdx, jsonIdx, yamlIdx, tomlIdx) {
return CSV
}
if isLowerIndexThan(jsonIdx, yamlIdx, tomlIdx) {
return JSON
}