joyful/internal/configparser/deviceconfig.go
Anna Rose Wiggins 8a903e0703 Make enum values typed strings (#18)
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>
2025-09-05 21:17:55 +00:00

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
}