dongwuxie5112 2015-08-21 18:41
浏览 236
已采纳

如何在Golang中使用动态键解析YAML

I am trying to parse a YAML file with Go. The problem is that the keys in the YAML file might not always be the same. This is to do API versioning so the user can define the versions they support. For instance V1, V2, V3 etc. They do not need to be in order and can omit versions they don't support i.e, V0, V2, V5 etc.

package main

import (
  "fmt"
  "gopkg.in/yaml.v2"
)

var data = `
---
development:
  skip-header-validation: true
  V1:
    current: "1.0.0"
    mime_types:
      - application/vnd.company.jk.identity+json;
      - application/vnd.company.jk.user+json;
      - application/vnd.company.jk.role+json;
      - application/vnd.company.jk.scope+json;
      - application/vnd.company.jk.test+json;
    skip-mime-type-validation: true
    skip-version-validation: true
  V2:
    current: "2.0.0"
    mime_types:
      - application/vnd.company.jk.identity+json;
      - application/vnd.company.jk.user+json;
      - application/vnd.company.jk.role+json;
      - application/vnd.company.jk.scope+json;
      - application/vnd.company.jk.test+json;

`

type MajorVersion struct {
  Current                 string    `yaml:"current"`
  MimeTypes               []string  `yaml:"mime_types"`
  SkipVersionValidation   bool      `yaml:"skip-version-validation"`
  SkipMimeTypeValidation  bool      `yaml:"skip-mime-type-validation"`
}

type Environment struct {
  SkipHeaderValidation  bool        `yaml:"skip-header-validation"`
  Version               map[string]MajorVersion
}

func main() {
  e := Environment{}

  yaml.Unmarshal([]byte(data), &e)
  fmt.Println(e)
}

I saw a similar question asked here

This is at the top level and I haven't quite figured out how to do this from inside the struct.

  • 写回答

1条回答 默认 最新

  • douwen3836 2015-08-22 15:37
    关注

    First, you are trying to parse the root as an Environment, but its actual type is map[string]Environment. Second, you are going to need a custom Unmarshaler if you want to keep that type structure. Something like this:

    package main
    
    import (
        "fmt"
    
        "gopkg.in/yaml.v2"
    )
    
    var data = `
    ---
    development:
      skip-header-validation: true
      V1:
        current: "1.0.0"
        mime_types:
          - application/vnd.company.jk.identity+json;
          - application/vnd.company.jk.user+json;
          - application/vnd.company.jk.role+json;
          - application/vnd.company.jk.scope+json;
          - application/vnd.company.jk.test+json;
        skip-mime-type-validation: true
        skip-version-validation: true
      V2:
        current: "2.0.0"
        mime_types:
          - application/vnd.company.jk.identity+json;
          - application/vnd.company.jk.user+json;
          - application/vnd.company.jk.role+json;
          - application/vnd.company.jk.scope+json;
          - application/vnd.company.jk.test+json;
    
    `
    
    type MajorVersion struct {
        Current                string   `yaml:"current"`
        MimeTypes              []string `yaml:"mime_types"`
        SkipVersionValidation  bool     `yaml:"skip-version-validation"`
        SkipMimeTypeValidation bool     `yaml:"skip-mime-type-validation"`
    }
    
    type Environment struct {
        SkipHeaderValidation bool
        Versions             map[string]MajorVersion
    }
    
    func (e *Environment) UnmarshalYAML(unmarshal func(interface{}) error) error {
        var params struct {
            SkipHeaderValidation bool `yaml:"skip-header-validation"`
        }
        if err := unmarshal(&params); err != nil {
            return err
        }
        var versions map[string]MajorVersion
        if err := unmarshal(&versions); err != nil {
            // Here we expect an error because a boolean cannot be converted to a
            // a MajorVersion
            if _, ok := err.(*yaml.TypeError); !ok {
                return err
            }
        }
        e.SkipHeaderValidation = params.SkipHeaderValidation
        e.Versions = versions
        return nil
    }
    
    func main() {
        var e map[string]Environment
        if err := yaml.Unmarshal([]byte(data), &e); err != nil {
            fmt.Println(err.Error())
        }
        fmt.Printf("%#v
    ", e)
    }
    

    Output (after using makeup):

    map[string]main.Environment{
        "development": {
            SkipHeaderValidation: true,
            Versions:             {
                "V2": {
                    Current:                "2.0.0",
                    MimeTypes:              {"application/vnd.company.jk.identity+json;", "application/vnd.company.jk.user+json;", "application/vnd.company.jk.role+json;", "application/vnd.company.jk.scope+json;", "application/vnd.company.jk.test+json;"},
                    SkipVersionValidation:  false,
                    SkipMimeTypeValidation: false,
                },
                "V1": {
                    Current:                "1.0.0",
                    MimeTypes:              {"application/vnd.company.jk.identity+json;", "application/vnd.company.jk.user+json;", "application/vnd.company.jk.role+json;", "application/vnd.company.jk.scope+json;", "application/vnd.company.jk.test+json;"},
                    SkipVersionValidation:  true,
                    SkipMimeTypeValidation: true,
                },
            },
        },
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab提取运动物体的坐标
  • ¥15 人大金仓下载,有人知道怎么解决吗
  • ¥15 一个小问题,本人刚入门,哪位可以help
  • ¥15 python安卓开发
  • ¥15 使用R语言GD包一直不出结果
  • ¥15 计算机微处理器与接口技术相关问题,求解答图片的这个问题,有多少个端口,端口地址和解答问题的方法和思路,不要AI作答
  • ¥15 如何根据一个截图编写对应的HTML代码
  • ¥15 stm32标准库的PID角度环
  • ¥15 ADS已经下载好了,但是DAS下载不了,一直显示这两种情况,有什么办法吗,非常急!
  • ¥100 Excel 点击发送自动跳转outlook邮件