2022-09-24 00:13:08 +02:00
|
|
|
/**
|
|
|
|
* Authors:
|
|
|
|
* Mike Bierlee, m.bierlee@lostmoment.com
|
|
|
|
* Copyright: 2022 Mike Bierlee
|
|
|
|
* License:
|
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* The full terms of the license can be found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2022-09-23 22:34:46 +02:00
|
|
|
module poodinis.config.dictionary;
|
|
|
|
|
2022-09-24 00:13:08 +02:00
|
|
|
import std.exception : enforce;
|
|
|
|
import std.string : split, startsWith, endsWith;
|
|
|
|
import std.conv : to, ConvException;
|
|
|
|
|
|
|
|
class ConfigReadException : Exception {
|
|
|
|
this(string msg, string file = __FILE__, size_t line = __LINE__) {
|
|
|
|
super(msg, file, line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class PathParseException : Exception {
|
|
|
|
this(string msg, string path, string file = __FILE__, size_t line = __LINE__) {
|
|
|
|
string fullMsg = msg ~ " (Path: " ~ path ~ ")";
|
|
|
|
super(fullMsg, file, line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-23 22:34:46 +02:00
|
|
|
interface ConfigNode {
|
|
|
|
}
|
|
|
|
|
2022-09-24 00:15:16 +02:00
|
|
|
class ValueNode : ConfigNode {
|
2022-09-23 22:34:46 +02:00
|
|
|
string value;
|
|
|
|
|
|
|
|
this() {
|
|
|
|
}
|
|
|
|
|
|
|
|
this(string value) {
|
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-24 00:15:16 +02:00
|
|
|
class ObjectNode : ConfigNode {
|
2022-09-23 22:34:46 +02:00
|
|
|
ConfigNode[string] children;
|
|
|
|
|
|
|
|
this() {
|
|
|
|
}
|
|
|
|
|
|
|
|
this(ConfigNode[string] children) {
|
|
|
|
this.children = children;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-24 00:15:16 +02:00
|
|
|
class ArrayNode : ConfigNode {
|
2022-09-23 22:34:46 +02:00
|
|
|
ConfigNode[] children;
|
|
|
|
|
|
|
|
this() {
|
|
|
|
}
|
|
|
|
|
|
|
|
this(ConfigNode[] children...) {
|
|
|
|
this.children = children;
|
|
|
|
}
|
2022-09-24 00:13:08 +02:00
|
|
|
|
|
|
|
this(string[] values...) {
|
|
|
|
foreach (string value; values) {
|
2022-09-24 00:15:16 +02:00
|
|
|
children ~= new ValueNode(value);
|
2022-09-24 00:13:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class PathSegment {
|
|
|
|
}
|
|
|
|
|
|
|
|
class ArrayPathSegment : PathSegment {
|
|
|
|
const size_t index;
|
|
|
|
|
|
|
|
this(const size_t index) {
|
|
|
|
this.index = index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ConfigPath {
|
|
|
|
private const string path;
|
|
|
|
private string[] segments;
|
|
|
|
|
|
|
|
this(const string path) {
|
|
|
|
this.path = path;
|
|
|
|
this.segments = path.split(".");
|
|
|
|
}
|
|
|
|
|
|
|
|
PathSegment getNextSegment() {
|
|
|
|
if (segments.length == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
PathSegment ret(PathSegment segment) {
|
|
|
|
segments = segments.length > 1 ? segments[1 .. $] : [];
|
|
|
|
return segment;
|
|
|
|
}
|
|
|
|
|
|
|
|
string segment = segments[0];
|
|
|
|
|
|
|
|
if (segment.startsWith("[") && segment.endsWith("]")) {
|
|
|
|
if (segment.length <= 2) {
|
|
|
|
throw new PathParseException("Path has array accessor but no index specified", path);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto indexString = segment[1 .. $ - 1];
|
|
|
|
try {
|
|
|
|
auto index = indexString.to!size_t;
|
|
|
|
return ret(new ArrayPathSegment(index));
|
|
|
|
} catch (ConvException e) {
|
|
|
|
throw new PathParseException("Array index '" ~ indexString ~ "' is not acceptable as an array number", path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Exception("Not yet implemented");
|
|
|
|
}
|
2022-09-23 22:34:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class ConfigDictionary {
|
|
|
|
ConfigNode rootNode;
|
2022-09-24 00:13:08 +02:00
|
|
|
|
|
|
|
string get(string configPath) {
|
|
|
|
enforce!ConfigReadException(rootNode !is null, "The config is empty");
|
|
|
|
enforce!ConfigReadException(configPath.length > 0, "Supplied config path is empty");
|
|
|
|
|
|
|
|
if (configPath == ".") {
|
2022-09-24 00:15:16 +02:00
|
|
|
auto rootValue = cast(ValueNode) rootNode;
|
2022-09-24 00:13:08 +02:00
|
|
|
if (rootValue) {
|
|
|
|
return rootValue.value;
|
|
|
|
} else {
|
|
|
|
throw new ConfigReadException("The root of the config is not a value type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto path = new ConfigPath(configPath);
|
|
|
|
auto currentNode = rootNode;
|
|
|
|
PathSegment currentPathSegment = path.getNextSegment();
|
|
|
|
while (currentPathSegment !is null) {
|
|
|
|
auto arrayPath = cast(ArrayPathSegment) currentPathSegment;
|
|
|
|
if (arrayPath) {
|
2022-09-24 00:15:16 +02:00
|
|
|
auto arrayNode = cast(ArrayNode) currentNode;
|
2022-09-24 00:13:08 +02:00
|
|
|
if (arrayNode) {
|
|
|
|
currentNode = arrayNode.children[arrayPath.index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
currentPathSegment = path.getNextSegment();
|
|
|
|
}
|
|
|
|
|
2022-09-24 00:15:16 +02:00
|
|
|
auto value = cast(ValueNode) currentNode;
|
2022-09-24 00:13:08 +02:00
|
|
|
if (value) {
|
|
|
|
return value.value;
|
|
|
|
} else {
|
|
|
|
throw new ConfigReadException(
|
|
|
|
"The configuration at the given path is not a value: " ~ configPath);
|
|
|
|
}
|
|
|
|
}
|
2022-09-23 22:34:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
version (unittest) {
|
2022-09-24 00:13:08 +02:00
|
|
|
import std.exception : assertThrown;
|
|
|
|
|
2022-09-23 22:34:46 +02:00
|
|
|
@("Dictionary creation")
|
|
|
|
unittest {
|
2022-09-24 00:15:16 +02:00
|
|
|
auto root = new ObjectNode([
|
|
|
|
"english": new ArrayNode([new ValueNode("one"), new ValueNode("two")]),
|
|
|
|
"spanish": new ArrayNode(new ValueNode("uno"), new ValueNode("dos"))
|
2022-09-23 22:34:46 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
auto dictionary = new ConfigDictionary();
|
|
|
|
dictionary.rootNode = root;
|
|
|
|
}
|
2022-09-24 00:13:08 +02:00
|
|
|
|
|
|
|
@("Get value in dictionary with empty root fails")
|
|
|
|
unittest {
|
|
|
|
auto dictionary = new ConfigDictionary();
|
|
|
|
|
|
|
|
assertThrown!ConfigReadException(dictionary.get("."));
|
|
|
|
}
|
|
|
|
|
|
|
|
@("Get value in dictionary with empty path fails")
|
|
|
|
unittest {
|
|
|
|
auto dictionary = new ConfigDictionary();
|
2022-09-24 00:15:16 +02:00
|
|
|
dictionary.rootNode = new ValueNode("hehehe");
|
2022-09-24 00:13:08 +02:00
|
|
|
|
|
|
|
assertThrown!ConfigReadException(dictionary.get(""));
|
|
|
|
}
|
|
|
|
|
|
|
|
@("Get value in root")
|
|
|
|
unittest {
|
|
|
|
auto dictionary = new ConfigDictionary();
|
2022-09-24 00:15:16 +02:00
|
|
|
dictionary.rootNode = new ValueNode("yup");
|
2022-09-24 00:13:08 +02:00
|
|
|
|
|
|
|
assert(dictionary.get(".") == "yup");
|
|
|
|
}
|
|
|
|
|
|
|
|
@("Get value in root fails when root is not a value")
|
|
|
|
unittest {
|
|
|
|
auto dictionary = new ConfigDictionary();
|
2022-09-24 00:15:16 +02:00
|
|
|
dictionary.rootNode = new ArrayNode();
|
2022-09-24 00:13:08 +02:00
|
|
|
|
|
|
|
assertThrown!ConfigReadException(dictionary.get("."));
|
|
|
|
}
|
|
|
|
|
|
|
|
@("Get array value from root")
|
|
|
|
unittest {
|
|
|
|
auto dictionary = new ConfigDictionary();
|
2022-09-24 00:15:16 +02:00
|
|
|
dictionary.rootNode = new ArrayNode("aap", "noot", "mies");
|
2022-09-24 00:13:08 +02:00
|
|
|
|
|
|
|
assert(dictionary.get("[0]") == "aap");
|
|
|
|
assert(dictionary.get("[1]") == "noot");
|
|
|
|
assert(dictionary.get("[2]") == "mies");
|
|
|
|
}
|
2022-09-23 22:34:46 +02:00
|
|
|
}
|