mcfg

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2025 License: MIT Imports: 16 Imported by: 2

Documentation

Index

Constants

View Source
const (
	// DefaultInstanceName is the default instance name.
	DefaultInstanceName = "default"
	// DefaultConfigFileName is the default config file name.
	DefaultConfigFileName = "config"
)

Variables

This section is empty.

Functions

func ClearHooks added in v0.2.0

func ClearHooks()

ClearHooks removes all registered hooks. This is intended for testing purposes only.

func Merge added in v0.2.0

func Merge(dest, src map[string]any) map[string]any

Merge merges the `src` map into the `dest` map. It performs a deep, case-insensitive merge. The `dest` map is modified in place.

func RegisterAfterLoadHook

func RegisterAfterLoadHook(hook any)

RegisterAfterLoadHook registers a hook to be executed after configuration is loaded. It accepts either a function with the signature `func(context.Context, map[string]any) (map[string]any, error)` or an implementation of the `StatefulHook` interface. Using a `StatefulHook` is the recommended way to handle expensive operations that should only run once (e.g., fetching remote config), as it allows caching within the hook's state. Each hook is stored with a unique key to prevent duplicate registrations.

Types

type Adapter

type Adapter interface {
	// Get gets the configuration value for the specified key.
	Get(ctx context.Context, pattern string) (any, error)

	// Data gets all configuration data.
	Data(ctx context.Context) (map[string]any, error)

	// Available checks and returns whether the configuration service is available.
	// The optional `resource` parameter specifies certain configuration resources.
	Available(ctx context.Context, resource ...string) bool
}

Adapter defines the configuration adapter interface.

type AdapterContent added in v0.2.0

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

AdapterContent implements the Adapter interface for configuration in content.

func NewAdapterContent added in v0.2.0

func NewAdapterContent(content string, format string) (*AdapterContent, error)

NewAdapterContent creates a new adapter for configuration in content. The parameter `format` specifies the format of the content, e.g., "yaml", "json".

func (*AdapterContent) Available added in v0.2.0

func (c *AdapterContent) Available(_ context.Context, _ ...string) bool

Available checks and returns whether the configuration service is available. For the content adapter, it is always available if it was initialized successfully.

func (*AdapterContent) Data added in v0.2.0

func (c *AdapterContent) Data(_ context.Context) (map[string]any, error)

Data gets all configuration data.

func (*AdapterContent) Get added in v0.2.0

func (c *AdapterContent) Get(_ context.Context, pattern string) (any, error)

Get gets the configuration value.

func (*AdapterContent) MergeConfigMap added in v0.2.0

func (c *AdapterContent) MergeConfigMap(_ context.Context, data map[string]any) error

MergeConfigMap merges a map into the existing configuration.

func (*AdapterContent) SetContent added in v0.2.0

func (c *AdapterContent) SetContent(content string, format string) error

SetContent sets the configuration content.

type AdapterFile

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

AdapterFile implements the Adapter interface for configuration in files.

func NewAdapterFile

func NewAdapterFile() (*AdapterFile, error)

NewAdapterFile creates a new file adapter and automatically loads the default config file if found.

func (*AdapterFile) Available

func (c *AdapterFile) Available(_ context.Context, resource ...string) bool

Available checks and returns whether the configuration service is available.

func (*AdapterFile) Data

func (c *AdapterFile) Data(_ context.Context) (map[string]any, error)

Data gets all configuration data.

func (*AdapterFile) Get

func (c *AdapterFile) Get(_ context.Context, pattern string) (any, error)

Get gets the configuration value for the specified key.

func (*AdapterFile) MergeConfigMap added in v0.1.1

func (c *AdapterFile) MergeConfigMap(_ context.Context, data map[string]any) error

MergeConfigMap merges a map into the existing configuration.

func (*AdapterFile) SetFile added in v0.2.0

func (c *AdapterFile) SetFile(name string) error

SetFile sets the configuration file by name or path and loads its content. If the `name` is a base name without an extension (e.g., "config"), it will search for the file in default paths. Otherwise, it treats `name` as a full path.

func (*AdapterFile) SetFileName

func (c *AdapterFile) SetFileName(name string) error

SetFileName sets the configuration file by name or path and loads its content. Deprecated: Use SetFile instead.

type Config

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

Config is a configuration management object.

func Instance

func Instance(name ...string) *Config

Instance returns a Config instance with default settings. The `name` parameter is the instance name. Note that if a file named "name.yaml" exists in the config directory, it will be used as the default config file.

Note: If a file named "name.yaml" exists in the config directory, it will be used as the default config file. If a file named "name.yaml" does not exist in the config directory, the default config file name "config" will be used.

func New

func New() (*Config, error)

New creates a new configuration management object and uses the file adapter.

func NewWithAdapter

func NewWithAdapter(adapter Adapter) *Config

NewWithAdapter creates a new configuration management object with an adapter.

func (*Config) Available

func (c *Config) Available(ctx context.Context, resource ...string) bool

Available checks if the adapter is available. The optional `resource` parameter is the resource name. If the resource name is not empty, it checks if the resource is available.

func (*Config) ClearCache added in v0.2.0

func (c *Config) ClearCache(_ context.Context)

ClearCache clears the internal configuration cache. It should be called when the underlying configuration source has changed.

func (*Config) Data

func (c *Config) Data(ctx context.Context) (map[string]any, error)

Data gets all configuration data.

func (*Config) Get

func (c *Config) Get(ctx context.Context, pattern string, def ...any) (*mvar.Var, error)

Get gets the configuration value for the specified key. The optional `def` parameter is the default value. If the configuration value is empty, the default value is returned. If the configuration value is empty and no default value is provided, nil is returned.

func (*Config) GetAdapter

func (c *Config) GetAdapter() Adapter

GetAdapter gets the configuration adapter.

func (*Config) GetBool added in v0.1.4

func (c *Config) GetBool(ctx context.Context, pattern string, def ...any) bool

GetBool gets the configuration value as a bool.

func (*Config) GetInt added in v0.1.4

func (c *Config) GetInt(ctx context.Context, pattern string, def ...any) int

GetInt gets the configuration value as an int.

func (*Config) GetMap added in v0.1.4

func (c *Config) GetMap(ctx context.Context, pattern string, def ...any) map[string]any

GetMap gets the configuration value as a map.

func (*Config) GetSlice added in v0.1.4

func (c *Config) GetSlice(ctx context.Context, pattern string, def ...any) []any

GetSlice gets the configuration value as a slice.

func (*Config) GetString added in v0.1.4

func (c *Config) GetString(ctx context.Context, pattern string, def ...any) string

GetString gets the configuration value as a string.

func (*Config) MustGet added in v0.1.4

func (c *Config) MustGet(ctx context.Context, pattern string, def ...any) *mvar.Var

MustGet acts as function Get, but it panics if error occurs.

func (*Config) SetAdapter

func (c *Config) SetAdapter(adapter Adapter)

SetAdapter sets the configuration adapter.

func (*Config) Struct added in v0.1.4

func (c *Config) Struct(ctx context.Context, v any, pattern string, hooks ...mconv.HookFunc) error

Struct unmarshals the configuration into a struct. The optional `pattern` parameter is the pattern to unmarshal the configuration into. If you want to specify the key name, you can use the `mconv` tag. It supports custom decoding hooks.

type ConfigHookFunc

type ConfigHookFunc func(ctx context.Context, data map[string]any) (map[string]any, error)

type StatefulHook added in v0.1.1

type StatefulHook interface {
	Hook(ctx context.Context, data map[string]any) (map[string]any, error)
}

StatefulHook is an interface for hooks that need to maintain state across multiple calls. This is useful for caching results of expensive operations, like fetching remote configuration.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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