Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load a dynamic yaml structure in Go [closed]

Tags:

dynamic

yaml

go

I'm trying to convert some Python code to Golang, and I'm having some difficulty figuring out how to load dynamic yaml data, which I thought was pretty basic. So far, all of the approaches I have found mention creating a Struct and mapping values, but this won't be possible since the data I'll be receiving will be different every time it's read.

This isn't the real data (which will actually be returned from an API), but as an example yaml file:

[ ~]$ cat /tmp/example.yaml
Massachusetts:
  cities:
    - name: 'Boston'
      area_code: 617
    - name: 'Springfield'
    - name: 'Worcester'
Virginia:
  cities:
    - name: 'Richmond'
    - name: 'Arlington'
      landmarks:
        - 'The Pentagon'
        - 'National Airport'
        - 'Arlington National Cemetary'
  presidents:
    - 'George Washington'
    - 'Thomas Jefferson'
    - 'James Madison'
    - 'James Monroe'
    - 'William Henry Harrison'
    - 'John Tyler'
Missouri:
  rivers:
    - 'Missouri River'
    - 'Mississippi'
    - 'Arkansas River'
    - 'White River'

And reading and manipulating it in Python is simple:

#!/usr/bin/python
import yaml
with open('/tmp/example.yaml', 'r') as fh:
    data = yaml.load(fh)

print yaml.dump(data, default_flow_style=False)

As I'm new to Go, does anyone know which technique I should use / documentation I should look for that will do what this Python code does?

like image 271
Jeremy Koppel Avatar asked May 10 '18 15:05

Jeremy Koppel


1 Answers

One of the most popular go yaml packages has this exact example in their documentation:

package main

import (
        "fmt"
        "log"

        "gopkg.in/yaml.v2"
)

var data = `
a: Easy!
b:
  c: 2
  d: [3, 4]
`

func main() {
     m := make(map[interface{}]interface{})

     err := yaml.Unmarshal([]byte(data), &m)
     if err != nil {
         log.Fatalf("error: %v", err)
     }
     fmt.Printf("--- m:\n%v\n\n", m)
}

As Flimzy points out above in his comment, now it is up to your app to dynamically handle the schema. I feel like this may qualify as "Schema on Read" and there are many tradeoffs to this approach vs an approach where you use a static definition of the data:

  • type assertions/inspection must be handled at runtime, after unmarshalling
  • Schema must be codified in logic and assertions instead of declared
  • ??

I would def question if you truly have dynamic data?

like image 93
dm03514 Avatar answered Oct 18 '22 01:10

dm03514