The user with no hat
The user with no hat

Reputation: 10846

How to unmarshal YAML in Go

I have a hard time to unmarshal this piece of YAML in Go. The error I'm getting is cannot unmarshal !!seq into map[string][]map[string][]string. I've tried all kind of maps with no success (map[string]string ; []map[string]string and so on)

import (
    "gopkg.in/yaml.v1"
    "io/ioutil"
)

type AppYAML struct {
    Runtime       string                           `yaml:"runtime,omitempty"`
    Handlers      map[string][]map[string][]string `yaml:"handlers,omitempty"`
    Env_Variables map[string]string                `yaml:"env_variables,omitempty"`
}

func main() {
    s := `
runtime: go
handlers:
- url: /.*
  runtime: _go_app
  secure: always
env_variables:
  something: 'test'
`

    var a AppYAML
    if err = yaml.Unmarshal([]byte(s), &a); err != nil {
        log.Error(err)
        return
    }
}

Upvotes: 3

Views: 11578

Answers (1)

ferhatelmas
ferhatelmas

Reputation: 3978

Change type declaration to this:

type AppYAML struct {
    Runtime       string              `yaml:"runtime,omitempty"`
    Handlers      []map[string]string `yaml:"handlers,omitempty"`
    Env_Variables map[string]string   `yaml:"env_variables,omitempty"`
}

Upvotes: 10

Related Questions