Home  >  Article  >  Backend Development  >  Go, unmarshal to uppercase keys

Go, unmarshal to uppercase keys

WBOY
WBOYforward
2024-02-14 09:36:091188browse

Go, unmarshal to uppercase keys

php editor Strawberry is here to introduce you to a practical technique, that is "Go, unmarshal to uppercase keys". This technique can help us program more quickly and efficiently. When we write code, we often need to comment or uncomment a piece of code, and manually adding or removing comment symbols can be relatively tedious. However, by using the "Go, unmarshal to uppercase keys" method, we can quickly comment or uncomment the code, which greatly improves our work efficiency. Next, let us learn the specific operation methods together!

Question content

The subsequent key is always lowercase? Chapter 923

I'm looking for the simplest way to unmarshal unstructured yaml into golangcasing keys. Details--

have a look

https://go.dev/play/p/nihdqorpek1

Right now,

package main

import (
    "fmt"
    "log"

    "gopkg.in/yaml.v3"
)

var data = `
a: Easy!
b:
  c: 2
  d: [3, 4]
`

// Note: struct fields must be public in order for unmarshal to
// correctly populate the data.
type T struct {
    A string
    B struct {
        RenamedC int   `yaml:"c"`
        D        []int `yaml:",flow"`
    }
}

func main() {
    m := make(map[interface{}]interface{})

    err := yaml.Unmarshal([]byte(data), &m)
    if err != nil {
        log.Fatalf("error: %v", err)
    }
    fmt.Printf("--- m:\n%v\n\n", m)

    d, err := yaml.Marshal(&m)
    if err != nil {
        log.Fatalf("error: %v", err)
    }
    fmt.Printf("--- m dump:\n%s\n\n", string(d))
}
  • The data at row 10 is always unmarshalled into lowercase keys in m.
  • In order to unmarshal then into uppercase keys, I have to pre-define a struct, as shown in line 19.
  • However, the challenge I'm facing is that I'm dealing with unstructured yamls, i.e. I don't know their structure beforehand.

A follow-up comment said they were "switching from gopkg.in/yaml.v3 to sigs.k8s.io/yaml" , but reading the documentation for sigs.k8s.io/yaml I think they still need it Understand the structure of yaml, and you also need to define the translation mechanism in advance.

Is there any easy way to unmarshal unstructured yaml to uppercase/golangcasing keys? What's the easiest way? If no go package provides such functionality out of the box, are there any plugins/callbacks in them that would allow me to easily do the translation myself?

Workaround

You can declare a custom key type that implements the yaml.unmarshaler interface.

Things like this:

type mykey string

func (k *mykey) unmarshalyaml(n *yaml.node) error {
    var s string
    if err := n.decode(&s); err != nil {
        return err
    }
    *k = mykey(strings.toupper(s))
    return nil
}

Then use it as the map's key.

m := make(map[MyKey]any)

https://www.php.cn/link/e18cfe46b96c30852b565e561152d055

The above is the detailed content of Go, unmarshal to uppercase keys. 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