e1.11.0 Configuration refactored and simplified (interface changed).

This commit is contained in:
2025-07-16 03:27:41 -04:00
parent 91b9961228
commit 09ef19bc9e
10 changed files with 170 additions and 482 deletions

174
config.go
View File

@ -2,10 +2,6 @@
package log
import (
"errors"
"fmt"
"github.com/lixenwraith/config"
"reflect"
"strings"
"time"
)
@ -105,161 +101,17 @@ var defaultConfig = Config{
// DefaultConfig returns a copy of the default configuration
func DefaultConfig() *Config {
// Create a copy to prevent modifications to the original
copiedConfig := defaultConfig
return defaultConfig.Clone()
}
// Clone creates a deep copy of the configuration
func (c *Config) Clone() *Config {
copiedConfig := *c
return &copiedConfig
}
// NewConfigFromFile loads configuration from a TOML file and returns a validated Config
func NewConfigFromFile(path string) (*Config, error) {
cfg := DefaultConfig()
// Use lixenwraith/config as a loader
loader := config.New()
// Register the struct to enable proper unmarshaling
if err := loader.RegisterStruct("log.", *cfg); err != nil {
return nil, fmt.Errorf("failed to register config struct: %w", err)
}
// Load from file (handles file not found gracefully)
if err := loader.Load(path, nil); err != nil && !errors.Is(err, config.ErrConfigNotFound) {
return nil, fmt.Errorf("failed to load config from %s: %w", path, err)
}
// Extract values into our Config struct
if err := extractConfig(loader, "log.", cfg); err != nil {
return nil, fmt.Errorf("failed to extract config values: %w", err)
}
// Validate the loaded configuration
if err := cfg.validate(); err != nil {
return nil, err
}
return cfg, nil
}
// NewConfigFromDefaults creates a Config with default values and applies overrides
func NewConfigFromDefaults(overrides map[string]any) (*Config, error) {
cfg := DefaultConfig()
// Apply overrides using reflection
if err := applyOverrides(cfg, overrides); err != nil {
return nil, fmt.Errorf("failed to apply overrides: %w", err)
}
// Validate the configuration
if err := cfg.validate(); err != nil {
return nil, err
}
return cfg, nil
}
// extractConfig extracts values from lixenwraith/config into our Config struct
func extractConfig(loader *config.Config, prefix string, cfg *Config) error {
v := reflect.ValueOf(cfg).Elem()
t := v.Type()
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
fieldValue := v.Field(i)
// Get the toml tag to determine the config key
tomlTag := field.Tag.Get("toml")
if tomlTag == "" {
continue
}
key := prefix + tomlTag
// Get value from loader
val, found := loader.Get(key)
if !found {
continue // Use default value
}
// Set the field value with type conversion
if err := setFieldValue(fieldValue, val); err != nil {
return fmt.Errorf("failed to set field %s: %w", field.Name, err)
}
}
return nil
}
// applyOverrides applies a map of overrides to the Config struct
func applyOverrides(cfg *Config, overrides map[string]any) error {
v := reflect.ValueOf(cfg).Elem()
t := v.Type()
// Create a map of field names to field values for efficient lookup
fieldMap := make(map[string]reflect.Value)
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
tomlTag := field.Tag.Get("toml")
if tomlTag != "" {
fieldMap[tomlTag] = v.Field(i)
}
}
for key, value := range overrides {
fieldValue, exists := fieldMap[key]
if !exists {
return fmt.Errorf("unknown config key: %s", key)
}
if err := setFieldValue(fieldValue, value); err != nil {
return fmt.Errorf("failed to set %s: %w", key, err)
}
}
return nil
}
// setFieldValue sets a reflect.Value with proper type conversion
func setFieldValue(field reflect.Value, value any) error {
switch field.Kind() {
case reflect.String:
strVal, ok := value.(string)
if !ok {
return fmt.Errorf("expected string, got %T", value)
}
field.SetString(strVal)
case reflect.Int64:
switch v := value.(type) {
case int64:
field.SetInt(v)
case int:
field.SetInt(int64(v))
default:
return fmt.Errorf("expected int64, got %T", value)
}
case reflect.Float64:
floatVal, ok := value.(float64)
if !ok {
return fmt.Errorf("expected float64, got %T", value)
}
field.SetFloat(floatVal)
case reflect.Bool:
boolVal, ok := value.(bool)
if !ok {
return fmt.Errorf("expected bool, got %T", value)
}
field.SetBool(boolVal)
default:
return fmt.Errorf("unsupported field type: %v", field.Kind())
}
return nil
}
// validate performs validation on the configuration
func (c *Config) validate() error {
// Validate performs validation on the configuration
func (c *Config) Validate() error {
// String validations
if strings.TrimSpace(c.Name) == "" {
return fmtErrorf("log name cannot be empty")
@ -277,8 +129,8 @@ func (c *Config) validate() error {
return fmtErrorf("timestamp_format cannot be empty")
}
if c.StdoutTarget != "stdout" && c.StdoutTarget != "stderr" {
return fmtErrorf("invalid stdout_target: '%s' (use stdout or stderr)", c.StdoutTarget)
if c.StdoutTarget != "stdout" && c.StdoutTarget != "stderr" && c.StdoutTarget != "split" {
return fmtErrorf("invalid stdout_target: '%s' (use stdout, stderr, or split)", c.StdoutTarget)
}
// Numeric validations
@ -319,10 +171,4 @@ func (c *Config) validate() error {
}
return nil
}
// Clone creates a deep copy of the configuration
func (c *Config) Clone() *Config {
copiedConfig := *c
return &copiedConfig
}