Trim segments in paths

This commit is contained in:
Mike Bierlee 2022-09-25 19:20:52 +03:00
parent 3e272f9e5a
commit ff6afb7e9c

View file

@ -12,7 +12,7 @@
module mirage.config; module mirage.config;
import std.exception : enforce; import std.exception : enforce;
import std.string : split, startsWith, endsWith, join, lastIndexOf; import std.string : split, startsWith, endsWith, join, lastIndexOf, strip;
import std.conv : to, ConvException; import std.conv : to, ConvException;
import std.file : readText; import std.file : readText;
@ -153,20 +153,22 @@ private class ConfigPath {
private void segmentAndNormalize(string path) { private void segmentAndNormalize(string path) {
foreach (segment; path.split(".")) { foreach (segment; path.split(".")) {
if (segment.length <= 0) { auto trimmedSegment = segment.strip;
if (trimmedSegment.length <= 0) {
continue; continue;
} }
if (segment.endsWith("]") && !segment.startsWith("[")) { if (trimmedSegment.endsWith("]") && !trimmedSegment.startsWith("[")) {
auto openBracketPos = segment.lastIndexOf("["); auto openBracketPos = trimmedSegment.lastIndexOf("[");
if (openBracketPos != -1) { if (openBracketPos != -1) {
segments ~= segment[0 .. openBracketPos]; segments ~= trimmedSegment[0 .. openBracketPos];
segments ~= segment[openBracketPos .. $]; segments ~= trimmedSegment[openBracketPos .. $];
continue; continue;
} }
} }
segments ~= segment; segments ~= trimmedSegment;
} }
} }
@ -592,4 +594,13 @@ version (unittest) {
auto config = dictionaryOne.getConfig("servers[0]"); auto config = dictionaryOne.getConfig("servers[0]");
assert(config.get("hostname") == "lala.com"); assert(config.get("hostname") == "lala.com");
} }
@("Trim spaces in path segments")
unittest {
auto dictionary = new ConfigDictionary(
new ObjectNode(["que": new ObjectNode(["pasa hombre": "not much"])])
);
assert(dictionary.get(" que. pasa hombre ") == "not much");
}
} }