【问题标题】:yaml file with list of data带有数据列表的 yaml 文件
【发布时间】:2020-08-28 18:34:46
【问题描述】:

全部,

我确信这很容易,但有点挣扎 - 尝试编写一个 yaml 文件,该文件将在使用 gopkg.in/yaml.v3 的 Go 程序中使用。我需要定义服务器列表及其相关元数据。在 JSON 中这是一个相当简单的过程,它在 yaml 文件中是如何处理的。

Go 代码结构如下。

type Config struct {
    Servers struct {
        Servers struct {
            ServerType string `yaml:"serverType"`
            ServerPort int `yaml:"serverPort"`
            Auth struct {
                AuthType string `yaml:"auth"`
                TLSKey  string `yaml:"tls"`
            } `yaml:"auth"`
        }`yaml:"server"`
    } `yaml:"Servers"`
}

yaml文件长这样

Servers:
  server:
    serverType: production
    serverPort: 80
    auth:
      auth: no
      tls:
  server:
    serverType: test
    serverPort: 8080
    auth:
      auth: no
      tls:

我确定我遗漏了一些相当明显的东西 - 有什么智慧的话可以帮助我前进吗?

谢谢

【问题讨论】:

    标签: go yaml


    【解决方案1】:

    这是一个很好的在线资源,可以帮助您了解 YAML 配置以及如何生成兼容的 Go 结构:https://yaml.to-go.online/

    如果您想要一个服务器列表(切片) - 并且不需要通过键名查找映射 - 然后从这个 YAML 开始:

    Servers:
      - serverType: production
        serverPort: 80
        auth:
          auth: no
          tls:
      - serverType: test
        serverPort: 8080
        auth:
          auth: no
          tls:
    

    并使用上面的在线资源,产生这个结构:

    type AutoGenerated struct {
        Servers []struct {
            ServerType string `yaml:"serverType"`
            ServerPort int    `yaml:"serverPort"`
            Auth       struct {
                Auth string      `yaml:"auth"`
                TLS  interface{} `yaml:"tls"`
            } `yaml:"auth"`
        } `yaml:"Servers"`
    }
    

    https://play.golang.org/p/726afn_I826


    如果您希望能够按名称(即地图)索引服务器配置,那么可能是这个 YAML 架构:

    Servers:
      production:
        serverPort: 80
        auth:
          auth: no
          tls:
      test:
        serverPort: 8080
        auth:
          auth: no
          tls:
    

    还有这个手工结构:

    type Config struct {
        Servers map[string]struct {
            ServerPort int `yaml:"serverPort"`
            Auth       struct {
                Auth string      `yaml:"auth"`
                TLS  interface{} `yaml:"tls"`
            } `yaml:"auth"`
        } `yaml:"Servers"`
    }
    

    https://play.golang.org/p/Cmvo0jxfZkd

    【讨论】:

    • 完美,我认为这很容易 - 我错过了...谢谢
    【解决方案2】:

    看起来您需要一组服务器。您不能在一个对象下重复相同的键:

    Servers:
       - serverType: production
         ...
       - serverType: test
    

    然后更改结构以匹配此:

    type Config struct {
        Servers []struct {
                ServerType string `yaml:"serverType"`
                ServerPort int `yaml:"serverPort"`
                Auth struct {
                    AuthType string `yaml:"auth"`
                    TLSKey  string `yaml:"tls"`
                } `yaml:"auth"`
        } `yaml:"Servers"`
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-28
      • 2020-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-09
      • 1970-01-01
      相关资源
      最近更新 更多