Documentation
¶
Index ¶
- Constants
- func ClearHooks()
- func Merge(dest, src map[string]any) map[string]any
- func RegisterAfterLoadHook(hook any)
- type Adapter
- type AdapterContent
- func (c *AdapterContent) Available(_ context.Context, _ ...string) bool
- func (c *AdapterContent) Data(_ context.Context) (map[string]any, error)
- func (c *AdapterContent) Get(_ context.Context, pattern string) (any, error)
- func (c *AdapterContent) MergeConfigMap(_ context.Context, data map[string]any) error
- func (c *AdapterContent) SetContent(content string, format string) error
- type AdapterFile
- func (c *AdapterFile) Available(_ context.Context, resource ...string) bool
- func (c *AdapterFile) Data(_ context.Context) (map[string]any, error)
- func (c *AdapterFile) Get(_ context.Context, pattern string) (any, error)
- func (c *AdapterFile) MergeConfigMap(_ context.Context, data map[string]any) error
- func (c *AdapterFile) SetFile(name string) error
- func (c *AdapterFile) SetFileName(name string) error
- type Config
- func (c *Config) Available(ctx context.Context, resource ...string) bool
- func (c *Config) ClearCache(_ context.Context)
- func (c *Config) Data(ctx context.Context) (map[string]any, error)
- func (c *Config) Get(ctx context.Context, pattern string, def ...any) (*mvar.Var, error)
- func (c *Config) GetAdapter() Adapter
- func (c *Config) GetBool(ctx context.Context, pattern string, def ...any) bool
- func (c *Config) GetInt(ctx context.Context, pattern string, def ...any) int
- func (c *Config) GetMap(ctx context.Context, pattern string, def ...any) map[string]any
- func (c *Config) GetSlice(ctx context.Context, pattern string, def ...any) []any
- func (c *Config) GetString(ctx context.Context, pattern string, def ...any) string
- func (c *Config) MustGet(ctx context.Context, pattern string, def ...any) *mvar.Var
- func (c *Config) SetAdapter(adapter Adapter)
- func (c *Config) Struct(ctx context.Context, v any, pattern string, hooks ...mconv.HookFunc) error
- type ConfigHookFunc
- type StatefulHook
Constants ¶
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
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) MergeConfigMap ¶ added in v0.2.0
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) MergeConfigMap ¶ added in v0.1.1
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 ¶
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 NewWithAdapter ¶
NewWithAdapter creates a new configuration management object with an adapter.
func (*Config) Available ¶
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
ClearCache clears the internal configuration cache. It should be called when the underlying configuration source has changed.
func (*Config) Get ¶
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 ¶
GetAdapter gets the configuration adapter.
func (*Config) MustGet ¶ added in v0.1.4
MustGet acts as function Get, but it panics if error occurs.
func (*Config) SetAdapter ¶
SetAdapter sets the configuration adapter.
type ConfigHookFunc ¶
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.