yaml uses multiple complex mapping keys as a structure

WBOY
Release: 2024-02-09 19:12:09
forward
697 people have browsed it

yaml 将多个复杂的映射键作为一个结构体

php editor Youzi mentioned an important concept when introducing YAML, which is to use multiple complex mapping keys as a structure. YAML is a format used to represent data serialization and is highly human-readable. In YAML, you can use structures to combine multiple key-value pairs into a whole, making the structure of the data clearer and easier to understand. This approach can help developers better organize and manage complex data and improve code readability and maintainability. In practical applications, structures are a very useful feature, let's take a look at it!

Question content

I want to unmarshal a complex map key into a structure:

yaml

unf:
    item_meta:
      source:
        ? id: "aa"
          name: "bb"
        : "some value"
Copy after login

structure

type source struct {
    id     string `yaml:"id"`
    name   string `yaml:"name"`
}
Copy after login

Everything works as expected until I add another key:

yaml 2

unf:
    item_meta:
      source:
        ? id: "012"
          name: "bill"
        : "some value"
        ? id: "066"
          name: "bob"
        : "another value"
Copy after login

I received an error

"line xxx: mapping key "" already defined at line xxx"
Copy after login

I decided to use an alias:

yaml 3

unf:
    item_meta:
      aliases:
        - bill: &alias_bill
          id: "012"
          name: "Bill"
        - bob: &alias_bob
          id: "066"
          name: "Bob"
      source:
        ? *alias_bill
        : "some value"
        ? *alias_bob
          name: "Bob"
        : "another value"
Copy after login

The problem is solved! But we are using hiera server in the stack and hiera returns the contents of the replaced config file, so I end up with yaml 2 version. p>

Any ideas on how to solve this problem? Unable to configure hiera server .

Go to the playground

Solution

My solution is mainly based on this problem @larsks

The idea is to find nodes with duplicate map keys and create custom values ​​from the mapped node's value node.

func fixYamlNode(node *yaml.Node) {
    if node.Kind == yaml.MappingNode {
        length := len(node.Content)

        for i := 0; i < length; i += 2 {
            nodes := make([]*yaml.Node, 0)
            nodes = append(nodes, node.Content[i])
            for j := i + 2; j < length; j += 2 {
                nj := node.Content[j]
                if nodes[0].Kind == nj.Kind && nodes[0].Value == nj.Value {
                    nodes = append(nodes, nj)
                }
            }
            if len(nodes) == 1 {
                continue
            }

            fillMapValue(nodes)
        }

        for i := 1; i < length; i += 2 {
            valueNode := node.Content[i]
            fixYamlNode(valueNode)
        }
    }
}

func fillMapValue(nodes []*yaml.Node) {
    for _, node := range nodes {
        length := len(node.Content)
        for i := 0; i < length; i += 2 {
            node.Value = fmt.Sprintf("%s %s", node.Value, node.Content[i+1].Value)
        }
    }
}
Copy after login

The above is the detailed content of yaml uses multiple complex mapping keys as a structure. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!