Home > Software design >  How to unmarshal type aliases inside structs from yaml in golang?
How to unmarshal type aliases inside structs from yaml in golang?

Time:01-31

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)
}

CodePudding user response:

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
}
  • Related