英文:
How to unmarshal type aliases inside structs from yaml in golang?
问题
我想要以下的yaml内容:
kind: bar
name: baryaml
被解析到结构体Resource中:
type Kind int
const (
KIND_FOO Kind = iota
KIND_BAR
)
type Resource struct {
Kind Kind
Name string
}
有人能解释一下为什么下面的代码无法正确存储kind,即使它被正确解析了吗?
# 输出:
Unmarshaled kind: 1
yamlBar: {0 baryaml}
# 期望输出:
Unmarshaled kind: 1
yamlBar: {1 baryaml}
package main
import (
"fmt"
"gopkg.in/yaml.v3"
)
type Kind int
const (
KIND_FOO Kind = iota
KIND_BAR
)
func (k *Kind) UnmarshalYAML(value *yaml.Node) error {
var kind string
err := value.Decode(&kind)
if err != nil {
return err
}
var x Kind
switch kind {
case "foo":
x = KIND_FOO
case "bar":
x = KIND_BAR
default:
return fmt.Errorf("unknown kind: %s", kind)
}
k = &x
fmt.Println("Unmarshaled kind:", *k)
return nil
}
type Resource struct {
Kind Kind
Name string
}
func main() {
var yamlBar = `
kind: bar
name: baryaml
`
r := Resource{}
err := yaml.Unmarshal([]byte(yamlBar), &r)
if err != nil {
panic(err)
}
fmt.Println("yamlBar:", r)
}
英文:
I would like the following yaml
kind: bar
name: baryaml
to be unmarshaled inside the struct Resource
type Kind int
const (
KIND_FOO Kind = iota
KIND_BAR
)
type Resource struct {
Kind Kind
Name string
}
Could someone explain why the code below is unable to store the correct kind, even though it is being unmarshaled correctly?
# Output:
Unmarshaled kind: 1
yamlBar: {0 baryaml}
# Expected Output:
Unmarshaled kind: 1
yamlBar: {1 baryaml}
package main
import (
"fmt"
"gopkg.in/yaml.v3"
)
type Kind int
const (
KIND_FOO Kind = iota
KIND_BAR
)
func (k *Kind) UnmarshalYAML(value *yaml.Node) error {
var kind string
err := value.Decode(&kind)
if err != nil {
return err
}
var x Kind
switch kind {
case "foo":
x = KIND_FOO
case "bar":
x = KIND_BAR
default:
return fmt.Errorf("unknown kind: %s", kind)
}
k = &x
fmt.Println("Unmarshaled kind:", *k)
return nil
}
type Resource struct {
Kind Kind
Name string
}
func main() {
var yamlBar = `
kind: bar
name: baryaml
`
r := Resource{}
err := yaml.Unmarshal([]byte(yamlBar), &r)
if err != nil {
panic(err)
}
fmt.Println("yamlBar:", r)
}
答案1
得分: 0
感谢@JimB建议解引用k
指针:
func (k *Kind) UnmarshalYAML(value *yaml.Node) error {
var kind string
err := value.Decode(&kind)
if err != nil {
return err
}
switch kind {
case "foo":
*k = KIND_FOO
case "bar":
*k = KIND_BAR
default:
return fmt.Errorf("未知的类型: %s", kind)
}
fmt.Println("解组的类型:", *k)
return nil
}
英文:
Thanks to @JimB for suggesting to dereference the k
pointer:
func (k *Kind) UnmarshalYAML(value *yaml.Node) error {
var kind string
err := value.Decode(&kind)
if err != nil {
return err
}
switch kind {
case "foo":
*k = KIND_FOO
case "bar":
*k = KIND_BAR
default:
return fmt.Errorf("unknown kind: %s", kind)
}
fmt.Println("Unmarshaled kind:", *k)
return nil
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论