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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号