mcfg

package
v0.1.21 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2025 License: MIT Imports: 10 Imported by: 2

Documentation

Index

Constants

View Source
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 IsExist

func IsExist(path string) bool

IsExist checks if the file exists.

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) Data

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

Data gets all configuration data.

func (*AdapterFile) Get

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

Get gets the configuration value.

func (*AdapterFile) MergeConfigMap added in v0.1.1

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

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

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) 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]interface{}) (map[string]interface{}, error)

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.

Jump to

Keyboard shortcuts

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