Skip to content

Instantly share code, notes, and snippets.

@davidejones
Last active April 24, 2019 15:39
Show Gist options
  • Save davidejones/f723cd75686fb7d7032449623c087f1e to your computer and use it in GitHub Desktop.
Save davidejones/f723cd75686fb7d7032449623c087f1e to your computer and use it in GitHub Desktop.
yaml go nested list

both js-yaml and PyYAML will convert

team:
  -
    - bio: this is a test
      name: another test
    - bio: second item
      name: foo bar 2
  -
    - bio: another
      name: another

to this

team:
  - - bio: this is a test
      name: another test
    - bio: second item
      name: foo bar 2
  - - bio: another
      name: another

In Hugo it appears to be having issues reading the data file when - - appears but has no issue when its spaced out and there is a single - on a line I tested a simple go script and it seemed to be able to parse both formats ok so i'm not sure why hugo doesn't like it

package main
import (
"fmt"
"log"
"gopkg.in/yaml.v2"
)
var data = `
team:
-
- bio: this is a test
name: another test
- bio: second item
name: foo bar 2
-
- bio: another
name: another
`
var data2 = `
team:
- - bio: this is a test
name: another test
- bio: second item
name: foo bar 2
- - bio: another
name: another
`
func main() {
m := make(map[string]interface{})
err := yaml.Unmarshal([]byte(data), &m)
if err != nil {
log.Fatalf("error: %v", err)
}
fmt.Printf("--- m:\n%v\n\n", m)
err = yaml.Unmarshal([]byte(data2), &m)
if err != nil {
log.Fatalf("error: %v", err)
}
fmt.Printf("--- m:\n%v\n\n", m)
}
--- m:
map[team:[[map[bio:this is a test name:another test] map[bio:second item name:foo bar 2]] [map[bio:another name:another]]]]
--- m:
map[team:[[map[bio:this is a test name:another test] map[name:foo bar 2 bio:second item]] [map[bio:another name:another]]]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment