dongsuo0517 2017-12-20 13:10
浏览 185
已采纳

解析Yaml以使用动态字段进行结构化

I use the following code to parse yaml to struct which works okay. Now let's assume that I have a struct like install which I know to have two const properties like Name and Group but in addition we can have additional key val properties which could change, you can get any key val properties (dynamic)

How should I define this struct? the idea is to read the yaml file modify some values and write it back (with exact same structure with modified value) to FS, therefore I don't want to miss some dynamically fields which could be in the some yaml file which need to be modified

package main

import (
    "fmt"
    "log"

    "github.com/go-yaml/yaml"
)

type File struct {
    TypeVersion string `yaml:"_type-version"`
    Dependency []Dependency
}

type Dependency struct {
    Name     string
    Type     string
    CWD      string
    Install  []Install
    Requires []Requires
}

type Install struct {
    Name  string
    Group string
   //Here any key value can be
}

type Requires struct {
    Name string
    Type string
}

var data = `
_type-version: "1.0.0"
dependency:
  - name: ui
    type: runner
    cwd: /ui
    install:
       - name: api
         group: test
    requires:
      - name: db
      - type: mongo
      - name: rst
      - name: test
      - name: test2
`

func main() {
    f := File{}

    err := yaml.Unmarshal([]byte(data), &f)
    if err != nil {
        log.Fatalf("error: %v", err)
    }
    fmt.Printf("--- t:
%v

", f)

    d, err := yaml.Marshal(&f)
    if err != nil {
        log.Fatalf("error: %v", err)
    }
    fmt.Printf("--- t dump:
%s

", string(d))
}

Example Install can be like above and also like this

    install:
       - name: api
         group: test
         a1:test2
         b1:test2

And also

   install:
           - name: api
             group: test
             z10:123
             zzz:111

And many more fields after name and group

  • 写回答

1条回答 默认 最新

  • douken7402 2017-12-20 14:06
    关注

    Map instead of a struct for Install will help to solve the problem.

    import (
        "fmt"
        "log"
    
        "github.com/go-yaml/yaml"
    )
    
    type File struct {
        TypeVersion string `yaml:"_type-version"`
        Dependency  []Dependency
    }
    
    type Dependency struct {
        Name     string
        Type     string
        CWD      string
        Install  []Install
        Requires []Requires
    }
    
    type Install map[string]string
    
    func (i Install) name() string {
        return i["name"]
    }
    
    func (i Install) group() string {
        return i["group"]
    }
    
    type Requires struct {
        Name string
        Type string
    }
    
    var data = `
    _type-version: "1.0.0"
    dependency:
      - name: ui
        type: runner
        cwd: /ui
        install:
           - name: api
             group: test
        requires:
          - name: db
          - type: mongo
          - name: rst
          - name: test
          - name: test2
    `
    
    func main() {
        f := File{}
    
        err := yaml.Unmarshal([]byte(data), &f)
        if err != nil {
            log.Fatalf("error: %v", err)
        }
        fmt.Printf("--- t:
    %v
    
    ", f)
    
        d, err := yaml.Marshal(&f)
        if err != nil {
            log.Fatalf("error: %v", err)
        }
        fmt.Printf("--- t dump:
    %s
    
    ", string(d))
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动