From daf4c9029b9985799c703ae0e1436bc67abf87a2 Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Sun, 25 Sep 2022 19:38:37 +0300 Subject: [PATCH] Add uber convenience function 'loadConfig' --- source/mirage/config.d | 25 ++++++++++++++++++++++++- source/mirage/json.d | 6 +++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/source/mirage/config.d b/source/mirage/config.d index d908ee3..224cc7f 100644 --- a/source/mirage/config.d +++ b/source/mirage/config.d @@ -12,9 +12,12 @@ module mirage.config; import std.exception : enforce; -import std.string : split, startsWith, endsWith, join, lastIndexOf, strip; +import std.string : split, startsWith, endsWith, join, lastIndexOf, strip, toLower; import std.conv : to, ConvException; import std.file : readText; +import std.path : extension; + +import mirage.json : loadJsonConfig; /** * Used by the ConfigDictionary when something goes wrong when reading configuration. @@ -370,6 +373,16 @@ abstract class ConfigFactory { ConfigDictionary parseConfig(string contents); } +ConfigDictionary loadConfig(const string configPath) { + auto extension = configPath.extension.toLower; + if (extension == ".json") { + return loadJsonConfig(configPath); + } + + throw new ConfigCreationException( + "File extension '" ~ extension ~ "' is not recognized as a supported config file format. Please use a specific function to load it, such as 'loadJsonConfig()'"); +} + version (unittest) { import std.exception : assertThrown; import std.math.operations : isClose; @@ -603,4 +616,14 @@ version (unittest) { assert(config.get(" que. pasa hombre ") == "not much"); } + + @("Load configurations using the loadConfig convenience function") + unittest { + auto jsonConfig = loadConfig("testfiles/groot.json"); + + assert(jsonConfig.get("name") == "Groot"); + assert(jsonConfig.get("traits[1]") == "tree"); + assert(jsonConfig.get("age") == "8728"); + assert(jsonConfig.get("taxNumber") == null); + } } diff --git a/source/mirage/json.d b/source/mirage/json.d index c00affe..c8505c9 100644 --- a/source/mirage/json.d +++ b/source/mirage/json.d @@ -103,7 +103,7 @@ class JsonConfigFactory : ConfigFactory { * json = Text contents of the config to be parsed. * Returns: The parsed configuration. */ -ConfigDictionary parseJsonConfig(string json) { +ConfigDictionary parseJsonConfig(const string json) { return new JsonConfigFactory().parseConfig(json); } @@ -114,7 +114,7 @@ ConfigDictionary parseJsonConfig(string json) { * contents = JSONValue config to be parsed. * Returns: The parsed configuration. */ -ConfigDictionary parseJsonConfig(JSONValue json) { +ConfigDictionary parseJsonConfig(const JSONValue json) { return new JsonConfigFactory().parseJson(json); } @@ -125,7 +125,7 @@ ConfigDictionary parseJsonConfig(JSONValue json) { * filePath = Path to the JSON configuration file. * Returns: The loaded configuration. */ -ConfigDictionary loadJsonConfig(string filePath) { +ConfigDictionary loadJsonConfig(const string filePath) { return new JsonConfigFactory().loadFile(filePath); }