From af6dd95d57fdcf1bfe859de88ffe9c93fbe29d5b Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Sat, 24 Sep 2022 03:46:19 +0300 Subject: [PATCH] Support conventional array indexing notation i.e. bla.bleh[4] --- source/poodinis/config/dictionary.d | 30 ++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/source/poodinis/config/dictionary.d b/source/poodinis/config/dictionary.d index 826d775..6881edf 100644 --- a/source/poodinis/config/dictionary.d +++ b/source/poodinis/config/dictionary.d @@ -10,7 +10,7 @@ module poodinis.config.dictionary; import std.exception : enforce; -import std.string : split, startsWith, endsWith, join; +import std.string : split, startsWith, endsWith, join, lastIndexOf; import std.conv : to, ConvException; class ConfigReadException : Exception { @@ -113,11 +113,25 @@ class ConfigPath { this(const string path) { this.path = path; + segmentAndNormalize(path); + } + private void segmentAndNormalize(string path) { foreach (segment; path.split(".")) { - if (segment.length > 0) { - segments ~= segment; + if (segment.length <= 0) { + continue; } + + if (segment.endsWith("]") && !segment.startsWith("[")) { + auto openBracketPos = segment.lastIndexOf("["); + if (openBracketPos != -1) { + segments ~= segment[0 .. openBracketPos]; + segments ~= segment[openBracketPos .. $]; + continue; + } + } + + segments ~= segment; } } @@ -423,4 +437,14 @@ version (unittest) { assert(dictionary.get(".one..two...three....") == "four"); } + @("Support conventional array indexing notation") + unittest { + auto dictionary = new ConfigDictionary(); + dictionary.rootNode = new ObjectNode( + [ + "one": new ObjectNode(["two": new ArrayNode(["dino", "mino"])]) + ]); + + assert(dictionary.get("one.two[1]") == "mino"); + } }