- Base type with standard sections (Server, Database, MCIAS, Log) - Duration wrapper type for TOML string→time.Duration decoding - Generic Load[T] with TOML parse, reflection-based env overrides, defaults, required field validation, optional Validator interface - Env overrides: PREFIX_SECTION_FIELD for string, duration, bool, []string (comma-separated) - WebConfig exported for services with web UIs (not embedded in Base) - 16 tests covering full/minimal configs, defaults, env overrides, validation, error cases Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
29 lines
766 B
Go
29 lines
766 B
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
// Duration is a time.Duration that can be unmarshalled from a TOML string
|
|
// (e.g., "30s", "5m"). go-toml v2 does not natively decode strings into
|
|
// time.Duration, so this wrapper implements encoding.TextUnmarshaler.
|
|
type Duration struct {
|
|
time.Duration
|
|
}
|
|
|
|
// UnmarshalText implements encoding.TextUnmarshaler for TOML string decoding.
|
|
func (d *Duration) UnmarshalText(text []byte) error {
|
|
parsed, err := time.ParseDuration(string(text))
|
|
if err != nil {
|
|
return fmt.Errorf("invalid duration %q: %w", string(text), err)
|
|
}
|
|
d.Duration = parsed
|
|
return nil
|
|
}
|
|
|
|
// MarshalText implements encoding.TextMarshaler for TOML string encoding.
|
|
func (d Duration) MarshalText() ([]byte, error) {
|
|
return []byte(d.String()), nil
|
|
}
|