Refactor and cleanup config parsing. Again.

This commit is contained in:
Anna Rose Wiggins 2025-09-05 17:16:11 -04:00
parent 553966ac87
commit b77135eab8
7 changed files with 220 additions and 154 deletions

View 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
}

View file

@ -0,0 +1,35 @@
package configparser
type DeviceConfigPhysical struct {
Name string
DeviceName string `yaml:"device_name,omitempty"`
DevicePath string `yaml:"device_path,omitempty"`
Lock bool
}
// TODO: custom yaml unmarshaling is obtuse; do we really need to do all of this work
// just to set a single default value?
func (dc *DeviceConfigPhysical) UnmarshalYAML(unmarshal func(data interface{}) error) error {
var raw struct {
Name string
DeviceName string `yaml:"device_name"`
DevicePath string `yaml:"device_path"`
Lock bool `yaml:"lock,omitempty"`
}
// Set non-standard defaults
raw.Lock = true
err := unmarshal(&raw)
if err != nil {
return err
}
*dc = DeviceConfigPhysical{
Name: raw.Name,
DeviceName: raw.DeviceName,
DevicePath: raw.DevicePath,
Lock: raw.Lock,
}
return nil
}

View 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
}

View file

@ -0,0 +1,60 @@
package configparser
type RuleConfig struct {
Type RuleType
Name string
Modes []string
Config interface{}
}
func (dc *RuleConfig) UnmarshalYAML(unmarshal func(data interface{}) error) error {
metaConfig := &struct {
Type RuleType
Name string
Modes []string
}{}
err := unmarshal(metaConfig)
if err != nil {
return err
}
dc.Type = metaConfig.Type
dc.Name = metaConfig.Name
dc.Modes = metaConfig.Modes
switch dc.Type {
case RuleTypeButton:
config := RuleConfigButton{}
err = unmarshal(&config)
dc.Config = config
case RuleTypeButtonCombo:
config := RuleConfigButtonCombo{}
err = unmarshal(&config)
dc.Config = config
case RuleTypeButtonLatched:
config := RuleConfigButtonLatched{}
err = unmarshal(&config)
dc.Config = config
case RuleTypeAxis:
config := RuleConfigAxis{}
err = unmarshal(&config)
dc.Config = config
case RuleTypeAxisCombined:
config := RuleConfigAxisCombined{}
err = unmarshal(&config)
dc.Config = config
case RuleTypeAxisToButton:
config := RuleConfigAxisToButton{}
err = unmarshal(&config)
dc.Config = config
case RuleTypeAxisToRelaxis:
config := RuleConfigAxisToRelaxis{}
err = unmarshal(&config)
dc.Config = config
case RuleTypeModeSelect:
config := RuleConfigModeSelect{}
err = unmarshal(&config)
dc.Config = config
}
return err
}

View file

@ -0,0 +1,53 @@
package configparser
import (
"fmt"
"strings"
)
// TODO: maybe these want to live somewhere other than configparser?
type RuleType string
const (
RuleTypeNone RuleType = ""
RuleTypeButton RuleType = "button"
RuleTypeButtonCombo RuleType = "button-combo"
RuleTypeButtonLatched RuleType = "button-latched"
RuleTypeAxis RuleType = "axis"
RuleTypeAxisCombined RuleType = "axis-combined"
RuleTypeAxisToButton RuleType = "axis-to-button"
RuleTypeAxisToRelaxis RuleType = "axis-to-relaxis"
RuleTypeModeSelect RuleType = "mode-select"
)
var (
ruleTypeMap = map[string]RuleType{
"button": RuleTypeButton,
"button-combo": RuleTypeButtonCombo,
"button-latched": RuleTypeButtonLatched,
"axis": RuleTypeAxis,
"axis-combined": RuleTypeAxisCombined,
"axis-to-button": RuleTypeAxisToButton,
"axis-to-relaxis": RuleTypeAxisToRelaxis,
"mode-select": RuleTypeModeSelect,
}
)
func ParseRuleType(in string) (RuleType, error) {
ruleType, ok := ruleTypeMap[strings.ToLower(in)]
if !ok {
return RuleTypeNone, fmt.Errorf("invalid rule type '%s'", in)
}
return ruleType, nil
}
func (rt *RuleType) UnmarshalYAML(unmarshal func(data interface{}) error) error {
var raw string
err := unmarshal(&raw)
if err != nil {
return err
}
*rt, err = ParseRuleType(raw)
return err
}

View file

@ -1,38 +1,13 @@
// These types comprise the YAML schema for configuring Joyful.
// The config files will be combined and then unmarshalled into this
// These types comprise the YAML schema that doesn't need custom unmarshalling.
package configparser
import (
"fmt"
)
type Config struct {
Devices []DeviceConfig
Modes []string
Rules []RuleConfig
}
// These top-level structs use custom unmarshaling to unpack each available sub-type
type DeviceConfig struct {
Type DeviceType
Config interface{}
}
type RuleConfig struct {
Type RuleType
Name string
Modes []string
Config interface{}
}
type DeviceConfigPhysical struct {
Name string
DeviceName string `yaml:"device_name,omitempty"`
DevicePath string `yaml:"device_path,omitempty"`
Lock bool
}
// TODO: configure custom unmarshaling so we can overload Buttons, Axes, and RelativeAxes...
type DeviceConfigVirtual struct {
Name string
@ -116,110 +91,3 @@ type RuleTargetConfigRelaxis struct {
type RuleTargetConfigModeSelect struct {
Modes []string
}
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
default:
err = fmt.Errorf("invalid device type '%s'", dc.Type)
}
return err
}
func (dc *RuleConfig) UnmarshalYAML(unmarshal func(data interface{}) error) error {
metaConfig := &struct {
Type RuleType
Name string
Modes []string
}{}
err := unmarshal(metaConfig)
if err != nil {
return err
}
dc.Type = metaConfig.Type
dc.Name = metaConfig.Name
dc.Modes = metaConfig.Modes
switch dc.Type {
case RuleTypeButton:
config := RuleConfigButton{}
err = unmarshal(&config)
dc.Config = config
case RuleTypeButtonCombo:
config := RuleConfigButtonCombo{}
err = unmarshal(&config)
dc.Config = config
case RuleTypeButtonLatched:
config := RuleConfigButtonLatched{}
err = unmarshal(&config)
dc.Config = config
case RuleTypeAxis:
config := RuleConfigAxis{}
err = unmarshal(&config)
dc.Config = config
case RuleTypeAxisCombined:
config := RuleConfigAxisCombined{}
err = unmarshal(&config)
dc.Config = config
case RuleTypeAxisToButton:
config := RuleConfigAxisToButton{}
err = unmarshal(&config)
dc.Config = config
case RuleTypeAxisToRelaxis:
config := RuleConfigAxisToRelaxis{}
err = unmarshal(&config)
dc.Config = config
case RuleTypeModeSelect:
config := RuleConfigModeSelect{}
err = unmarshal(&config)
dc.Config = config
default:
err = fmt.Errorf("invalid rule type '%s'", dc.Type)
}
return err
}
// TODO: custom yaml unmarshaling is obtuse; do we really need to do all of this work
// just to set a single default value?
func (dc *DeviceConfigPhysical) UnmarshalYAML(unmarshal func(data interface{}) error) error {
var raw struct {
Name string
DeviceName string `yaml:"device_name"`
DevicePath string `yaml:"device_path"`
Lock bool `yaml:"lock,omitempty"`
}
// Set non-standard defaults
raw.Lock = true
err := unmarshal(&raw)
if err != nil {
return err
}
*dc = DeviceConfigPhysical{
Name: raw.Name,
DeviceName: raw.DeviceName,
DevicePath: raw.DevicePath,
Lock: raw.Lock,
}
return nil
}

View file

@ -1,21 +0,0 @@
package configparser
type DeviceType string
const (
DeviceTypePhysical DeviceType = "physical"
DeviceTypeVirtual DeviceType = "virtual"
)
type RuleType string
const (
RuleTypeButton RuleType = "button"
RuleTypeButtonCombo RuleType = "button-combo"
RuleTypeButtonLatched RuleType = "button-latched"
RuleTypeAxis RuleType = "axis"
RuleTypeAxisCombined RuleType = "axis-combined"
RuleTypeAxisToButton RuleType = "axis-to-button"
RuleTypeAxisToRelaxis RuleType = "axis-to-relaxis"
RuleTypeModeSelect RuleType = "mode-select"
)