mirror of
https://github.com/mbierlee/mirage-config.git
synced 2024-11-15 04:44:01 +01:00
Add file loading
This commit is contained in:
parent
a1a99d1dee
commit
20a7c0471b
|
@ -12,6 +12,7 @@ module mirage.config;
|
||||||
import std.exception : enforce;
|
import std.exception : enforce;
|
||||||
import std.string : split, startsWith, endsWith, join, lastIndexOf;
|
import std.string : split, startsWith, endsWith, join, lastIndexOf;
|
||||||
import std.conv : to, ConvException;
|
import std.conv : to, ConvException;
|
||||||
|
import std.file : readText;
|
||||||
|
|
||||||
class ConfigReadException : Exception {
|
class ConfigReadException : Exception {
|
||||||
this(string msg, string file = __FILE__, size_t line = __LINE__) {
|
this(string msg, string file = __FILE__, size_t line = __LINE__) {
|
||||||
|
@ -265,8 +266,12 @@ class ConfigDictionary {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ConfigFactory {
|
abstract class ConfigFactory {
|
||||||
ConfigDictionary loadFile(string path);
|
ConfigDictionary loadFile(string path) {
|
||||||
|
auto json = readText(path);
|
||||||
|
return parseConfig(json);
|
||||||
|
}
|
||||||
|
|
||||||
ConfigDictionary parseConfig(string contents);
|
ConfigDictionary parseConfig(string contents);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,11 +15,7 @@ import std.conv : to;
|
||||||
import mirage.config : ConfigFactory, ConfigDictionary, ConfigNode, ValueNode, ObjectNode, ArrayNode, ConfigCreationException;
|
import mirage.config : ConfigFactory, ConfigDictionary, ConfigNode, ValueNode, ObjectNode, ArrayNode, ConfigCreationException;
|
||||||
|
|
||||||
class JsonConfigFactory : ConfigFactory {
|
class JsonConfigFactory : ConfigFactory {
|
||||||
ConfigDictionary loadFile(string path) {
|
override ConfigDictionary parseConfig(string contents) {
|
||||||
throw new Exception("not yet implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
ConfigDictionary parseConfig(string contents) {
|
|
||||||
return parseJson(parseJSON(contents));
|
return parseJson(parseJSON(contents));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,4 +126,15 @@ version (unittest) {
|
||||||
assert(config.get("age") == "8728");
|
assert(config.get("age") == "8728");
|
||||||
assert(config.get("taxNumber") == null);
|
assert(config.get("taxNumber") == null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@("Load JSON file")
|
||||||
|
unittest {
|
||||||
|
auto loader = new JsonConfigFactory();
|
||||||
|
auto config = loader.loadFile("testfiles/groot.json");
|
||||||
|
|
||||||
|
assert(config.get("name") == "Groot");
|
||||||
|
assert(config.get("traits[1]") == "tree");
|
||||||
|
assert(config.get("age") == "8728");
|
||||||
|
assert(config.get("taxNumber") == null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue