mirror of
https://github.com/mbierlee/mirage-config.git
synced 2024-11-15 04:44:01 +01:00
Ignore comments in java properties
This commit is contained in:
parent
4ee94f8a9c
commit
16b5032e9b
|
@ -13,7 +13,7 @@ module mirage.java;
|
||||||
|
|
||||||
import mirage.config : ConfigFactory, ConfigDictionary, ConfigNode, ValueNode, ObjectNode, ConfigCreationException;
|
import mirage.config : ConfigFactory, ConfigDictionary, ConfigNode, ValueNode, ObjectNode, ConfigCreationException;
|
||||||
|
|
||||||
import std.string : lineSplitter, strip, startsWith, split;
|
import std.string : lineSplitter, strip, startsWith, split, indexOf;
|
||||||
import std.array : array;
|
import std.array : array;
|
||||||
import std.exception : enforce;
|
import std.exception : enforce;
|
||||||
import std.conv : to;
|
import std.conv : to;
|
||||||
|
@ -34,15 +34,20 @@ class JavaPropertiesFactory : ConfigFactory {
|
||||||
auto lines = contents.lineSplitter().array;
|
auto lines = contents.lineSplitter().array;
|
||||||
auto properties = new ConfigDictionary();
|
auto properties = new ConfigDictionary();
|
||||||
foreach (size_t index, string line; lines) {
|
foreach (size_t index, string line; lines) {
|
||||||
auto trimmedLine = line.strip;
|
auto normalizedLine = line.strip;
|
||||||
if (trimmedLine.length == 0 || trimmedLine.startsWith('#')) {
|
if (normalizedLine.length == 0 || normalizedLine.startsWith('#')) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto parts = trimmedLine.split('=');
|
auto commentPosition = normalizedLine.indexOf('#');
|
||||||
|
if (commentPosition >= 0) {
|
||||||
|
normalizedLine = normalizedLine[0 .. commentPosition];
|
||||||
|
}
|
||||||
|
|
||||||
|
auto parts = normalizedLine.split('=');
|
||||||
enforce!ConfigCreationException(parts.length <= 2, "Line has too many equals signs and cannot be parsed (L" ~ index
|
enforce!ConfigCreationException(parts.length <= 2, "Line has too many equals signs and cannot be parsed (L" ~ index
|
||||||
.to!string ~ "): " ~ trimmedLine);
|
.to!string ~ "): " ~ normalizedLine);
|
||||||
enforce!ConfigCreationException(parts.length == 2, "Missing value assignment (L" ~ index.to!string ~ "): " ~ trimmedLine);
|
enforce!ConfigCreationException(parts.length == 2, "Missing value assignment (L" ~ index.to!string ~ "): " ~ normalizedLine);
|
||||||
properties.set(parts[0].strip, parts[1].strip);
|
properties.set(parts[0].strip, parts[1].strip);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,4 +136,13 @@ version (unittest) {
|
||||||
|
|
||||||
assert(config.get("one") == "money");
|
assert(config.get("one") == "money");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@("Remove end-of-line comments")
|
||||||
|
unittest {
|
||||||
|
auto config = parseJavaProperties("
|
||||||
|
server=localhost #todo: change me. default=localhost when not set.
|
||||||
|
");
|
||||||
|
|
||||||
|
assert(config.get("server") == "localhost");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue