config_manager 0.3.0
config_manager: ^0.3.0 copied to clipboard
A JSON-based configuration manager with path-based access to nested values, singleton pattern, and mixin support.
Config Manager #
A Dart configuration manager package that loads JSON configuration files and provides path-based access to configuration values.
Features #
- Load configuration from JSON files, raw JSON strings, or in-memory maps
- Access nested values via an explicit key path (
List<String>), e.g.['database', 'host']— a single top-level key is a one-element list, e.g.['appName']; an empty list[]addresses the whole configuration map - List support with index access (e.g.
['features', '0']) - Sector-based configuration, so multiple independent configs can coexist
- Check whether a sector is loaded (
isLoaded) or whether a set of key paths is present (containsKeys) - Skip reloading an already-loaded sector with
force: false - Singleton-based configuration access
- Mixin-based configuration extension for classes
Usage #
import 'package:config_manager/config_manager.dart';
void main() {
final config = ConfigManagerSingleton();
config.loadFromJson('config.json');
print(config.get(['appName']));
print(config.get(['database', 'host']));
print(config[['database', 'credentials', 'user']]);
print(config.isLoaded());
print(config.containsKeys([
['appName'],
['database', 'host'],
]));
// force: false is a no-op when the sector is already loaded.
config.loadFromJson('config.json', force: false);
// A key path is always an explicit list of segments, so a literal "."
// inside a key name is never ambiguous with nesting.
config.set(['database.host'], 'literal-value');
print(config.get(['database.host']));
}
Examples #
The example/ folder has one focused file per topic, each runnable on its
own (e.g. dart run example/sectors_example.dart). example/main.dart runs
all of them in sequence.
basic_usage.dart— loading a JSON file, reading valueslists_and_paths.dart— lists, casting, index accesswrites_and_edge_cases.dart— writing nested values, missing keyssectors_example.dart— independent per-sector configurationis_loaded_and_contains_keys.dart—isLoadedandcontainsKeysforce_reload.dart—force: falseno-op reloadnested_vs_flat_keys.dart— nested paths vs. a literal dotted keymixin_extension.dart—ConfigExtensionmixincustom_extension.dart— a customIConfigExtensionimplementation
License #
MIT