From b82b50a16fecda4f63fe4e5e0c76d1437996ac32 Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Thu, 13 Oct 2022 00:53:50 +0300 Subject: [PATCH] Support exclamation marks as comments in Java properties --- source/mirage/java.d | 7 ++++--- source/mirage/keyvalue.d | 31 +++++++++++++++++++++---------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/source/mirage/java.d b/source/mirage/java.d index 6f2963a..c1d7127 100644 --- a/source/mirage/java.d +++ b/source/mirage/java.d @@ -13,8 +13,8 @@ module mirage.java; import mirage.config : ConfigDictionary; import mirage.keyvalue : KeyValueConfigFactory, SupportHashtagComments, SupportSemicolonComments, - SupportSections, NormalizeQuotedValues, SupportEqualsSeparator, SupportColonSeparator, - SupportKeysWithoutValues; + SupportExclamationComments, SupportSections, NormalizeQuotedValues, SupportEqualsSeparator, + SupportColonSeparator, SupportKeysWithoutValues; /** * Creates configuration files from Java properties. @@ -22,6 +22,7 @@ import mirage.keyvalue : KeyValueConfigFactory, SupportHashtagComments, SupportS class JavaPropertiesFactory : KeyValueConfigFactory!( SupportHashtagComments.yes, SupportSemicolonComments.no, + SupportExclamationComments.yes, SupportSections.no, NormalizeQuotedValues.no, SupportEqualsSeparator.yes, @@ -65,7 +66,7 @@ version (unittest) { di.bla=two meh: very much = not much - much: much + much: much !important!!!!!!!! empty "); diff --git a/source/mirage/keyvalue.d b/source/mirage/keyvalue.d index fa2d7dc..e1fa147 100644 --- a/source/mirage/keyvalue.d +++ b/source/mirage/keyvalue.d @@ -21,6 +21,7 @@ import std.typecons : Flag; alias SupportHashtagComments = Flag!"SupportHashtagComments"; alias SupportSemicolonComments = Flag!"SupportSemicolonComments"; +alias SupportExclamationComments = Flag!"SupportExclamationComments"; alias SupportSections = Flag!"SupportSections"; alias NormalizeQuotedValues = Flag!"NormalizeQuotedValues"; alias SupportEqualsSeparator = Flag!"SupportEqualsSeparator"; @@ -34,6 +35,7 @@ alias SupportKeysWithoutValues = Flag!"SupportKeysWithoutValues"; class KeyValueConfigFactory( SupportHashtagComments supportHashtagComments = SupportHashtagComments.no, SupportSemicolonComments supportSemicolonComments = SupportSemicolonComments.no, + SupportExclamationComments supportExclamationComments = SupportExclamationComments.no, SupportSections supportSections = SupportSections.no, NormalizeQuotedValues normalizeQuotedValues = NormalizeQuotedValues.no, SupportEqualsSeparator supportEqualsSeparator = SupportEqualsSeparator.no, @@ -58,19 +60,18 @@ class KeyValueConfigFactory( foreach (size_t index, string line; lines) { auto processedLine = line; - if (supportHashtagComments) { - auto commentPosition = processedLine.indexOf('#'); - if (commentPosition >= 0) { - processedLine = processedLine[0 .. commentPosition]; + void replaceComments(bool isTypeSupported, char commentToken) { + if (isTypeSupported) { + auto commentPosition = processedLine.indexOf(commentToken); + if (commentPosition >= 0) { + processedLine = processedLine[0 .. commentPosition]; + } } } - if (supportSemicolonComments) { - auto commentPosition = processedLine.indexOf(';'); - if (commentPosition >= 0) { - processedLine = processedLine[0 .. commentPosition]; - } - } + replaceComments(supportHashtagComments, '#'); + replaceComments(supportSemicolonComments, ';'); + replaceComments(supportExclamationComments, '!'); processedLine = processedLine.strip; @@ -127,6 +128,7 @@ version (unittest) { class TestKeyValueConfigFactory : KeyValueConfigFactory!( SupportHashtagComments.no, SupportSemicolonComments.no, + SupportExclamationComments.no, SupportSections.no, NormalizeQuotedValues.no, SupportEqualsSeparator.yes, @@ -151,6 +153,7 @@ version (unittest) { auto config = new KeyValueConfigFactory!( SupportHashtagComments.yes, SupportSemicolonComments.yes, + SupportExclamationComments.yes, SupportSections.no, NormalizeQuotedValues.no, SupportEqualsSeparator.yes, @@ -158,6 +161,7 @@ version (unittest) { )().parseConfig(" # this is a comment ; this is another comment + ! this then is also a comment! iamset=true "); @@ -181,6 +185,7 @@ version (unittest) { auto config = new KeyValueConfigFactory!( SupportHashtagComments.no, SupportSemicolonComments.no, + SupportExclamationComments.no, SupportSections.no, NormalizeQuotedValues.no, SupportEqualsSeparator.yes, @@ -223,6 +228,7 @@ version (unittest) { auto config = new KeyValueConfigFactory!( SupportHashtagComments.yes, SupportSemicolonComments.yes, + SupportExclamationComments.yes, SupportSections.no, NormalizeQuotedValues.no, SupportEqualsSeparator.yes, @@ -230,10 +236,12 @@ version (unittest) { )().parseConfig(" server=localhost #todo: change me. default=localhost when not set. port=9876; I think this port = right? + timeout=36000 ! pretty long! "); assert(config.get("server") == "localhost"); assert(config.get("port") == "9876"); + assert(config.get("timeout") == "36000"); } @("Support sections when enabled") @@ -241,6 +249,7 @@ version (unittest) { auto config = new KeyValueConfigFactory!( SupportHashtagComments.no, SupportSemicolonComments.yes, + SupportExclamationComments.no, SupportSections.yes, NormalizeQuotedValues.no, SupportEqualsSeparator.yes, @@ -275,6 +284,7 @@ version (unittest) { auto config = new KeyValueConfigFactory!( SupportHashtagComments.yes, SupportSemicolonComments.no, + SupportExclamationComments.no, SupportSections.no, NormalizeQuotedValues.yes, SupportEqualsSeparator.yes, @@ -299,6 +309,7 @@ version (unittest) { auto config = new KeyValueConfigFactory!( SupportHashtagComments.no, SupportSemicolonComments.no, + SupportExclamationComments.no, SupportSections.no, NormalizeQuotedValues.no, SupportEqualsSeparator.yes,