From 16b430a1354e91d405b05b78d2418e75050908be Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Sat, 24 Sep 2022 03:18:49 +0300 Subject: [PATCH] Slight refactor --- source/poodinis/config/dictionary.d | 50 +++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/source/poodinis/config/dictionary.d b/source/poodinis/config/dictionary.d index 5b782d8..854e81b 100644 --- a/source/poodinis/config/dictionary.d +++ b/source/poodinis/config/dictionary.d @@ -170,47 +170,63 @@ class ConfigDictionary { auto path = new ConfigPath(configPath); auto currentNode = rootNode; PathSegment currentPathSegment = path.getNextSegment(); + string createExceptionPath() { 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) { if (currentNode is null) { - throw new ConfigReadException( - "Path does not exist: " ~ createExceptionPath()); + throwPathNotExists(); } auto valueNode = cast(ValueNode) currentNode; if (valueNode) { - throw new ConfigReadException( - "Path does not exist: " ~ createExceptionPath()); + throwPathNotExists(); } auto arrayPath = cast(ArrayPathSegment) currentPathSegment; if (arrayPath) { auto arrayNode = cast(ArrayNode) currentNode; - if (arrayNode) { + ifNotNull(arrayNode, { if (arrayNode.children.length < arrayPath.index) { throw new ConfigReadException( "Array index out of bounds: " ~ createExceptionPath()); } currentNode = arrayNode.children[arrayPath.index]; - } + }); } auto propertyPath = cast(PropertyPathSegment) currentPathSegment; if (propertyPath) { auto objectNode = cast(ObjectNode) currentNode; - if (objectNode) { + ifNotNull(objectNode, { auto propertyNode = propertyPath.propertyName in objectNode.children; - if (propertyNode) { + ifNotNullPointer(propertyNode, { currentNode = *propertyNode; - } else { - throw new ConfigReadException( - "Path does not exist: " ~ createExceptionPath()); - } - } + }); + }); } currentPathSegment = path.getNextSegment(); @@ -357,4 +373,12 @@ version (unittest) { 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")); + } + }