登录
首页 >  Golang >  Go问答

向 yaml 文件中添加新数据项的代码示例

来源:stackoverflow

时间:2024-02-17 18:42:24 366浏览 收藏

大家好,今天本人给大家带来文章《向 yaml 文件中添加新数据项的代码示例》,文中内容主要涉及到,如果你对Golang方面的知识点感兴趣,那就请各位朋友继续看下去吧~希望能真正帮到你们,谢谢!

问题内容

我有 yaml 文件,我需要使用 go 代码在运行时向其中添加数据 我的意思是,路径如下所示

这是一个 yaml 文件,在 snk_devsif 下有一个条目

spec:
  mec:
    tolerations:
    - effect: noschedule
      key: workgroup
      operator: equal
      value: goxy
    resources:
      requests:
        cpu: 100m
        memory: 1gi
    customconfig:
      sif:
        prom_exporter:
          type: prometheus_exporter
        snk_dev:   
          type: sk_hc_logs
          inputs:
            - tesslt
          ep: ${nt}
          dken: ${sn}
          encoding:
            codec: "json"
          index: us
          compression: gzip
          buffer:
            type: memory

在以下 yaml 路径下我需要添加一个新条目

spec->mec->customconfig->sif 一个新条目 snd_prd

snk_prod:   
      type: sk_hc_logs
      inputs:
        - tesslt
      ep: ${NT}
      dken: ${SN}
      encoding:
        codec: "json"
      index: us
      compression: gzip
      buffer:
        type: memory

我们正在使用 kustomize,我想知道是否有办法通过代码来完成它,我的意思是提前选择我需要添加的文件并在运行时添加它 或者也许使用 更好 https://github.com/go-yaml/yaml


正确答案


这里的关键是生成等效的 go 结构来对 yaml 进行建模并使用 gopkg.in/yaml.v3 包中的 marhshal/unmarshal 函数。

您可以使用 yaml-to-go 等工具来自动生成 yaml 所需的结构,然后在其之上执行更多其他自定义操作。下面的答案采用了此类工具的定义。

您的 yaml 结构可以稍微改进一下,因为 snk_devsnk_prod 字段看起来很相似。您应该为这两者定义一个通用类型,并定义一个 yaml 对象列表,这些对象又会转换为该特定类型的结构切片。但由于原始 yaml 将它们两个保留为不同的实体,因此您的结构也需要不同。

根据您 comment 的答案,字段 snk_devsnk_prod 是动态派生的,因此将您的 customconfig 定义为 map[string]interface{} 以允许动态键名称是有意义的。

package main

import (
    "fmt"
    "log"

    "gopkg.in/yaml.v3"
)

type yamldata struct {
    spec spec `yaml:"spec"`
}
type tolerations struct {
    effect   string `yaml:"effect"`
    key      string `yaml:"key"`
    operator string `yaml:"operator"`
    value    string `yaml:"value"`
}
type requests struct {
    cpu    string `yaml:"cpu"`
    memory string `yaml:"memory"`
}
type resources struct {
    requests requests `yaml:"requests"`
}
type promexporter struct {
    type string `yaml:"type"`
}
type encoding struct {
    codec string `yaml:"codec"`
}
type buffer struct {
    type string `yaml:"type"`
}
type sifconfig struct {
    type        string   `yaml:"type"`
    inputs      []string `yaml:"inputs"`
    ep          string   `yaml:"ep"`
    dken        string   `yaml:"dken"`
    encoding    encoding `yaml:"encoding"`
    index       string   `yaml:"index"`
    compression string   `yaml:"compression"`
    buffer      buffer   `yaml:"buffer"`
}
type customconfig struct {
    sif map[string]interface{} `yaml:"sif"`
}
type mec struct {
    tolerations  []tolerations `yaml:"tolerations"`
    resources    resources     `yaml:"resources"`
    customconfig customconfig  `yaml:"customconfig"`
}
type spec struct {
    mec mec `yaml:"mec"`
}

var data = `spec:
  mec:
    tolerations:
    - effect: noschedule
      key: workgroup
      operator: equal
      value: goxy
    resources:
      requests:
        cpu: 100m
        memory: 1gi
    customconfig:
      sif:
        prom_exporter:
          type: prometheus_exporter
        snk_dev:   
          type: sk_hc_logs
          inputs:
            - tesslt
          ep: ${nt}
          dken: ${sn}
          encoding:
            codec: "json"
          index: us
          compression: gzip
          buffer:
            type: memory
`

func main() {
    t := yamldata{}
    err := yaml.unmarshal([]byte(data), &t)
    if err != nil {
        log.fatalf("error: %v", err)
    }

    config := &t.spec.mec.customconfig
    config.sif["snk_prod"] = sifconfig{
        type:        "sk_hc_logs",
        inputs:      []string{"tesslt"},
        ep:          "${nt}",
        dken:        "${sn}",
        encoding:    encoding{codec: "json"},
        index:       "us",
        compression: "gzip",
        buffer:      buffer{type: "memory"},
    }

    yamlbytes, err := yaml.marshal(t)
    if err != nil {
        log.fatalf("error: %v", err)
    }
    fmt.println(string(yamlbytes))

}

yamlbytes 可以进一步用作单独的文件,该文件被排除在上面。

Go playground

yaml 编解码器支持解码为 map[string]any 并将此类映射编码为 yaml。

这个想法是解码文档和额外的树,然后将额外的映射放在所需的路径下,然后编码回来。

type yamlobject map[string]any

func main() {
    // parse the initial document
    doc := make(yamlobject)
    yaml.unmarshal([]byte(document), &doc)
    // parse the additional nodes
    addon := make(yamlobject)
    yaml.unmarshal([]byte(extra), &addon)
    // find the node by the path
    node := findchild(doc, "spec", "mec", "customconfig", "sif")
    if node == nil {
        panic("must not happen")
    }
    // add the keys from the additional document
    // under the specified path
    for key, val := range addon {
        (*node)[key] = val
    }
    // output the modified document
    outdoc, _ := yaml.marshal(doc)
    println(string(outdoc))
}

func findchild(obj yamlobject, path ...string) *yamlobject {
    if len(path) == 0 {
        return &obj
    }
    key := path[0]
    child, ok := obj[key]
    if !ok {
        return nil
    }
    obj, ok = child.(yamlobject)
    if !ok {
        return nil
    }
    return findchild(obj, path[1:]...)
}

完整示例 https://go.dev/play/p/pTdXR53p0mq

输出:

spec:
    mec:
        customConfig:
            sif:
                prom_exporter:
                    type: prometheus_exporter
                snk_dev:
                    buffer:
                        type: memory
                    compression: gzip
                    dken: ${SN}
                    encoding:
                        codec: json
                    ep: ${NT}
                    index: us
                    inputs:
                        - tesslt
                    type: sk_hc_logs
                snk_prod:
                    buffer:
                        type: memory
                    compression: gzip
                    dken: ${SN}
                    encoding:
                        codec: json
                    ep: ${NT}
                    index: us
                    inputs:
                        - tesslt
                    type: sk_hc_logs
        resources:
            requests:
                cpu: 100m
                memory: 1Gi
        tolerations:
            - effect: NoSchedule
              key: WorkGroup
              operator: Equal
              value: goxy

yaml 编解码器按字母顺序输出键

今天关于《向 yaml 文件中添加新数据项的代码示例》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>