I parse a .yaml file and need to unmarshal one of its properties in a custom manner. I am using the "gopkg.in/yaml.v2"
package.
The property in question is stored like this in my .yaml file:
endPointNumberSequences:
AD1: [ 0, 10, 14, 1, 11, 2, 100, 101, 12 ]
So it is basically a map[string][]uint16
kind of type.
But I need map[string]EpnSeq
where EpnSeq
is defined as:
type EpnSeq map[uint16]uint16
My struct:
type CitConfig struct {
// lots of other properties
// ...
EndPointNumberSequences map[string]EpnSeq `yaml:"endPointNumberSequences"`
}
I tried to implement the Unmarshaler interface on it like this:
// Implements the Unmarshaler interface of the yaml pkg.
func (e EpnSeq) UnmarshalYAML(unmarshal func(interface{}) error) error {
yamlEpnSequence := make([]uint16, 0)
err := unmarshal(&yamlEpnSequence)
if err != nil {
return err
}
for priority, epn := range yamlEpnSequence {
e[epn] = uint16(priority) // crashes with nil pointer
}
return nil
}
My problem is that inside the UnmarshalYAML
function the EpnSeq
type is not defined, causing a nil pointer exception at runtime.
How do I correctly implement the Unmarshaler interface here?