configuration

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 13, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package configuration provides a flexible, multi-source configuration system inspired by .NET's IConfiguration pattern. Configuration values can be loaded from environment variables, JSON files, command-line flags, and custom sources, then merged with priority ordering.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder provides a fluent API for assembling a Configuration from multiple sources with priority ordering (last added wins).

Example:

cfg := configuration.NewBuilder().
    AddEnv("APP_").
    AddJSONFile("config.json").
    Build()

func NewBuilder

func NewBuilder() *Builder

NewBuilder creates a new Builder.

func (*Builder) Add

func (b *Builder) Add(p Provider) *Builder

Add registers a configuration provider. Providers added later take priority over earlier ones for overlapping keys.

func (*Builder) Build

func (b *Builder) Build(ctx context.Context) (*Configuration, error)

Build merges all registered providers into a single Configuration. Providers are queried in order; later providers override earlier ones.

type Configuration

type Configuration struct {
	// contains filtered or unexported fields
}

Configuration holds merged configuration data from multiple providers.

Example:

cfg := configuration.NewBuilder().
    AddEnv("APP_").
    AddJSONFile("config.json").
    Build()
val := cfg.Get("db:host")

func New

func New() *Configuration

New creates an empty Configuration.

func (*Configuration) Bind

func (c *Configuration) Bind(target any) error

Bind populates a struct from the configuration data. Fields are matched using the `conf` tag. If no tag is present, the field name is used as key.

Example:

type Config struct {
    Host string `conf:"db:host"`
    Port int    `conf:"db:port" default:"5432"`
}
var cfg Config
err := configuration.Bind(&cfg)

func (*Configuration) Get

func (c *Configuration) Get(key string) (any, bool)

Get returns the value for the given key. The key is case-insensitive. Nested keys can be accessed with colon syntax, e.g. "db:host".

Returns:

The value and true if found, otherwise nil and false.

func (*Configuration) GetBool

func (c *Configuration) GetBool(key string) (bool, bool)

GetBool returns the bool value for the given key.

Returns:

The value and true if the key exists and can be converted to bool.

func (*Configuration) GetInt

func (c *Configuration) GetInt(key string) (int, bool)

GetInt returns the int value for the given key.

Returns:

The value and true if the key exists and can be converted to int.

func (*Configuration) GetSection

func (c *Configuration) GetSection(prefix string) *Configuration

GetSection returns a new Configuration containing only keys with the given prefix.

func (*Configuration) GetString

func (c *Configuration) GetString(key string) (string, bool)

GetString returns the string value for the given key.

Returns:

The value and true if the key exists and is a string.

type Provider

type Provider interface {
	// Name returns a human-readable identifier for this source.
	Name() string

	// Load retrieves configuration key-value pairs.
	Load(ctx context.Context) (map[string]any, error)
}

Provider is the interface that configuration sources must implement.

Example:

type MyProvider struct{}
func (p *MyProvider) Name() string { return "my" }
func (p *MyProvider) Load(ctx) (map[string]any, error) { return map[string]any{"key": "val"}, nil }

Directories

Path Synopsis
source
env

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL