mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-27 05:15:47 +00:00
102 lines
1.9 KiB
Go
102 lines
1.9 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
type Config struct {
|
|
Modules []ModuleConfig `json:"modules"`
|
|
Routes []RouteConfig `json:"routes"`
|
|
}
|
|
|
|
type Params map[string]any
|
|
|
|
var (
|
|
ErrParamNotFound = errors.New("not found")
|
|
ErrParamNotString = errors.New("not a string")
|
|
ErrParamNotNumber = errors.New("not a number")
|
|
)
|
|
|
|
func (p Params) GetString(key string) (string, error) {
|
|
value, ok := p[key]
|
|
if !ok {
|
|
return "", ErrParamNotFound
|
|
}
|
|
|
|
stringValue, ok := value.(string)
|
|
if !ok {
|
|
return "", ErrParamNotString
|
|
}
|
|
return stringValue, nil
|
|
}
|
|
|
|
func (p Params) GetInt(key string) (int, error) {
|
|
value, ok := p[key]
|
|
if !ok {
|
|
return 0, ErrParamNotFound
|
|
}
|
|
|
|
intValue, ok := value.(int)
|
|
if !ok {
|
|
floatValue, ok := value.(float64)
|
|
if !ok {
|
|
return 0, ErrParamNotNumber
|
|
}
|
|
intValue = int(floatValue)
|
|
}
|
|
return intValue, nil
|
|
}
|
|
|
|
func (p Params) GetBool(key string) (bool, error) {
|
|
value, ok := p[key]
|
|
if !ok {
|
|
return false, ErrParamNotFound
|
|
}
|
|
|
|
boolValue, ok := value.(bool)
|
|
if !ok {
|
|
return false, errors.New("not a boolean")
|
|
}
|
|
return boolValue, nil
|
|
}
|
|
|
|
func (p Params) GetStringSlice(key string) ([]string, error) {
|
|
value, ok := p[key]
|
|
if !ok {
|
|
return nil, ErrParamNotFound
|
|
}
|
|
|
|
interfaceSlice, ok := value.([]any)
|
|
if !ok {
|
|
return nil, errors.New("not a slice")
|
|
}
|
|
|
|
stringSlice := make([]string, len(interfaceSlice))
|
|
for i, v := range interfaceSlice {
|
|
str, ok := v.(string)
|
|
if !ok {
|
|
return nil, fmt.Errorf("element at index %d is not a string", i)
|
|
}
|
|
stringSlice[i] = str
|
|
}
|
|
return stringSlice, nil
|
|
}
|
|
|
|
type ModuleConfig struct {
|
|
Id string `json:"id"`
|
|
Type string `json:"type"`
|
|
Params Params `json:"params"`
|
|
}
|
|
|
|
type RouteConfig struct {
|
|
Input string `json:"input"`
|
|
Processors []ProcessorConfig `json:"processors"`
|
|
Output string `json:"output"`
|
|
}
|
|
|
|
type ProcessorConfig struct {
|
|
Type string `json:"type"`
|
|
Params Params `json:"params"`
|
|
}
|