dongyingtang3803 2018-03-27 15:11
浏览 721
已采纳

解析yaml返回空对象

I have the following yaml which I need to parse to struct. In the builds property I got empty value while debug, what am I missing here?

I use "gopkg.in/yaml.v2"

- name: srv
    type: java
    path: srv
    builds:
     - name: db
        properties:
           JBR_CONFIG_RESOURCE_CONFIG: '[META-INF/context.xml:
              {"service_name" : "~{h-container}"}]'
           TEST2: aaaa

The struct is

type Runs struct {
    Name       string
    Type       string
    Path       string     `yaml:"path,omitempty"`
    Builds   []Builds `yaml:”builds,omitempty"`
}

type Builds struct {
    Name       string     `yaml:"name,omitempty"`
    Properties Properties `yaml:"properties,omitempty"`
}

type Properties map[string]string
  • 写回答

2条回答 默认 最新

  • dsf5632 2018-03-27 16:28
    关注

    Properly formated yaml is the first thing that you should consider. If u wanna have one Runs you should have your yaml formated something like that

    name: srv
    builds:
      -
        name: db
        properties:
          JBR_CONFIG_RESOURCE_CONFIG: "[META-INF/context.xml:
          {\"service_name\" : \"~{h-container}\"}]"
          TEST2: aaaa
    path: srv
    type: java
    

    But then i u wanna have more of this object you need to group them in one parameter. It can look like this

    runs:
      -
        name: srv
        builds:
          -
            name: db
            properties:
              JBR_CONFIG_RESOURCE_CONFIG: "[META-INF/context.xml:
              {\"service_name\" : \"~{h-container}\"}]"
              TEST2: aaaa
        path: srv
        type: java
      -
        name: srv2
        builds:
          -
            name: db2
            properties:
              JBR_CONFIG_RESOURCE_CONFIG: "[META-INF/context.xml:
              {\"service_name\" : \"~{h-container}\"}]"
              TEST2: aaaa2
        path: srv2
        type: java2
    

    And then in your code could look like this

    package main
    
    import (
        "fmt"
        "gopkg.in/yaml.v2"
        "io/ioutil"
        "log"
        "os"
    )
    
    type Document struct {
        Runs []Runs `yaml:"runs,omitempty"`
    }
    type Runs struct {
        Name   string   `yaml:"name,omitempty"`
        Type   string   `yaml:"type,omitempty"`
        Path   string   `yaml:"path,omitempty"`
        Builds []Builds `yaml:"builds,omitempty"`
    }
    
    type Builds struct {
        Name       string            `yaml:"name,omitempty"`
        Properties map[string]string `yaml:"properties,omitempty"`
    }
    
    func main() {
    
        var document Document
        reader, err := os.Open("demo.yml")
        if err != nil {
            log.Fatal(err)
        }
        buf, _ := ioutil.ReadAll(reader)
        yaml.Unmarshal(buf, &document)
        if err := yaml.Unmarshal(buf, &document); err != nil {
            fmt.Print(err)
            os.Exit(1)
        }
        fmt.Println(document)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services