e3.0.1 Bug fix.
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@ -2,5 +2,7 @@
|
|||||||
data
|
data
|
||||||
dev
|
dev
|
||||||
log
|
log
|
||||||
|
logs
|
||||||
|
script
|
||||||
*.log
|
*.log
|
||||||
bin
|
bin
|
||||||
|
|||||||
15
builder.go
15
builder.go
@ -2,6 +2,7 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
@ -10,7 +11,7 @@ import (
|
|||||||
type Builder struct {
|
type Builder struct {
|
||||||
cfg *Config
|
cfg *Config
|
||||||
opts LoadOptions
|
opts LoadOptions
|
||||||
defaults interface{}
|
defaults any
|
||||||
prefix string
|
prefix string
|
||||||
file string
|
file string
|
||||||
args []string
|
args []string
|
||||||
@ -27,7 +28,7 @@ func NewBuilder() *Builder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// WithDefaults sets the struct containing default values
|
// WithDefaults sets the struct containing default values
|
||||||
func (b *Builder) WithDefaults(defaults interface{}) *Builder {
|
func (b *Builder) WithDefaults(defaults any) *Builder {
|
||||||
b.defaults = defaults
|
b.defaults = defaults
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
@ -94,7 +95,9 @@ func (b *Builder) Build() (*Config, error) {
|
|||||||
|
|
||||||
// Load configuration
|
// Load configuration
|
||||||
if err := b.cfg.LoadWithOptions(b.file, b.args, b.opts); err != nil {
|
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
|
return b.cfg, nil
|
||||||
@ -104,7 +107,11 @@ func (b *Builder) Build() (*Config, error) {
|
|||||||
func (b *Builder) MustBuild() *Config {
|
func (b *Builder) MustBuild() *Config {
|
||||||
cfg, err := b.Build()
|
cfg, err := b.Build()
|
||||||
if err != nil {
|
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
|
return cfg
|
||||||
}
|
}
|
||||||
@ -12,7 +12,7 @@ import (
|
|||||||
|
|
||||||
// Quick creates a fully configured Config instance with a single call
|
// Quick creates a fully configured Config instance with a single call
|
||||||
// This is the recommended way to initialize configuration for most applications
|
// 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()
|
cfg := New()
|
||||||
|
|
||||||
// Register defaults from struct if provided
|
// 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
|
// 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)
|
cfg := NewWithOptions(opts)
|
||||||
|
|
||||||
// Register defaults from struct if provided
|
// 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
|
// 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)
|
cfg, err := Quick(structDefaults, envPrefix, configFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("config initialization failed: %v", err))
|
panic(fmt.Sprintf("config initialization failed: %v", err))
|
||||||
|
|||||||
2
go.mod
2
go.mod
@ -1,6 +1,6 @@
|
|||||||
module github.com/lixenwraith/config
|
module github.com/lixenwraith/config
|
||||||
|
|
||||||
go 1.24.2
|
go 1.24.5
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/BurntSushi/toml v1.5.0
|
github.com/BurntSushi/toml v1.5.0
|
||||||
|
|||||||
@ -101,7 +101,7 @@ func (c *Config) Unregister(path string) error {
|
|||||||
// RegisterStruct registers configuration values derived from a struct.
|
// RegisterStruct registers configuration values derived from a struct.
|
||||||
// It uses struct tags (`toml:"..."`) to determine the configuration paths.
|
// 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.
|
// 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)
|
v := reflect.ValueOf(structWithDefaults)
|
||||||
|
|
||||||
// Handle pointer or direct struct value
|
// 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
|
// 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
|
// Save current tag preference
|
||||||
oldTag := "toml"
|
oldTag := "toml"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user