Refactor and cleanup config parsing. Again.

This commit is contained in:
Anna Rose Wiggins 2025-09-05 17:16:11 -04:00
parent 553966ac87
commit b77135eab8
7 changed files with 220 additions and 154 deletions

View file

@ -0,0 +1,31 @@
package configparser
// These top-level structs use custom unmarshaling to unpack each available sub-type
type DeviceConfig struct {
Type DeviceType
Config interface{}
}
func (dc *DeviceConfig) UnmarshalYAML(unmarshal func(data interface{}) error) error {
metaConfig := &struct {
Type DeviceType
}{}
err := unmarshal(metaConfig)
if err != nil {
return err
}
dc.Type = metaConfig.Type
err = nil
switch metaConfig.Type {
case DeviceTypePhysical:
config := DeviceConfigPhysical{}
err = unmarshal(&config)
dc.Config = config
case DeviceTypeVirtual:
config := DeviceConfigVirtual{}
err = unmarshal(&config)
dc.Config = config
}
return err
}