mirror of
https://github.com/mbierlee/mirage-config.git
synced 2024-11-15 04:44:01 +01:00
Make it possible to get a value from a root object
This commit is contained in:
parent
cde58e1224
commit
e3673755de
|
@ -49,6 +49,12 @@ class ObjectNode : ConfigNode {
|
|||
this(ConfigNode[string] children) {
|
||||
this.children = children;
|
||||
}
|
||||
|
||||
this(string[string] values) {
|
||||
foreach (key, value; values) {
|
||||
children[key] = new ValueNode(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ArrayNode : ConfigNode {
|
||||
|
@ -79,6 +85,14 @@ class ArrayPathSegment : PathSegment {
|
|||
}
|
||||
}
|
||||
|
||||
class PropertyPathSegment : PathSegment {
|
||||
const string propertyName;
|
||||
|
||||
this(const string propertyName) {
|
||||
this.propertyName = propertyName;
|
||||
}
|
||||
}
|
||||
|
||||
class ConfigPath {
|
||||
private const string path;
|
||||
private string[] segments;
|
||||
|
@ -114,7 +128,7 @@ class ConfigPath {
|
|||
}
|
||||
}
|
||||
|
||||
throw new Exception("Not yet implemented");
|
||||
return ret(new PropertyPathSegment(segment));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -142,10 +156,25 @@ class ConfigDictionary {
|
|||
if (arrayPath) {
|
||||
auto arrayNode = cast(ArrayNode) currentNode;
|
||||
if (arrayNode) {
|
||||
if (arrayNode.children.length < arrayPath.index) {
|
||||
throw new ConfigReadException("Array index out of bounds: " ~ configPath);
|
||||
}
|
||||
|
||||
currentNode = arrayNode.children[arrayPath.index];
|
||||
}
|
||||
}
|
||||
|
||||
auto propertyPath = cast(PropertyPathSegment) currentPathSegment;
|
||||
if (propertyPath) {
|
||||
auto objectNode = cast(ObjectNode) currentNode;
|
||||
if (objectNode) {
|
||||
auto propertyNode = propertyPath.propertyName in objectNode.children;
|
||||
if (propertyNode) {
|
||||
currentNode = *propertyNode;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
currentPathSegment = path.getNextSegment();
|
||||
}
|
||||
|
||||
|
@ -213,4 +242,18 @@ version (unittest) {
|
|||
assert(dictionary.get("[1]") == "noot");
|
||||
assert(dictionary.get("[2]") == "mies");
|
||||
}
|
||||
|
||||
@("Get value from object at root")
|
||||
unittest {
|
||||
auto dictionary = new ConfigDictionary();
|
||||
dictionary.rootNode = new ObjectNode([
|
||||
"aap": "monkey",
|
||||
"noot": "nut",
|
||||
"mies": "mies" // It's a name!
|
||||
]);
|
||||
|
||||
assert(dictionary.get("aap") == "monkey");
|
||||
assert(dictionary.get("noot") == "nut");
|
||||
assert(dictionary.get("mies") == "mies");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue