config

package
v0.10.3 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: Apache-2.0 Imports: 21 Imported by: 4

README

config

config loads key/value configuration from one or more sources, merges the decoded data, resolves placeholders, and exposes values through path lookup or struct scanning.

Use the root package when you need a configurable Config object:

cfg := config.New(
	config.WithSource(file.NewSource("./configs")),
	config.WithSource(env.NewSource("APP_")),
)
if err := cfg.Load(); err != nil {
	return err
}

addr, err := cfg.Value("server.http.addr").String()

Use config/loader when you want the opinionated application-config flow:

var opts struct {
	Name string `yaml:"name"`
	Age  int    `yaml:"age"`
}

err := loader.Load(
	&opts,
	loader.WithConfigurationDirectory("./configs", "defaults"),
	loader.WithConfigFilePrefix("app"),
	loader.WithProfilesAlias("dev"),
)

Source packages:

  • config/file loads files or directories.
  • config/env reads environment variables by prefix.
  • config/inmem supplies an in-memory source for tests or embedded defaults.
  • config/loader merges defaults and explicit profile files into a struct.

Documentation

Overview

Package config provides source-based configuration loading, merging, placeholder resolution, value lookup, scanning, and watching.

Use config.New when composing explicit sources such as config/file, config/env, and config/inmem. Use config/loader for the repository's opinionated defaults-plus-profile loading flow.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is key not found.
	ErrNotFound = errors.New("key not found")
	// ErrTypeAssert is type assert error.
	ErrTypeAssert = errors.New("type assert error")
)

Functions

This section is empty.

Types

type Config

type Config interface {
	Load() error
	Scan(v interface{}) error
	Value(key string) Value
	Watch(key string, o Observer) error
	Close() error
}

Config is a config interface.

func New

func New(opts ...Option) Config

New new a config with options.

Example
package main

import (
	"fmt"

	"github.com/go-jimu/components/config"
	"github.com/go-jimu/components/config/inmem"
)

func main() {
	cfg := config.New(config.WithSource(inmem.NewSource("app", []byte(`{
		"server": {
			"addr": "127.0.0.1",
			"port": 8080
		}
	}`), "json")))
	if err := cfg.Load(); err != nil {
		panic(err)
	}
	defer cfg.Close()

	addr, err := cfg.Value("server.addr").String()
	if err != nil {
		panic(err)
	}
	port, err := cfg.Value("server.port").Int()
	if err != nil {
		panic(err)
	}

	fmt.Println(addr, port)

}
Output:
127.0.0.1 8080

type Decoder

type Decoder func(*KeyValue, map[string]interface{}) error

Decoder is config decoder.

type KeyValue

type KeyValue struct {
	Key    string
	Value  []byte
	Format string
}

KeyValue is config key value.

type Merge

type Merge func(dst, src interface{}) error

Merge is config merge func.

type Observer

type Observer func(string, Value)

Observer is config observer.

type Option

type Option func(*options)

Option is config option.

func WithDecoder

func WithDecoder(d Decoder) Option

WithDecoder with config decoder. DefaultDecoder behavior: If KeyValue.Format is non-empty, then KeyValue.Value will be deserialized into map[string]interface{} and stored in the config cache(map[string]interface{}) if KeyValue.Format is empty,{KeyValue.Key : KeyValue.Value} will be stored in config cache(map[string]interface{})

func WithMergeFunc

func WithMergeFunc(m Merge) Option

WithMergeFunc with config merge func.

func WithResolveActualTypes

func WithResolveActualTypes(enableConvertToType bool) Option

WithResolveActualTypes with config resolver. bool input will enable conversion of config to data types

func WithResolver

func WithResolver(r Resolver) Option

WithResolver with config resolver.

func WithSource

func WithSource(s ...Source) Option

WithSource with config source.

type Reader

type Reader interface {
	Merge(...*KeyValue) error
	Value(string) (Value, bool)
	Source() ([]byte, error)
	Resolve() error
}

Reader is config reader.

type Resolver

type Resolver func(map[string]interface{}) error

Resolver resolve placeholder in config.

type Source

type Source interface {
	Load() ([]*KeyValue, error)
	Watch() (Watcher, error)
}

Source is config source.

type Value

type Value interface {
	Bool() (bool, error)
	Int() (int64, error)
	Float() (float64, error)
	String() (string, error)
	Duration() (time.Duration, error)
	Slice() ([]Value, error)
	Map() (map[string]Value, error)
	Scan(interface{}) error
	Load() interface{}
	Store(interface{})
}

Value is config value interface.

type Watcher

type Watcher interface {
	Next() ([]*KeyValue, error)
	Stop() error
}

Watcher watches a source for changes.

Directories

Path Synopsis
Package env provides a config.Source backed by environment variables.
Package env provides a config.Source backed by environment variables.
Package file provides a config.Source backed by files or directories.
Package file provides a config.Source backed by files or directories.
Package inmem provides an in-memory config.Source.
Package inmem provides an in-memory config.Source.
Package loader provides an opinionated application configuration loader.
Package loader provides an opinionated application configuration loader.

Jump to

Keyboard shortcuts

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