Reputation: 11
So I'm looking to parse a yaml file for my job, and being fairly new to Golang I am a bit stuck... Im used to being on python all the time so some concepts are still dragging in my head for Go. But I have the following issue ...
So I have this yaml file I am trying to parse and Im running the following ... I created the go struct using yaml-to-go. So I have this ..
type Application struct {
Environments []struct {
Name string `yaml:"name"`
RulesEngineTargets []string `yaml:"rulesEngineTargets,omitempty"`
Zones []struct {
Name string `yaml:"name"`
CloudURI string `yaml:"cloudURI"`
Deployments []struct {
Name string `yaml:"name"`
VipTemplate string `yaml:"vipTemplate"`
} `yaml:"deployments"`
} `yaml:"zones"`
} `yaml:"environments"`
Apps []struct {
Name string `yaml:"name"`
Path string `yaml:"path"`
Route string `yaml:"route"`
NumDeploys int `yaml:"numDeploys"`
EcpVersion string `yaml:"ecpVersion,omitempty"`
ExcludeZones []string `yaml:"excludeZones,omitempty"`
EpaasFile string `yaml:"epaasFile,omitempty"`
Version struct {
Path string `yaml:"path"`
JSONPath string `yaml:"jsonPath"`
} `yaml:"version,omitempty"`
Service string `yaml:"service,omitempty"`
} `yaml:"apps"`
}
And I keep getting this error
line 231: cannot unmarshal !!str `m3-TEST1...` into []string
line 239: cannot unmarshal !!str `m3-TEST2...` into []string
line 247: cannot unmarshal !!str `m3-TEST2...` into []string
line 260: cannot unmarshal !!str `m3-TEST1...` into []string
The error seems to be coming from the excludedZones field in the yaml ..
rulesEngineTargets: [m2-rules-eng.test.com, m4-rules-eng.test2.test.com]
zones:
- name: m3-TEST1-zone1
cloudURI: m3-Test1.com
deployments:
- name: m3-TEST2-zone1-green
vipTemplate: "{{.route}}-green1.test.com"
- name: m3-TEST1-zone1-blue
vipTemplate: "{{.route}}-blue1.test.com"
What am I missing in my code to make this work right? Im fairly lost, is it something wrong with the parsing on the '-' character?
Here is my full code
package main
import (
"fmt"
"gopkg.in/yaml.v3"
"log"
"os"
)
type Application struct {
Environments []struct {
Name string `yaml:"name"`
RulesEngineTargets []string `yaml:"rulesEngineTargets,omitempty"`
Zones []struct {
Name string `yaml:"name"`
CloudURI string `yaml:"cloudURI"`
Deployments []struct {
Name string `yaml:"name"`
VipTemplate string `yaml:"vipTemplate"`
} `yaml:"deployments"`
} `yaml:"zones"`
} `yaml:"environments"`
Apps []struct {
Name string `yaml:"name"`
Path string `yaml:"path"`
Route string `yaml:"route"`
NumDeploys int `yaml:"numDeploys"`
EcpVersion string `yaml:"ecpVersion,omitempty"`
ExcludeZones []string `yaml:"excludeZones,omitempty"`
EpaasFile string `yaml:"epaasFile,omitempty"`
Version struct {
Path string `yaml:"path"`
JSONPath string `yaml:"jsonPath"`
} `yaml:"version,omitempty"`
Service string `yaml:"service,omitempty"`
} `yaml:"apps"`
}
func main() {
file, err := os.ReadFile("r42.yaml")
if err != nil {
log.Println("action failed: ", err)
return
}
var apps Application
err = yaml.Unmarshal(file, &apps)
if err != nil {
log.Println("action failed: ", err)
return
}
fmt.Printf("Here are the Results: %+v\\n", apps)
//for i := 0; i < len(apps.Apps); i++ {
// fmt.Println(apps.Apps[i].Name)
// fmt.Println(apps.Apps[i].Route)
}
Upvotes: 1
Views: 262