e3.0.1 Bug fix.

This commit is contained in:
2025-07-12 13:00:30 -04:00
parent 4053c463d6
commit 64cc9baaef
5 changed files with 19 additions and 10 deletions

2
.gitignore vendored
View File

@ -2,5 +2,7 @@
data
dev
log
logs
script
*.log
bin

View File

@ -2,6 +2,7 @@
package config
import (
"errors"
"fmt"
"os"
)
@ -10,7 +11,7 @@ import (
type Builder struct {
cfg *Config
opts LoadOptions
defaults interface{}
defaults any
prefix string
file string
args []string
@ -27,7 +28,7 @@ func NewBuilder() *Builder {
}
// WithDefaults sets the struct containing default values
func (b *Builder) WithDefaults(defaults interface{}) *Builder {
func (b *Builder) WithDefaults(defaults any) *Builder {
b.defaults = defaults
return b
}
@ -94,7 +95,9 @@ func (b *Builder) Build() (*Config, error) {
// Load configuration
if err := b.cfg.LoadWithOptions(b.file, b.args, b.opts); err != nil {
return nil, err
// The error might be non-fatal (e.g., file not found).
// Return the config object so it can be used with other sources.
return b.cfg, err
}
return b.cfg, nil
@ -104,7 +107,11 @@ func (b *Builder) Build() (*Config, error) {
func (b *Builder) MustBuild() *Config {
cfg, err := b.Build()
if err != nil {
panic(fmt.Sprintf("config build failed: %v", err))
// Ignore ErrConfigNotFound as it is not a fatal error for MustBuild.
// The application can proceed with defaults/env vars.
if !errors.Is(err, ErrConfigNotFound) {
panic(fmt.Sprintf("config build failed: %v", err))
}
}
return cfg
}

View File

@ -12,7 +12,7 @@ import (
// Quick creates a fully configured Config instance with a single call
// This is the recommended way to initialize configuration for most applications
func Quick(structDefaults interface{}, envPrefix, configFile string) (*Config, error) {
func Quick(structDefaults any, envPrefix, configFile string) (*Config, error) {
cfg := New()
// Register defaults from struct if provided
@ -31,7 +31,7 @@ func Quick(structDefaults interface{}, envPrefix, configFile string) (*Config, e
}
// QuickCustom creates a Config with custom options
func QuickCustom(structDefaults interface{}, opts LoadOptions, configFile string) (*Config, error) {
func QuickCustom(structDefaults any, opts LoadOptions, configFile string) (*Config, error) {
cfg := NewWithOptions(opts)
// Register defaults from struct if provided
@ -46,7 +46,7 @@ func QuickCustom(structDefaults interface{}, opts LoadOptions, configFile string
}
// MustQuick is like Quick but panics on error
func MustQuick(structDefaults interface{}, envPrefix, configFile string) *Config {
func MustQuick(structDefaults any, envPrefix, configFile string) *Config {
cfg, err := Quick(structDefaults, envPrefix, configFile)
if err != nil {
panic(fmt.Sprintf("config initialization failed: %v", err))

2
go.mod
View File

@ -1,6 +1,6 @@
module github.com/lixenwraith/config
go 1.24.2
go 1.24.5
require (
github.com/BurntSushi/toml v1.5.0

View File

@ -101,7 +101,7 @@ func (c *Config) Unregister(path string) error {
// RegisterStruct registers configuration values derived from a struct.
// It uses struct tags (`toml:"..."`) to determine the configuration paths.
// The prefix is prepended to all paths (e.g., "log."). An empty prefix is allowed.
func (c *Config) RegisterStruct(prefix string, structWithDefaults interface{}) error {
func (c *Config) RegisterStruct(prefix string, structWithDefaults any) error {
v := reflect.ValueOf(structWithDefaults)
// Handle pointer or direct struct value
@ -129,7 +129,7 @@ func (c *Config) RegisterStruct(prefix string, structWithDefaults interface{}) e
}
// RegisterStructWithTags is like RegisterStruct but allows custom tag names
func (c *Config) RegisterStructWithTags(prefix string, structWithDefaults interface{}, tagName string) error {
func (c *Config) RegisterStructWithTags(prefix string, structWithDefaults any, tagName string) error {
// Save current tag preference
oldTag := "toml"