Refactored configuration to make sections less code intensive to add

This commit is contained in:
bwilliams committed 2026-10-09 09:30:15 +01:00
1 parent 8b0cecb51b
commit ff33ef082a
4 files changed
+438 -94

No files matched your search

+23 -51
View File
@@ -22,76 +22,48 @@ import (
"reflect"
)
const JSCKLogging string = "logging"
type JobSearchConfigLogging struct {
Level string
File string
Level string `cfg:"level" default:"warn"`
File string `cfg:"file"`
}
// Default configuration
func defaultConfigLogging() JobSearchConfigLogging {
return JobSearchConfigLogging{
Level: "warn",
}
/*
* General section functions
* See related methods in section.go
*/
func (c *JobSearchConfigLogging) Keys() []string { return keysOf(c) }
func (c JobSearchConfigLogging) Map() map[string]any { return mapOf(c) }
func (c JobSearchConfigLogging) Flatten() map[string]any {
return flattenOf(JSCKLogging, c)
}
// Converts configuration to a map
func (conf JobSearchConfigLogging) Map() map[string]any {
return map[string]any{
"level": conf.Level,
"file": conf.File,
}
}
// Converts configuration to a flat dotted format
func (conf JobSearchConfigLogging) Flatten() map[string]any {
confMap := conf.Map()
retMap := map[string]any{}
for k, v := range confMap {
retMap["logging."+k] = v
}
return retMap
}
// Returns the expected type of a configuration key value
func (c JobSearchConfigLogging) TypeOf(key string) (reflect.Kind, error) {
switch key {
case "level":
return reflect.String, nil
case "file":
return reflect.String, nil
default:
return reflect.Invalid, errors.New("unknown configuration key")
}
return typeOf(c, key)
}
// Returns the value of a configuration key
func (c JobSearchConfigLogging) ValueOf(key string) (any, error) {
switch key {
case "level":
return c.Level, nil
case "file":
return c.File, nil
default:
return "", errors.New("unknown configuration key")
}
return valueOf(c, key)
}
// Sets the value of a configuration key
func (c *JobSearchConfigLogging) SetValue(key string, value any) error {
return setValue(c, key, value)
}
/* Section specific functions */
// Checks values before they are stored (see validator)
func (c *JobSearchConfigLogging) validate(key string, value any) error {
switch key {
case "level":
newLevel := value.(string)
switch newLevel {
switch value.(string) {
case "debug", "info", "warn", "error":
c.Level = value.(string)
default:
return errors.New(
"invalid log level. Must be one of: debug, info, warn, error")
}
case "file":
c.File = value.(string)
default:
return errors.New("unknown configuration key")
}
return nil
}