Home >Backend Development >Golang >How to Parse YAML Files with Dynamic Keys in Go?

How to Parse YAML Files with Dynamic Keys in Go?

Barbara Streisand
Barbara StreisandOriginal
2024-11-04 02:38:29526browse

How to Parse YAML Files with Dynamic Keys in Go?

YAML parsing Go code with dynamic keys

To parse Go code with dynamic keys in a YAML file, follow these steps:

1. Install the yaml library:

<code class="sh">go get gopkg.in/yaml.v2</code>

2. Define the model structure:
Create a structure to represent YAML data. If you know all possible keys you can define them using regular fields, otherwise you can use map[string]interface{} fields to contain dynamic keys.

3. Use a custom Unmarshaler (optional):
If you need to customize how YAML data is parsed, you can use a custom Unmarshaler. This can help you handle dynamic keys or other complex parsing scenarios.

4. Parse YAML data:
Use the yaml.Unmarshal function to parse the YAML data and deserialize it into a model structure.

Example:
The following example demonstrates how to parse a YAML file with dynamic keys:

<code class="go">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 `yaml:",inline"`
}

func main() {
    e := map[string]Environment{}
    if err := yaml.Unmarshal([]byte(data), &e); err != nil {
        fmt.Println(err.Error())
    }
    fmt.Printf("%#v\n", e)
}</code>

Output:

map[string]main.Environment{
    "development": {
        SkipHeaderValidation: true,
        Versions: {
            "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,
            },
            "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,
            },
        },
    },
}

The above is the detailed content of How to Parse YAML Files with Dynamic Keys in Go?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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