This document demonstrates the new functional options pattern for creating Confy instances.
import (
"time"
"github.com/xraph/confy"
)
// Create with multiple options
cfg := confy.New(
confy.WithWatchInterval(30 * time.Second),
confy.WithSecretsEnabled(true),
confy.WithMetricsEnabled(true),
confy.WithErrorRetryCount(5),
confy.WithErrorRetryDelay(2 * time.Second),
)
// Minimal configuration
cfg := confy.New() // Uses all defaults
// With specific settings
cfg := confy.New(
confy.WithValidationMode(confy.ValidationModeStrict),
)// Using Config struct
cfg := confy.NewFromConfig(confy.Config{
WatchInterval: 30 * time.Second,
SecretsEnabled: true,
MetricsEnabled: true,
ErrorRetryCount: 5,
ErrorRetryDelay: 2 * time.Second,
})WithWatchInterval(time.Duration)- Set watch interval for configuration changesWithValidationMode(ValidationMode)- Set validation mode (Strict, Permissive, etc.)WithSecretsEnabled(bool)- Enable/disable secrets managementWithCacheEnabled(bool)- Enable/disable cachingWithReloadOnChange(bool)- Enable/disable automatic reload on changesWithMetricsEnabled(bool)- Enable/disable metrics collection
WithErrorRetryCount(int)- Set number of retry attempts on errorsWithErrorRetryDelay(time.Duration)- Set delay between retry attempts
WithLogger(logger.Logger)- Set custom logger instanceWithMetrics(metrics.Metrics)- Set custom metrics instanceWithErrorHandler(errors.ErrorHandler)- Set custom error handler
WithDefaultSources([]SourceConfig)- Set default configuration sources
cfg := confy.New(
confy.WithWatchInterval(5 * time.Second), // Fast reload
confy.WithValidationMode(confy.ValidationModePermissive),
confy.WithMetricsEnabled(false),
)cfg := confy.New(
confy.WithWatchInterval(30 * time.Second),
confy.WithValidationMode(confy.ValidationModeStrict),
confy.WithSecretsEnabled(true),
confy.WithMetricsEnabled(true),
confy.WithErrorRetryCount(10),
confy.WithErrorRetryDelay(3 * time.Second),
)// Define reusable option sets
var developmentOpts = []confy.Option{
confy.WithWatchInterval(5 * time.Second),
confy.WithValidationMode(confy.ValidationModePermissive),
}
var productionOpts = []confy.Option{
confy.WithWatchInterval(30 * time.Second),
confy.WithValidationMode(confy.ValidationModeStrict),
confy.WithSecretsEnabled(true),
confy.WithMetricsEnabled(true),
}
// Apply option sets
devCfg := confy.New(developmentOpts...)
prodCfg := confy.New(productionOpts...)
// Mix and match
customCfg := confy.New(append(
developmentOpts,
confy.WithLogger(myCustomLogger),
)...)import (
"github.com/xraph/confy"
"github.com/xraph/go-utils/log"
"github.com/xraph/go-utils/metrics"
"github.com/xraph/go-utils/errs"
)
logger := log.NewBeautifulLogger(log.WithLevel(log.LevelInfo))
metricsCollector := metrics.NewPrometheusMetrics()
errorHandler := errs.NewDefaultErrorHandler()
cfg := confy.New(
confy.WithLogger(logger),
confy.WithMetrics(metricsCollector),
confy.WithErrorHandler(errorHandler),
confy.WithMetricsEnabled(true),
)- Type Safety: Compile-time validation of options
- Discoverability: IDE autocomplete shows all available options
- Flexibility: Mix and match options as needed
- Readability: Self-documenting code
- Backward Compatibility: Legacy
NewFromConfigstill works - Composability: Create reusable option sets
- Default Values: Omit options to use sensible defaults
cfg := confy.New(confy.Config{
WatchInterval: 30 * time.Second,
SecretsEnabled: true,
MetricsEnabled: true,
Logger: myLogger,
})cfg := confy.New(
confy.WithWatchInterval(30 * time.Second),
confy.WithSecretsEnabled(true),
confy.WithMetricsEnabled(true),
confy.WithLogger(myLogger),
)Or keep using the old pattern with NewFromConfig:
cfg := confy.NewFromConfig(confy.Config{
WatchInterval: 30 * time.Second,
SecretsEnabled: true,
MetricsEnabled: true,
Logger: myLogger,
})- Use Variadic Options for New Code: The functional options pattern is more idiomatic and flexible
- Group Related Options: Organize options logically for better readability
- Create Option Sets: For different environments (dev, staging, prod)
- Document Custom Configurations: When creating reusable option sets
- Leverage Defaults: Only specify options that differ from defaults
The options are fully tested:
// Test individual options
func TestWithWatchInterval(t *testing.T) {
cfg := &Config{}
WithWatchInterval(10 * time.Second)(cfg)
// Assert cfg.WatchInterval == 10 * time.Second
}
// Test composition
func TestNew_WithOptions(t *testing.T) {
c := confy.New(
confy.WithWatchInterval(20*time.Second),
confy.WithSecretsEnabled(true),
)
// Assert instance created correctly
}See config_opts_test.go for comprehensive test examples.