Add load functions for individual file formats

This commit is contained in:
Mike Bierlee 2022-11-27 17:59:46 +03:00
parent 344dff29de
commit d6dce5df1b
3 changed files with 97 additions and 1 deletions

View file

@ -10,6 +10,9 @@ module poodinis.valueinjector.mirage;
import poodinis : ValueInjector, DependencyContainer, Value, Autowire, existingInstance;
import mirage : ConfigDictionary, mirageLoadConfig = loadConfig;
import mirage.json : mirageLoadJsonConfig = loadJsonConfig;
import mirage.java : mirageLoadJavaConfig = loadJavaConfig;
import mirage.ini : mirageLoadIniConfig = loadIniConfig;
class MirageValueInjector(Type) : ValueInjector!Type
{
@ -76,9 +79,60 @@ public void registerMirageInjectors(shared(DependencyContainer) container)
* Throws: ConfigCreationException when the file's extension is unrecognized.
*/
public void loadConfig(shared(DependencyContainer) container, const string configPath)
{
loadConfigWithLoader(container, configPath, &mirageLoadConfig);
}
/**
* Load a JSON config from disk.
* registerMirageInjectors will be called by this function. The loaded ConfigDictionary will be
* registered and available for injection by itself too.
* Params:
* container = Dependency container to register config and injectors with.
* configPath = Path to the configuration file.
*/
public void loadJsonConfig(shared(DependencyContainer) container, const string configPath)
{
loadConfigWithLoader(container, configPath, &mirageLoadJsonConfig);
}
/**
* Load a Java properties from disk.
* registerMirageInjectors will be called by this function. The loaded ConfigDictionary will be
* registered and available for injection by itself too.
* Params:
* container = Dependency container to register config and injectors with.
* configPath = Path to the configuration file.
*/
public void loadJavaProperties(shared(DependencyContainer) container, const string configPath)
{
loadConfigWithLoader(container, configPath, &mirageLoadJavaConfig);
}
/// ditto
alias loadJavaConfig = loadJavaProperties;
/**
* Load an INI config from disk.
* registerMirageInjectors will be called by this function. The loaded ConfigDictionary will be
* registered and available for injection by itself too.
* Params:
* container = Dependency container to register config and injectors with.
* configPath = Path to the configuration file.
*/
public void loadIniConfig(shared(DependencyContainer) container, const string configPath)
{
loadConfigWithLoader(container, configPath, &mirageLoadIniConfig);
}
private void loadConfigWithLoader(
shared(DependencyContainer) container,
const string configPath,
ConfigDictionary function(const string configPath) loaderFunc
)
{
container.registerMirageInjectors;
auto config = mirageLoadConfig(configPath);
auto config = loaderFunc(configPath);
container.register!ConfigDictionary.existingInstance(config);
}
@ -122,4 +176,38 @@ version (unittest)
assert(testClass.horseName == "Breeeeeezer");
assert(testClass.horseChildCount == 4);
}
@("Load JSON config")
unittest
{
auto dependencies = new shared DependencyContainer;
dependencies.register!TestClass;
dependencies.loadJsonConfig("testfiles/horses.json");
auto testClass = dependencies.resolve!TestClass;
assert(testClass.horseName == "Maaarrrilll");
}
@("Load Java config")
unittest
{
auto dependencies = new shared DependencyContainer;
dependencies.register!TestClass;
dependencies.loadJavaConfig("testfiles/horses.properties");
auto testClass = dependencies.resolve!TestClass;
assert(testClass.horseName == "Beaaaaaaan");
}
@("Load INI config")
unittest
{
auto dependencies = new shared DependencyContainer;
dependencies.register!TestClass;
dependencies.loadIniConfig("testfiles/horses.ini");
auto testClass = dependencies.resolve!TestClass;
assert(testClass.horseName == "Breeeeeezer");
}
}

6
testfiles/horses.json Normal file
View file

@ -0,0 +1,6 @@
{
"horse": {
"name": "Maaarrrilll",
"children": 11
}
}

View file

@ -0,0 +1,2 @@
horse.name = Beaaaaaaan
horse.children = 98