Documentation
¶
Index ¶
- Constants
- func IsExist(path string) bool
- func RegisterAfterLoadHook(hook interface{})
- type Adapter
- type AdapterFile
- func (c *AdapterFile) Available(ctx context.Context, resource ...string) bool
- func (c *AdapterFile) Data(ctx context.Context) (map[string]any, error)
- func (c *AdapterFile) Get(ctx context.Context, pattern string) (any, error)
- func (c *AdapterFile) MergeConfigMap(ctx context.Context, data map[string]any) error
- func (c *AdapterFile) SetFileName(name string)
- type Config
- func (c *Config) Available(ctx context.Context, resource ...string) bool
- 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 = "config" // DefaultConfigFileName is the default config file name. DefaultConfigFileName = "config" )
Variables ¶
This section is empty.
Functions ¶
func RegisterAfterLoadHook ¶
func RegisterAfterLoadHook(hook interface{})
RegisterAfterLoadHook registers a hook to be executed after configuration is loaded. It accepts either a function with the signature `func(context.Context, map[string]interface{}) (map[string]interface{}, 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 AdapterFile ¶
type AdapterFile struct {
// contains filtered or unexported fields
}
func NewAdapterFile ¶
func NewAdapterFile() (*AdapterFile, error)
NewAdapterFile creates a new file adapter.
func (*AdapterFile) Available ¶
func (c *AdapterFile) Available(ctx context.Context, resource ...string) bool
Available checks and returns whether the configuration service is available. The optional `resource` parameter specifies certain configuration resources.
func (*AdapterFile) MergeConfigMap ¶ added in v0.1.1
MergeConfigMap merges a map into the existing configuration.
func (*AdapterFile) SetFileName ¶
func (c *AdapterFile) SetFileName(name string)
SetFileName sets the configuration file name.
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) 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]interface{}) (map[string]interface{}, 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.