Refactor and cleanup config parsing. Again.
This commit is contained in:
parent
553966ac87
commit
b77135eab8
7 changed files with 220 additions and 154 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