【问题标题】:Convert struct to YAML file golang, how to avoid empty quote in yaml output?将struct转换为YAML文件golang,如何避免yaml输出中的空引号?
【发布时间】:2020-04-28 15:12:13
【问题描述】:

我正在尝试使用 struct 生成以下 YAML,

预期输出:

all:
  hosts:
  children:
    master:
      hosts:
// This is dynamic data coming from the slice.
        192.168.99.123:
        192.168.99.125:
    worker:
      hosts:
        192.168.99.123:
        192.168.99.125:
    etcd:
      hosts:
        192.168.99.123:
        192.168.99.125:
  vars:
    ansible_user: vagrant
    ansible_ssh_private_key_file: "~/.ssh/id_rsa"

我的golang结构如下,

type Inventory struct {
    All *AnsibleInventory `json:"all"`
}

// AnsibleInventory defines ansible inventory file struct
type AnsibleInventory struct {
    Hosts    string     `json:"hosts"`
    Children *HostGroup `json:"children"`
    Vars     *Vars      `json:"vars"`
}

type HostGroup struct {
    Master *Hosts `json:"master"`
    Worker *Hosts `json:"worker"`
    Etcd   *Hosts `json:"etcd"`
}

type Hosts struct {
    Hosts map[string]string `json:"hosts,omitempty"`
}

type Vars struct {
    User              string `json:"ansible_user"`
    SshPrivateKeyFile string `json:"ansible_ssh_private_key_file"`
}

我正在初始化结构如下,

    elementMap := make(map[string]string)
    for _, ip := range p.MasterIPs {
        elementMap[ip] = "" // I tried using slice this is definitely not expected 
    }

    ansibleInventory := &Inventory{
        &AnsibleInventory{Children: &HostGroup{
            Master: &Hosts{
                elementMap,
            },
            Worker: &Hosts{
                elementMap,
            },
            Etcd: &Hosts{
                elementMap,
            },
        }, Vars: &Vars{
            User:              p.Username,
            SshPrivateKeyFile: p.PrivateKeyPath,
        }}}

      b, err := yaml.Marshal(ansibleInventory)

      filename:= "/tmp/filename"

    _, err = os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0644)
    err = ioutil.WriteFile(filename, b, 0644)

产生的输出是这样的,

all:
  children:
    etcd:
      hosts:
// I want to avoid this empty quote 
        192.168.99.123: ""
        192.168.99.125: ""
    master:
      hosts:
        192.168.99.123: ""
        192.168.99.125: ""
    worker:
      hosts:
        192.168.99.123: ""
        192.168.99.125: ""
// Same here aovid this empty quote
  hosts: ""
  vars:
    ansible_ssh_private_key_file: ~/.ssh/id_rsa
    ansible_user: vagrant

【问题讨论】:

  • yourbasic.org/golang/sort-map-keys-values 如果您的问题是对孩子进行排序。否则请描述究竟是什么不正确
  • 检查 p.MasterIPs 是否有回车(?表示它的 complex mapping key 和 | 是 literal style,其中换行符很重要)
  • 我将您的代码示例放入工作框架here;它产生的输出与您声称的完全不同。也许您应该提供一个更完整的复制器...?
  • 向 torek 的example 添加一个carrage return 使其产生类似于问题发布的输出。
  • 谢谢@torek 和@Brits 回车导致产生额外line and ? 元素的问题。现在我看到了192.168.99.123: "" 格式的输出,知道如何避免额外的引用。如果我直接使用 slice 而不是 map 它的生产列表。

标签: go struct


【解决方案1】:

您需要在每个结构中指定yaml 标签而不是json

type Inventory struct {
    All *AnsibleInventory `yaml:"all"` // not `json:"all"`
}

【讨论】:

  • 抱歉结果是一样的
  • 尽量不要忽略err's,你可能会弄清楚为什么它不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-13
  • 1970-01-01
  • 1970-01-01
  • 2018-12-04
  • 2021-11-26
  • 2011-06-22
相关资源
最近更新 更多