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
40
internal/configparser/devicetype.go
Normal file
40
internal/configparser/devicetype.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
package configparser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type DeviceType string
|
||||
|
||||
const (
|
||||
DeviceTypeNone DeviceType = ""
|
||||
DeviceTypePhysical DeviceType = "physical"
|
||||
DeviceTypeVirtual DeviceType = "virtual"
|
||||
)
|
||||
|
||||
var (
|
||||
deviceTypeMap = map[string]DeviceType{
|
||||
"physical": DeviceTypePhysical,
|
||||
"virtual": DeviceTypeVirtual,
|
||||
}
|
||||
)
|
||||
|
||||
func ParseDeviceType(in string) (DeviceType, error) {
|
||||
deviceType, ok := deviceTypeMap[strings.ToLower(in)]
|
||||
if !ok {
|
||||
return DeviceTypeNone, fmt.Errorf("invalid rule type '%s'", in)
|
||||
}
|
||||
return deviceType, nil
|
||||
}
|
||||
|
||||
func (rt *DeviceType) UnmarshalYAML(unmarshal func(data interface{}) error) error {
|
||||
var raw string
|
||||
err := unmarshal(&raw)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*rt, err = ParseDeviceType(raw)
|
||||
return err
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue