This also moves validation into the parsing process and refactors a bunch of code related to the config. Reviewed-on: #18 Co-authored-by: Anna Rose Wiggins <annabunches@gmail.com> Co-committed-by: Anna Rose Wiggins <annabunches@gmail.com>
31 lines
671 B
Go
31 lines
671 B
Go
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
|
|
}
|