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>
This commit is contained in:
parent
8d2b15a7c8
commit
8a903e0703
10 changed files with 232 additions and 173 deletions
31
internal/configparser/deviceconfig.go
Normal file
31
internal/configparser/deviceconfig.go
Normal 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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue