Documentation
¶
Overview ¶
Package gcfg provides reading, caching and managing for configuration.
Package gcfg provides reading, caching and managing for configuration.
Package gcfg provides reading, caching and managing for configuration.
Package gcfg provides reading, caching and managing for configuration.
Index ¶
- Constants
- type Adapter
- type AdapterContent
- func (a *AdapterContent) AddWatcher(name string, fn WatcherFunc)
- func (a *AdapterContent) Available(ctx context.Context, resource ...string) (ok bool)
- func (a *AdapterContent) Data(ctx context.Context) (data map[string]any, err error)
- func (a *AdapterContent) Get(ctx context.Context, pattern string) (value any, err error)
- func (a *AdapterContent) GetWatcherNames() []string
- func (a *AdapterContent) IsWatching(name string) bool
- func (a *AdapterContent) RemoveWatcher(name string)
- func (a *AdapterContent) SetContent(content string) error
- type AdapterContentCtx
- type AdapterFile
- func (a *AdapterFile) AddPath(directoryPaths ...string) (err error)
- func (a *AdapterFile) AddWatcher(name string, fn WatcherFunc)
- func (a *AdapterFile) Available(ctx context.Context, fileName ...string) bool
- func (a *AdapterFile) Clear()
- func (a *AdapterFile) ClearContent()
- func (a *AdapterFile) Data(ctx context.Context) (data map[string]any, err error)
- func (a *AdapterFile) Dump()
- func (a *AdapterFile) Get(ctx context.Context, pattern string) (value any, err error)
- func (a *AdapterFile) GetContent(fileNameOrPath ...string) string
- func (a *AdapterFile) GetFileName() string
- func (a *AdapterFile) GetFilePath(fileNameOrPath ...string) (filePath string, err error)
- func (a *AdapterFile) GetPaths() []string
- func (a *AdapterFile) GetWatcherNames() []string
- func (a *AdapterFile) IsWatching(name string) bool
- func (a *AdapterFile) MustGet(ctx context.Context, pattern string) *gvar.Var
- func (a *AdapterFile) RemoveContent(fileNameOrPath ...string)
- func (a *AdapterFile) RemoveWatcher(name string)
- func (a *AdapterFile) Set(pattern string, value any) error
- func (a *AdapterFile) SetContent(content string, fileNameOrPath ...string)
- func (a *AdapterFile) SetFileName(fileNameOrPath string)
- func (a *AdapterFile) SetPath(directoryPath string) (err error)
- func (a *AdapterFile) SetViolenceCheck(check bool)
- type AdapterFileCtx
- func (a *AdapterFileCtx) GetContent() *gvar.Var
- func (a *AdapterFileCtx) GetFileName() string
- func (a *AdapterFileCtx) GetFilePath() string
- func (a *AdapterFileCtx) GetFileType() string
- func (a *AdapterFileCtx) GetKey() string
- func (a *AdapterFileCtx) GetOperation() OperationType
- func (a *AdapterFileCtx) GetValue() *gvar.Var
- func (a *AdapterFileCtx) WithContent(content any) *AdapterFileCtx
- func (a *AdapterFileCtx) WithFileName(fileName string) *AdapterFileCtx
- func (a *AdapterFileCtx) WithFilePath(filePath string) *AdapterFileCtx
- func (a *AdapterFileCtx) WithFileType(fileType string) *AdapterFileCtx
- func (a *AdapterFileCtx) WithKey(key string) *AdapterFileCtx
- func (a *AdapterFileCtx) WithOperation(operation OperationType) *AdapterFileCtx
- func (a *AdapterFileCtx) WithValue(value any) *AdapterFileCtx
- type Config
- func (c *Config) Available(ctx context.Context, resource ...string) (ok bool)
- func (c *Config) Data(ctx context.Context) (data map[string]any, err error)
- func (c *Config) Get(ctx context.Context, pattern string, def ...any) (*gvar.Var, error)
- func (c *Config) GetAdapter() Adapter
- func (c *Config) GetWithCmd(ctx context.Context, pattern string, def ...any) (*gvar.Var, error)
- func (c *Config) GetWithEnv(ctx context.Context, pattern string, def ...any) (*gvar.Var, error)
- func (c *Config) MustData(ctx context.Context) map[string]any
- func (c *Config) MustGet(ctx context.Context, pattern string, def ...any) *gvar.Var
- func (c *Config) MustGetWithCmd(ctx context.Context, pattern string, def ...any) *gvar.Var
- func (c *Config) MustGetWithEnv(ctx context.Context, pattern string, def ...any) *gvar.Var
- func (c *Config) SetAdapter(adapter Adapter)
- type Loader
- func (l *Loader[T]) Get() T
- func (l *Loader[T]) GetPointer() *T
- func (l *Loader[T]) IsWatching() bool
- func (l *Loader[T]) Load(ctx context.Context) error
- func (l *Loader[T]) MustLoad(ctx context.Context)
- func (l *Loader[T]) MustLoadAndWatch(ctx context.Context, name string)
- func (l *Loader[T]) MustWatch(ctx context.Context, name string)
- func (l *Loader[T]) OnChange(fn func(updated T) error) *Loader[T]
- func (l *Loader[T]) SetConverter(converter func(data any, target *T) error) *Loader[T]
- func (l *Loader[T]) SetReuseTargetStruct(reuse bool) *Loader[T]
- func (l *Loader[T]) SetWatchErrorHandler(errorFunc func(ctx context.Context, err error)) *Loader[T]
- func (l *Loader[T]) StopWatch(ctx context.Context) (bool, error)
- func (l *Loader[T]) Watch(ctx context.Context, name string) error
- type OperationType
- type WatcherAdapter
- type WatcherFunc
- type WatcherRegistry
Examples ¶
Constants ¶
const ( // DefaultInstanceName is the default instance name for instance usage. DefaultInstanceName = "config" // DefaultConfigFileName is the default configuration file name. DefaultConfigFileName = "config" )
const ( // ContextKeyFileName is the context key for file name ContextKeyFileName gctx.StrKey = "fileName" // ContextKeyFilePath is the context key for file path ContextKeyFilePath gctx.StrKey = "filePath" // ContextKeyFileType is the context key for file type ContextKeyFileType gctx.StrKey = "fileType" // ContextKeyOperation is the context key for operation type ContextKeyOperation gctx.StrKey = "operation" // ContextKeyKey is the context key for key ContextKeyKey gctx.StrKey = "key" // ContextKeyValue is the context key for value ContextKeyValue gctx.StrKey = "value" // ContextKeyContent is the context key for set content ContextKeyContent gctx.StrKey = "content" )
Context key constants for configuration operations.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Adapter ¶
type Adapter interface {
// Available checks and returns the backend configuration service is available.
// The optional parameter `resource` specifies certain configuration resource.
//
// Note that this function does not return error as it just does simply check for
// backend configuration service.
Available(ctx context.Context, resource ...string) (ok bool)
// Get retrieves and returns value by specified `pattern` in current resource.
// Pattern like:
// "x.y.z" for map item.
// "x.0.y" for slice item.
Get(ctx context.Context, pattern string) (value any, err error)
// Data retrieves and returns all configuration data in current resource as map.
// Note that this function may lead lots of memory usage if configuration data is too large,
// you can implement this function if necessary.
Data(ctx context.Context) (data map[string]any, err error)
}
Adapter is the interface for configuration retrieving.
type AdapterContent ¶ added in v2.5.3
type AdapterContent struct {
// contains filtered or unexported fields
}
AdapterContent implements interface Adapter using content. The configuration content supports the coding types as package `gjson`.
func NewAdapterContent ¶ added in v2.5.3
func NewAdapterContent(content ...string) (*AdapterContent, error)
NewAdapterContent returns a new configuration management object using custom content. The parameter `content` specifies the default configuration content for reading.
func (*AdapterContent) AddWatcher ¶ added in v2.9.5
func (a *AdapterContent) AddWatcher(name string, fn WatcherFunc)
AddWatcher adds a watcher for the specified configuration file.
func (*AdapterContent) Available ¶ added in v2.5.3
func (a *AdapterContent) Available(ctx context.Context, resource ...string) (ok bool)
Available checks and returns the backend configuration service is available. The optional parameter `resource` specifies certain configuration resource.
Note that this function does not return error as it just does simply check for backend configuration service.
func (*AdapterContent) Data ¶ added in v2.5.3
Data retrieves and returns all configuration data in current resource as map. Note that this function may lead lots of memory usage if configuration data is too large, you can implement this function if necessary.
func (*AdapterContent) Get ¶ added in v2.5.3
Get retrieves and returns value by specified `pattern` in current resource. Pattern like: "x.y.z" for map item. "x.0.y" for slice item.
func (*AdapterContent) GetWatcherNames ¶ added in v2.9.5
func (a *AdapterContent) GetWatcherNames() []string
GetWatcherNames returns all watcher names.
func (*AdapterContent) IsWatching ¶ added in v2.10.0
func (a *AdapterContent) IsWatching(name string) bool
IsWatching checks and returns whether the specified `name` is watching.
func (*AdapterContent) RemoveWatcher ¶ added in v2.9.5
func (a *AdapterContent) RemoveWatcher(name string)
RemoveWatcher removes the watcher for the specified configuration file.
func (*AdapterContent) SetContent ¶ added in v2.5.3
func (a *AdapterContent) SetContent(content string) error
SetContent sets customized configuration content for specified `file`. The `file` is unnecessary param, default is DefaultConfigFile.
type AdapterContentCtx ¶ added in v2.9.5
type AdapterContentCtx struct {
// Ctx is the context with configuration values
Ctx context.Context
}
AdapterContentCtx is the context for AdapterContent.
func GetAdapterContentCtx ¶ added in v2.9.5
func GetAdapterContentCtx(ctx context.Context) *AdapterContentCtx
GetAdapterContentCtx creates and returns an AdapterContentCtx with the given context.
func NewAdapterContentCtx ¶ added in v2.9.5
func NewAdapterContentCtx(ctx ...context.Context) *AdapterContentCtx
NewAdapterContentCtx creates and returns a new AdapterContentCtx. If ctx is provided, it uses that context, otherwise it creates a background context.
func NewAdapterContentCtxWithCtx ¶ added in v2.9.5
func NewAdapterContentCtxWithCtx(ctx context.Context) *AdapterContentCtx
NewAdapterContentCtxWithCtx creates and returns a new AdapterContentCtx with the given context.
func (*AdapterContentCtx) GetContent ¶ added in v2.9.5
func (a *AdapterContentCtx) GetContent() string
GetContent retrieves the content from the context. Returns empty string if not found.
func (*AdapterContentCtx) GetOperation ¶ added in v2.9.5
func (a *AdapterContentCtx) GetOperation() OperationType
GetOperation retrieves the operation from the context. Returns empty string if not found.
func (*AdapterContentCtx) WithContent ¶ added in v2.9.5
func (a *AdapterContentCtx) WithContent(content string) *AdapterContentCtx
WithContent sets the content in the context and returns the updated AdapterContentCtx.
func (*AdapterContentCtx) WithOperation ¶ added in v2.9.5
func (a *AdapterContentCtx) WithOperation(operation OperationType) *AdapterContentCtx
WithOperation sets the operation in the context and returns the updated AdapterContentCtx.
type AdapterFile ¶
type AdapterFile struct {
// contains filtered or unexported fields
}
AdapterFile implements interface Adapter using file.
func NewAdapterFile ¶
func NewAdapterFile(fileNameOrPath ...string) (*AdapterFile, error)
NewAdapterFile returns a new configuration management object. The parameter `file` specifies the default configuration file name for reading.
func (*AdapterFile) AddPath ¶
func (a *AdapterFile) AddPath(directoryPaths ...string) (err error)
AddPath adds an absolute or relative `directory` path to the search paths.
Note that this parameter is paths to a directories not files.
func (*AdapterFile) AddWatcher ¶ added in v2.9.5
func (a *AdapterFile) AddWatcher(name string, fn WatcherFunc)
AddWatcher adds a watcher for the specified configuration file.
func (*AdapterFile) Available ¶
func (a *AdapterFile) Available(ctx context.Context, fileName ...string) bool
Available checks and returns whether configuration of given `file` is available.
func (*AdapterFile) Clear ¶
func (a *AdapterFile) Clear()
Clear removes all parsed configuration files content cache, which will force reload configuration content from the file.
func (*AdapterFile) ClearContent ¶
func (a *AdapterFile) ClearContent()
ClearContent removes all global configuration contents.
func (*AdapterFile) Dump ¶
func (a *AdapterFile) Dump()
Dump prints current JSON object with more manually readable.
func (*AdapterFile) Get ¶
Get retrieves and returns value by specified `pattern`. It returns all values of the current JSON object if `pattern` is given empty or string ".". It returns nil if no value found by `pattern`.
We can also access slice item by its index number in `pattern` like: "list.10", "array.0.name", "array.0.1.id".
It returns a default value specified by `def` if value for `pattern` is not found.
func (*AdapterFile) GetContent ¶
func (a *AdapterFile) GetContent(fileNameOrPath ...string) string
GetContent returns customized configuration content for specified `file`. The `file` is unnecessary param, default is DefaultConfigFile.
func (*AdapterFile) GetFileName ¶
func (a *AdapterFile) GetFileName() string
GetFileName returns the default configuration file name.
func (*AdapterFile) GetFilePath ¶
func (a *AdapterFile) GetFilePath(fileNameOrPath ...string) (filePath string, err error)
GetFilePath returns the absolute configuration file path for the given filename by `file`. If `file` is not passed, it returns the configuration file path of the default name. It returns an empty `path` string and an error if the given `file` does not exist.
func (*AdapterFile) GetPaths ¶ added in v2.0.5
func (a *AdapterFile) GetPaths() []string
GetPaths returns the searching directory path array of current configuration manager.
func (*AdapterFile) GetWatcherNames ¶ added in v2.9.5
func (a *AdapterFile) GetWatcherNames() []string
GetWatcherNames returns all watcher names.
func (*AdapterFile) IsWatching ¶ added in v2.10.0
func (a *AdapterFile) IsWatching(name string) bool
IsWatching checks and returns whether the specified `name` is watching.
func (*AdapterFile) RemoveContent ¶
func (a *AdapterFile) RemoveContent(fileNameOrPath ...string)
RemoveContent removes the global configuration with specified `file`. If `name` is not passed, it removes configuration of the default group name.
func (*AdapterFile) RemoveWatcher ¶ added in v2.9.5
func (a *AdapterFile) RemoveWatcher(name string)
RemoveWatcher removes the watcher for the specified configuration file.
func (*AdapterFile) Set ¶ added in v2.0.5
func (a *AdapterFile) Set(pattern string, value any) error
Set sets value with specified `pattern`. It supports hierarchical data access by char separator, which is '.' in default. It is commonly used to update certain configuration values in runtime. Note that it is not recommended using `Set` configuration at runtime as the configuration would be automatically refreshed if the underlying configuration file changed.
func (*AdapterFile) SetContent ¶
func (a *AdapterFile) SetContent(content string, fileNameOrPath ...string)
SetContent sets customized configuration content for specified `file`. The `file` is unnecessary param, default is DefaultConfigFile.
func (*AdapterFile) SetFileName ¶
func (a *AdapterFile) SetFileName(fileNameOrPath string)
SetFileName sets the default configuration file name.
func (*AdapterFile) SetPath ¶
func (a *AdapterFile) SetPath(directoryPath string) (err error)
SetPath sets the configuration `directory` path for file search. The parameter `path` can be absolute or relative `directory` path, but absolute `directory` path is strongly recommended.
Note that this parameter is a path to a directory not a file.
func (*AdapterFile) SetViolenceCheck ¶
func (a *AdapterFile) SetViolenceCheck(check bool)
SetViolenceCheck sets whether to perform hierarchical conflict checking. This feature needs to be enabled when there is a level symbol in the key name. It is off in default.
Note that turning on this feature is quite expensive, and it is not recommended allowing separators in the key names. It is best to avoid this on the application side.
type AdapterFileCtx ¶ added in v2.9.5
AdapterFileCtx is the context for AdapterFile.
func GetAdapterFileCtx ¶ added in v2.9.5
func GetAdapterFileCtx(ctx context.Context) *AdapterFileCtx
GetAdapterFileCtx creates and returns an AdapterFileCtx with the given context.
func NewAdapterFileCtx ¶ added in v2.9.5
func NewAdapterFileCtx(ctx ...context.Context) *AdapterFileCtx
NewAdapterFileCtx creates and returns a new AdapterFileCtx. If ctx is provided, it uses that context, otherwise it creates a background context.
func NewAdapterFileCtxWithCtx ¶ added in v2.9.5
func NewAdapterFileCtxWithCtx(ctx context.Context) *AdapterFileCtx
NewAdapterFileCtxWithCtx creates and returns a new AdapterFileCtx with the given context.
func (*AdapterFileCtx) GetContent ¶ added in v2.9.5
func (a *AdapterFileCtx) GetContent() *gvar.Var
GetContent retrieves the set content from the context. Returns nil if not found.
func (*AdapterFileCtx) GetFileName ¶ added in v2.9.5
func (a *AdapterFileCtx) GetFileName() string
GetFileName retrieves the file name from the context. Returns empty string if not found.
func (*AdapterFileCtx) GetFilePath ¶ added in v2.9.5
func (a *AdapterFileCtx) GetFilePath() string
GetFilePath retrieves the file path from the context. Returns empty string if not found.
func (*AdapterFileCtx) GetFileType ¶ added in v2.9.5
func (a *AdapterFileCtx) GetFileType() string
GetFileType retrieves the file type from the context. Returns empty string if not found.
func (*AdapterFileCtx) GetKey ¶ added in v2.9.5
func (a *AdapterFileCtx) GetKey() string
GetKey retrieves the key from the context. Returns empty string if not found.
func (*AdapterFileCtx) GetOperation ¶ added in v2.9.5
func (a *AdapterFileCtx) GetOperation() OperationType
GetOperation retrieves the operation from the context. Returns empty string if not found.
func (*AdapterFileCtx) GetValue ¶ added in v2.9.5
func (a *AdapterFileCtx) GetValue() *gvar.Var
GetValue retrieves the value from the context. Returns nil if not found.
func (*AdapterFileCtx) WithContent ¶ added in v2.9.5
func (a *AdapterFileCtx) WithContent(content any) *AdapterFileCtx
WithContent sets the content in the context and returns the updated AdapterFileCtx.
func (*AdapterFileCtx) WithFileName ¶ added in v2.9.5
func (a *AdapterFileCtx) WithFileName(fileName string) *AdapterFileCtx
WithFileName sets the file name in the context and returns the updated AdapterFileCtx.
func (*AdapterFileCtx) WithFilePath ¶ added in v2.9.5
func (a *AdapterFileCtx) WithFilePath(filePath string) *AdapterFileCtx
WithFilePath sets the file path in the context and returns the updated AdapterFileCtx.
func (*AdapterFileCtx) WithFileType ¶ added in v2.9.5
func (a *AdapterFileCtx) WithFileType(fileType string) *AdapterFileCtx
WithFileType sets the file type in the context and returns the updated AdapterFileCtx.
func (*AdapterFileCtx) WithKey ¶ added in v2.9.5
func (a *AdapterFileCtx) WithKey(key string) *AdapterFileCtx
WithKey sets the key in the context and returns the updated AdapterFileCtx.
func (*AdapterFileCtx) WithOperation ¶ added in v2.9.5
func (a *AdapterFileCtx) WithOperation(operation OperationType) *AdapterFileCtx
WithOperation sets the operation in the context and returns the updated AdapterFileCtx.
func (*AdapterFileCtx) WithValue ¶ added in v2.9.5
func (a *AdapterFileCtx) WithValue(value any) *AdapterFileCtx
WithValue sets the value in the context and returns the updated AdapterFileCtx.
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config is the configuration management object.
Example (NewWithAdapter) ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/os/gcfg"
"github.com/gogf/gf/v2/os/gctx"
)
func main() {
var (
ctx = gctx.New()
content = `{"a":"b", "c":1}`
adapter, err = gcfg.NewAdapterContent(content)
)
if err != nil {
panic(err)
}
config := gcfg.NewWithAdapter(adapter)
fmt.Println(config.MustGet(ctx, "a"))
fmt.Println(config.MustGet(ctx, "c"))
}
Output: b 1
func Instance ¶
Instance returns an instance of Config with default settings. The parameter `name` is the name for the instance. But very note that, if the file "name.toml" exists in the configuration directory, it then sets it as the default configuration file. The toml file type is the default configuration file type.
func NewWithAdapter ¶
NewWithAdapter creates and returns a Config object with given adapter.
func (*Config) Available ¶
Available checks and returns the configuration service is available. The optional parameter `pattern` specifies certain configuration resource.
It returns true if configuration file is present in default AdapterFile, or else false. Note that this function does not return error as it just does simply check for backend configuration service.
func (*Config) Get ¶
Get retrieves and returns value by specified `pattern`. It returns all values of current Json object if `pattern` is given empty or string ".". It returns nil if no value found by `pattern`.
It returns a default value specified by `def` if value for `pattern` is not found.
func (*Config) GetAdapter ¶
GetAdapter returns the adapter of the current Config object.
func (*Config) GetWithCmd ¶
GetWithCmd returns the configuration value specified by pattern `pattern`. If the configuration value does not exist, then it retrieves and returns the command line option specified by `key`. It returns the default value `def` if none of them exists.
Fetching Rules: Command line arguments are in lowercase format, eg: gf.package.variable.
Example ¶
package main
import (
"fmt"
"os"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcmd"
"github.com/gogf/gf/v2/os/gctx"
)
func main() {
var (
key = `cmd.test`
ctx = gctx.New()
)
v, err := g.Cfg().GetWithCmd(ctx, key)
if err != nil {
panic(err)
}
fmt.Printf("cmd:%s\n", v)
// Re-Initialize custom command arguments.
os.Args = append(os.Args, fmt.Sprintf(`--%s=yes`, key))
gcmd.Init(os.Args...)
// Retrieve the configuration and command option again.
v, err = g.Cfg().GetWithCmd(ctx, key)
if err != nil {
panic(err)
}
fmt.Printf("cmd:%s", v)
}
Output: cmd: cmd:yes
func (*Config) GetWithEnv ¶
GetWithEnv returns the configuration value specified by pattern `pattern`. If the configuration value does not exist, then it retrieves and returns the environment value specified by `key`. It returns the default value `def` if none of them exists.
Fetching Rules: Environment arguments are in uppercase format, eg: GF_PACKAGE_VARIABLE.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/os/genv"
)
func main() {
var (
key = `ENV_TEST`
ctx = gctx.New()
)
v, err := g.Cfg().GetWithEnv(ctx, key)
if err != nil {
panic(err)
}
fmt.Printf("env:%s\n", v)
if err = genv.Set(key, "gf"); err != nil {
panic(err)
}
v, err = g.Cfg().GetWithEnv(ctx, key)
if err != nil {
panic(err)
}
fmt.Printf("env:%s", v)
}
Output: env: env:gf
func (*Config) MustGetWithCmd ¶
MustGetWithCmd acts as function GetWithCmd, but it panics if error occurs.
func (*Config) MustGetWithEnv ¶
MustGetWithEnv acts as function GetWithEnv, but it panics if error occurs.
func (*Config) SetAdapter ¶
SetAdapter sets the adapter of the current Config object.
type Loader ¶ added in v2.10.0
type Loader[T any] struct { // contains filtered or unexported fields }
Loader is a generic configuration manager that provides configuration loading, watching and management similar to Spring Boot's @ConfigurationProperties
func NewLoader ¶ added in v2.10.0
NewLoader creates a new Loader instance config: the configuration instance to watch for changes propertyKey: the property key pattern to watch (use "" or "." to watch all configuration) targetStruct: pointer to the struct that will receive the configuration values
func NewLoaderWithAdapter ¶ added in v2.10.0
func NewLoaderWithAdapter[T any](adapter Adapter, propertyKey string, targetStruct ...*T) *Loader[T]
NewLoaderWithAdapter creates a new Loader instance adapter: the adapter instance to use for loading and watching configuration propertyKey: the property key pattern to watch (use "" or "." to watch all configuration) targetStruct: pointer to the struct that will receive the configuration values
func (*Loader[T]) Get ¶ added in v2.10.0
func (l *Loader[T]) Get() T
Get returns the current configuration struct This method is thread-safe and returns a copy of the current configuration
func (*Loader[T]) GetPointer ¶ added in v2.10.0
func (l *Loader[T]) GetPointer() *T
GetPointer returns a pointer to the current configuration struct This method is thread-safe and returns a pointer to the current configuration The returned pointer is safe for read operations but should not be modified
func (*Loader[T]) IsWatching ¶ added in v2.10.0
IsWatching returns true if the loader is currently watching for configuration changes
func (*Loader[T]) Load ¶ added in v2.10.0
Load loads configuration from the config instance and binds it to the target struct The context is passed to the underlying configuration adapter
func (*Loader[T]) MustLoadAndWatch ¶ added in v2.10.0
MustLoadAndWatch is a convenience method that calls MustLoad and MustWatch
func (*Loader[T]) MustWatch ¶ added in v2.10.0
MustWatch is like Watch but panics if there is an error
func (*Loader[T]) OnChange ¶ added in v2.10.0
OnChange sets the callback function that will be called when configuration changes The callback function receives the updated configuration struct and can return an error
func (*Loader[T]) SetConverter ¶ added in v2.10.0
SetConverter sets a custom converter function that will be used during Load operations The converter function receives the source data and the target struct pointer
func (*Loader[T]) SetReuseTargetStruct ¶ added in v2.10.0
SetReuseTargetStruct sets whether to reuse the same target struct or create a new one on updates
func (*Loader[T]) SetWatchErrorHandler ¶ added in v2.10.0
SetWatchErrorHandler sets an error handling function that will be called when Load operations fail during Watch
func (*Loader[T]) StopWatch ¶ added in v2.10.0
StopWatch stops watching for configuration changes and removes the associated watcher
type OperationType ¶ added in v2.9.5
type OperationType string
OperationType defines the type for configuration operation.
const ( // OperationSet represents set operation OperationSet OperationType = "set" // OperationWrite represents write operation OperationWrite OperationType = "write" // OperationRename represents rename operation OperationRename OperationType = "rename" // OperationRemove represents remove operation OperationRemove OperationType = "remove" // OperationCreate represents create operation OperationCreate OperationType = "create" // OperationChmod represents chmod operation OperationChmod OperationType = "chmod" // OperationClear represents clear operation OperationClear OperationType = "clear" // OperationUpdate represents update operation OperationUpdate OperationType = "update" )
Operation constants for configuration operations.
type WatcherAdapter ¶ added in v2.9.5
type WatcherAdapter interface {
// AddWatcher adds a watcher function for specified `pattern` and `resource`.
AddWatcher(name string, fn WatcherFunc)
// RemoveWatcher removes the watcher function for specified `pattern` and `resource`.
RemoveWatcher(name string)
// GetWatcherNames returns all watcher names.
GetWatcherNames() []string
// IsWatching checks and returns whether the specified `pattern` is watching.
IsWatching(name string) bool
}
WatcherAdapter is the interface for configuration watcher.
type WatcherFunc ¶ added in v2.10.0
WatcherFunc is the callback function type for configuration watchers.
type WatcherRegistry ¶ added in v2.9.5
type WatcherRegistry struct {
// contains filtered or unexported fields
}
WatcherRegistry is a helper type for managing configuration watchers. It provides a unified implementation of watcher management to avoid code duplication across different adapter implementations.
func NewWatcherRegistry ¶ added in v2.9.5
func NewWatcherRegistry() *WatcherRegistry
NewWatcherRegistry creates and returns a new WatcherRegistry instance.
func (*WatcherRegistry) Add ¶ added in v2.9.5
func (r *WatcherRegistry) Add(name string, fn WatcherFunc)
Add adds a watcher with the specified name and callback function.
func (*WatcherRegistry) GetNames ¶ added in v2.9.5
func (r *WatcherRegistry) GetNames() []string
GetNames returns all watcher names.
func (*WatcherRegistry) IsWatching ¶ added in v2.10.0
func (r *WatcherRegistry) IsWatching(name string) bool
IsWatching checks whether the watcher with the specified name is registered.
func (*WatcherRegistry) Notify ¶ added in v2.9.5
func (r *WatcherRegistry) Notify(ctx context.Context)
Notify notifies all registered watchers by calling their callback functions. Each callback is executed in a separate goroutine with panic recovery to prevent one watcher's panic from affecting others.
func (*WatcherRegistry) Remove ¶ added in v2.9.5
func (r *WatcherRegistry) Remove(name string)
Remove removes the watcher with the specified name.