Home  >  Article  >  Backend Development  >  YAML custom tags in Go

YAML custom tags in Go

WBOY
WBOYforward
2024-02-10 10:18:08849browse

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"`
}

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)

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 ...

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/f21d44ef4f931cd592d500a3ff92049e[email protected]16ef26f8b7ea0fc89bdf90e275a93e5d 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"`
}

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

Please note that you must use the json tag instead of yaml for this to work just sigs.k8s.io/a7a5a3bc425f70cb2ec6c9d26f099434[email-protected]5db79b134e9f6b82c0b36e0489ee08ed 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"`
}

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!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete
Previous article:Meow conversation dataNext article:Meow conversation data