YAML custom tags in Go

WBOY
Release: 2024-02-10 10:18:08
forward
853 people have browsed it

Go 中的 YAML 自定义标签

php Editor Banana introduces you to YAML custom tags in Go language. YAML is a lightweight data serialization format, and the Go language, as a powerful programming language, naturally provides support for YAML. In Go, we can define the YAML data structure through custom tags to better parse and process YAML data. By using custom tags, we can easily map YAML data to structures in Go to achieve more flexible and convenient data processing. The following will introduce in detail the usage and precautions of YAML custom tags in Go.

Question content

I have these nested structures in go and added custom tags to their properties,

type dummyparams struct {
  param1 string `yaml:"param1"`
  param2 string `yaml:"param2"`
}

type dummy struct {
  name string `yaml:"name"`
  type string `yaml:"type"`
  params dummyparams `yaml:"params"`
}
Copy after login

I created some dummy instances and added them to the slice,

dummies := make([]dummy, 0)
dummy1 := dummy {
    name: "a"
    type: "type a"
    params: dummyparams {
        param1: "foo"
        param2: "bar"
    }
}
dummies = append(dummies, dummy1)
dummy2 := dummy {
    name: "b"
    type: "type b"
    params: dummyparams {
        param1: "foo"
        param2: "bar"
    }
}
dummies = append(dummies, dummy2)
Copy after login

Finally I organize the data and write it to a file

yamlData, err := yaml.Marshal(&dummies)
// handle error ...
writeErr := os.WriteFile("foo.yaml", yamlData, 0644)
// handle write error ...
Copy after login

But the yaml I get does not have lowercase tag names but uppercase structure names. Does anyone know why this happens and how to fix it?

Workaround

Blame it on the yaml implementation you are using. For example, if you use gopkg.in/yaml.v3 it will work. Try it on go playground. So one solution is to use another yaml implementation like gopkg.in/yaml.v3.

You mentioned in your comments that you are using https:// /pkg.go.dev/sigs.k8s.io/[email protected]. Its package documentation says:

In short, the library first uses go-yaml to convert yaml to json, and then uses json.marshal and json.unmarshal to do the conversion with the struct. This means that it effectively reuses the json structure tags as well as the custom json methods marshaljson and unmarshaljson , unlike go-yaml.

Sosigs.k8s.io/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2c554d41406c5a1d021f021c">[email protected]< /a> The first one is marshalled to json. If you want lowercase field names, use the json tag instead of the yaml tag:

import "sigs.k8s.io/yaml"

type dummyparams struct {
    param1 string `json:"param1"`
    param2 string `json:"param2"`
}

type dummy struct {
    name   string      `json:"name"`
    type   string      `json:"type"`
    params dummyparams `json:"params"`
}
Copy after login

With this change, the output contains lowercase names (try it on go playground):

- name: a
  params:
    param1: foo
    param2: bar
  type: type a
- name: b
  params:
    param1: foo
    param2: bar
  type: type b
Copy after login

Please note that you must use the json tag instead of yaml for this to work just sigs.k8s.io/<a href="/cdn-cgi/ l/email-protection" class="__cf_email__" A quirk of the data-cfemail="047d6569684472352a372a34">[email-protected]</a> package. If you want it to work with this package and other yaml implementations, you can provide both json and yaml tags:

type DummyParams struct {
    Param1 string `json:"param1" yaml:"param1"`
    Param2 string `json:"param2" yaml:"param2"`
}

type Dummy struct {
    Name   string      `json:"name" yaml:"name"`
    Type   string      `json:"type" yaml:"type"`
    Params DummyParams `json:"params" yaml:"params"`
}
Copy after login

The above is the detailed content of YAML custom tags in Go. 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!