MSX Configuration Module
MSX configuration is a spring-compatible dynamic configuration library. It includes support for:
- remote configuration stores
- dynamic configuration updates
- JSON, JSON5, YAML, INI and Properties files
- key normalization
- structure population
Model
MSX Configuration has three main components: providers, settings, and the config object.
- Providers load settings for your application. This could be from a file, environment variables, or some other source of configuration.
- Settings represent the configuration options for your application. Settings are represented as key/value pairs.
- Config holds all of the providers and loaded settings. This object allows you to load, watch, retrieve, apply and convert your settings.
Each provider combines its contents together into a single keyspace; these are then superposed (like a "1000-layer lasagna") in order to produce a final combined keyspace and key/value mapping.
Quick Start
Instantiation
When using MSX Configuration inside the MSX Application context, you can retrieve the configuration object from the ctx context.Context:
cfg := config.MustFromContext(ctx)
When using MSX Configuration outside of the MSX Application context, you can instantiate your own providers. For example, to consume the environment variables from the current process:
environmentProvider := config.NewEnvironment("env")
cfg := config.NewConfig(environmentProvider)
Value Retrieval
Using one of the above cfg objects, you can retrieve the user's home directory from the HOME environment variable. Note that all config keys are normalized to be lowercase, no hyphens, period-separated. This means the HOME environment variable will be mapped to home:
homePath, err := cfg.String("home")
The cfg object presents a number of functions to return a strongly-typed value:
String(key string)Int(key string)Float(key string)Bool(key string)
These functions will look up the specified key in the configuration, and if found, will attempt to convert the value to the specified type. If the key is not found or configuration has not yet been loaded, an appropriate error will be returned.
If you wish to use an alternative (default) value in case the lookup fails, you can use the Or functions:
StringOr(key string, other string)Int(key string, other int)Float(key string, other float)Bool(key string, other bool)
The specified other value will be returned if the config has been loaded, but lookup fails:
buildPath, err := cfg.StringOr("build.path", "./build")
Structure Population
You can also populate appropriately defined structures:
type ConnectionConfig struct {
Name string
Skipped bool `config:"-"`
AnotherName int `config:"somethingelse"`
}
var connectionConfig ConnectionConfig
err := cfg.Populate(&connectionConfig, "some.connection")
Each structure field is treated a little differently based on the contents/existence of the config struct tag:
Name: populated fromsome.connection.name(default behaviour)Skipped: not populated due to theconfig:"-"(omit when source name is a hyphen)AnotherName: populated fromsome.connection.somethingelse(overridden field name)
Spring Compatibility
One of the primary goals for MSX Configuration is close compatibility with Spring-style configuration. Several known incompatibilities and limitations currently exist:
- Key Normalization
- Configuration keys in MSX Configuration are simply normalized to be lowercase, no hyphens, period-separated. As of Spring 2.0, configuration keys are expected to be snake-case, period-separated. MSX Configuration cannot distinguish between the
app.some-dataandapp.somedatakeys, and normalizes them both toapp.somedata.
- Configuration keys in MSX Configuration are simply normalized to be lowercase, no hyphens, period-separated. As of Spring 2.0, configuration keys are expected to be snake-case, period-separated. MSX Configuration cannot distinguish between the
- Arbitrary Population
- MSX Configuration currently supports
@ConfigurationPropertiesstyle structure population. As a consequence, all data used to populate a structure must be direct descendants of the key used to populate the structure. We intend to support arbitrary key specification for structures in the future.
- MSX Configuration currently supports
Built-In Providers
MSX Configuration has many built-in providers, allowing the application to unify configuration from a wide variety of sources:
INIFile- Loads settings from a.inifileJSONFile- Loads settings from a.jsonor.json5fileYAMLFile- Loads settings from a.yamlor.ymlfileTOMLFile- Loads settings from a.tomlfilePropertiesFile- Loads settings from a.propertiesfileCobraProvider- Loads settings from a Cobra command contextPFlagProvider- Loads settings from PFlag flagsetGoFlagProvider- Loads settings from a go flag flagsetConsulProvider- Loads settings from ConsulVaultProvider- Loads settings from VaultEnvironment- Loads settings from environment variablesStatic- Loads settings from an in-memory map
Helpers
Along with the above providers, there are some wrappers for managing config lifecycle:
CachedLoader- Caches settings in memory until flushed byInvalidate()OnceLoader- Caches settings in memory permanentlyResolver- Remaps settings from one key to another