mirror of
https://github.com/mbierlee/mirage-config.git
synced 2024-11-15 04:44:01 +01:00
Slight refactor
This commit is contained in:
parent
a4b0e7982e
commit
16b430a135
|
@ -170,47 +170,63 @@ class ConfigDictionary {
|
||||||
auto path = new ConfigPath(configPath);
|
auto path = new ConfigPath(configPath);
|
||||||
auto currentNode = rootNode;
|
auto currentNode = rootNode;
|
||||||
PathSegment currentPathSegment = path.getNextSegment();
|
PathSegment currentPathSegment = path.getNextSegment();
|
||||||
|
|
||||||
string createExceptionPath() {
|
string createExceptionPath() {
|
||||||
return "'" ~ configPath ~ "' (at '" ~ path.getCurrentPath() ~ "')";
|
return "'" ~ configPath ~ "' (at '" ~ path.getCurrentPath() ~ "')";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void throwPathNotExists() {
|
||||||
|
throw new ConfigReadException("Path does not exist: " ~ createExceptionPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
void ifNotNullPointer(void* obj, void delegate() fn) {
|
||||||
|
if (obj) {
|
||||||
|
fn();
|
||||||
|
} else {
|
||||||
|
throwPathNotExists();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ifNotNull(Object obj, void delegate() fn) {
|
||||||
|
if (obj) {
|
||||||
|
fn();
|
||||||
|
} else {
|
||||||
|
throwPathNotExists();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
while (currentPathSegment !is null) {
|
while (currentPathSegment !is null) {
|
||||||
if (currentNode is null) {
|
if (currentNode is null) {
|
||||||
throw new ConfigReadException(
|
throwPathNotExists();
|
||||||
"Path does not exist: " ~ createExceptionPath());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto valueNode = cast(ValueNode) currentNode;
|
auto valueNode = cast(ValueNode) currentNode;
|
||||||
if (valueNode) {
|
if (valueNode) {
|
||||||
throw new ConfigReadException(
|
throwPathNotExists();
|
||||||
"Path does not exist: " ~ createExceptionPath());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto arrayPath = cast(ArrayPathSegment) currentPathSegment;
|
auto arrayPath = cast(ArrayPathSegment) currentPathSegment;
|
||||||
if (arrayPath) {
|
if (arrayPath) {
|
||||||
auto arrayNode = cast(ArrayNode) currentNode;
|
auto arrayNode = cast(ArrayNode) currentNode;
|
||||||
if (arrayNode) {
|
ifNotNull(arrayNode, {
|
||||||
if (arrayNode.children.length < arrayPath.index) {
|
if (arrayNode.children.length < arrayPath.index) {
|
||||||
throw new ConfigReadException(
|
throw new ConfigReadException(
|
||||||
"Array index out of bounds: " ~ createExceptionPath());
|
"Array index out of bounds: " ~ createExceptionPath());
|
||||||
}
|
}
|
||||||
|
|
||||||
currentNode = arrayNode.children[arrayPath.index];
|
currentNode = arrayNode.children[arrayPath.index];
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
auto propertyPath = cast(PropertyPathSegment) currentPathSegment;
|
auto propertyPath = cast(PropertyPathSegment) currentPathSegment;
|
||||||
if (propertyPath) {
|
if (propertyPath) {
|
||||||
auto objectNode = cast(ObjectNode) currentNode;
|
auto objectNode = cast(ObjectNode) currentNode;
|
||||||
if (objectNode) {
|
ifNotNull(objectNode, {
|
||||||
auto propertyNode = propertyPath.propertyName in objectNode.children;
|
auto propertyNode = propertyPath.propertyName in objectNode.children;
|
||||||
if (propertyNode) {
|
ifNotNullPointer(propertyNode, {
|
||||||
currentNode = *propertyNode;
|
currentNode = *propertyNode;
|
||||||
} else {
|
});
|
||||||
throw new ConfigReadException(
|
});
|
||||||
"Path does not exist: " ~ createExceptionPath());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
currentPathSegment = path.getNextSegment();
|
currentPathSegment = path.getNextSegment();
|
||||||
|
@ -357,4 +373,12 @@ version (unittest) {
|
||||||
assertThrown!ConfigReadException(dictionary.get("hostname"));
|
assertThrown!ConfigReadException(dictionary.get("hostname"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@("Exception is thrown when given path does not exist because config is an array")
|
||||||
|
unittest {
|
||||||
|
auto dictionary = new ConfigDictionary();
|
||||||
|
dictionary.rootNode = new ArrayNode();
|
||||||
|
|
||||||
|
assertThrown!ConfigReadException(dictionary.get("hostname"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue