从 yaml 文件中以自定义格式读取配置 [英] Read config in a custom format from yaml file

查看:26
本文介绍了从 yaml 文件中以自定义格式读取配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 YAML 格式的配置文件.我正在尝试以某种自定义格式读取配置.我无法猜测我可以使用的任何模式,例如树、json 等.例如.应用程序.yaml

I have a configuration file in YAML format. I am trying to read the configuration in some custom format. I couldn't guess any pattern that I can go for like tree, json etc. Eg. application.yaml

organization:
  products:
    product1:
      manager: "Rob"
      engineer: "John"
    product2:
      manager: "Henry"
      lead: "patrick"

配置文件可能包含大量信息,并且可能因文件而异.我想按以下格式构造数据,

The configuration file can have huge information and that can vary from file to file. I want to construct data in the following format,

organization/products/product1/manager  =  Rob
organization/products/product1/engineer = John
organization/products/product2/lead     = patrick

{"organization/products/product1/manager":"Rob","organization/products/product2/lead":"patrick"}

知道如何实现这种模式吗?

Any idea how I can achieve this pattern?

推荐答案

这本质上是打印树的练习.确切的实现将取决于您选择的特定 YAML 解析器,但几乎所有这些解析器都会具有某种任何映射"类型.在非常流行的 gopkg.in/yaml.v2 中,这种类型被命名为 MapSlice(不要让这个名字混淆你;它泄露了它必须处理灵活键类型的实现).

This is essentially an exercise in printing trees. The exact implementation will depend on the particular YAML parser you pick, but pretty much all of them will have some kind of "map of anything" type. In the very popular gopkg.in/yaml.v2 this type is named MapSlice (don't let the name confuse you; it leaks its implementation which has to deal with flexible key types).

只需将它扔给您最喜欢的树遍历算法即可呈现文本文件.这是一个仅适用于字符串键和一些标量叶节点的简单示例:

Just throw it at your favorite tree traversal algorithm to render the text file. Here is a simple example that works with only string keys and only some scalar leaf nodes:

package main

import (
    "bytes"
    "fmt"
    "io"
    "log"
    "path/filepath"
)

func main() {
    var tree yaml.MapSlice
    if err := yaml.Unmarshal(input, &tree); err != nil {
        log.Fatal(err)
    }

    var buf bytes.Buffer
    if err := render(&buf, tree, ""); err != nil {
        log.Fatal(err)
    }
}

func render(w io.Writer, tree yaml.MapSlice, prefix string) error {
    for _, branch := range tree {
        key, ok := branch.Key.(string)
        if !ok {
            return fmt.Errorf("unsupported key type: %T", branch.Key)
        }

        prefix := filepath.Join(prefix, key)

        switch x := branch.Value.(type) {
        default:
            return fmt.Errorf("unsupported value type: %T", branch.Value)

        case yaml.MapSlice:
            // recurse
            if err := render(w, x, prefix); err != nil {
                return err
            }
            continue

        // scalar values
        case string:
        case int:
        case float64:
        // ...
        }

        // print scalar
        if _, err := fmt.Fprintf(w, "%s = %v\n", prefix, branch.Value); err != nil {
            return err
        }
    }

    return nil
}

这篇关于从 yaml 文件中以自定义格式读取配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆