I'm having the following yaml , when I try to parse it I got error, any idea what could be missing here? im not sure how to struct the zone property.
This is the valid yaml
https://codebeautify.org/yaml-validator/cb42f23a
Error:
error in model extConfigYaml: *yaml: line 4: mapping values are not allowed in this context
type ExternalConfig struct {
Landscape zone `yaml:"Landscape"`
}
type zone struct {
zone models `yaml:"zone"`
}
type models struct {
models []Model `yaml:"models"`
}
type Model struct {
AppType string `yaml:"app-type"`
ServiceType string `yaml:"service-type"`
}
var external_config = []byte(`
Landscape:
zone: zone1
models:
- app-type: app1
service-type: GCP
- app-type: app2
service-type: AMAZON
zone: zone2
models:
- app-type: app3
service-type: AZURE
- app-type: app4Í
service-type: HEROKU
`)
extConfigYaml := ExternalConfig{}
err = yaml.Unmarshal([]byte(external_config), &extConfigYaml)
if err != nil {
log.Fatalf("error in model extConfigYaml: %v", err)
}
fmt.Printf("%+v
", extConfigYaml)
According to the error:
error in model extConfigYaml: *yaml: line 4: mapping values are not allowed in this context
There is an error in the indentation of your provided yaml content in which the models
indent level should match the zone
indent level. Along with that there is no need to create a struct for zone
since it is a string value but you have assigned it a model
struct which contains models
data. Also you should export the structs using uppercase letters.
Modify your code to the code below:
package main
import (
"fmt"
"log"
"gopkg.in/yaml.v2"
)
type ExternalConfig struct {
Landscape Zone `yaml:"Landscape"`
}
type Zone struct {
Zone string `yaml:"zone"`
Models []Model `yaml:"models"`
}
type Model struct {
AppType string `yaml:"app-type"`
ServiceType string `yaml:"service-type"`
}
var external_config = []byte(`
Landscape:
zone: zone1
models:
- app-type: app1
service-type: GCP
- app-type: app2
service-type: AMAZON
zone: zone2
models:
- app-type: app3
service-type: AZURE
- app-type: app4Í
service-type: HEROKU
`)
func main() {
extConfigYaml := ExternalConfig{}
err := yaml.Unmarshal(external_config, &extConfigYaml)
if err != nil {
log.Fatalln("error in model extConfigYaml: %v", err)
}
fmt.Printf("%+v
", extConfigYaml)
}
Working Code on Playground