dr897777 2018-08-13 15:20
浏览 206
已采纳

解析yaml结构错误

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)
  • 写回答

1条回答 默认 最新

  • duanci19881003 2018-08-13 15:58
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊