From 7f65ef9d9e6ba6737bc1db441ad367a285389628 Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Thu, 13 Oct 2022 00:42:01 +0300 Subject: [PATCH] Allow values to be empty in Java config --- source/mirage/java.d | 18 +++++++++++------- source/mirage/keyvalue.d | 28 ++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/source/mirage/java.d b/source/mirage/java.d index 4dd5b04..6f2963a 100644 --- a/source/mirage/java.d +++ b/source/mirage/java.d @@ -13,7 +13,8 @@ module mirage.java; import mirage.config : ConfigDictionary; import mirage.keyvalue : KeyValueConfigFactory, SupportHashtagComments, SupportSemicolonComments, - SupportSections, NormalizeQuotedValues, SupportEqualsSeparator, SupportColonSeparator; + SupportSections, NormalizeQuotedValues, SupportEqualsSeparator, SupportColonSeparator, + SupportKeysWithoutValues; /** * Creates configuration files from Java properties. @@ -24,7 +25,8 @@ class JavaPropertiesFactory : KeyValueConfigFactory!( SupportSections.no, NormalizeQuotedValues.no, SupportEqualsSeparator.yes, - SupportColonSeparator.yes + SupportColonSeparator.yes, + SupportKeysWithoutValues.yes ) { } @@ -61,10 +63,17 @@ version (unittest) { # I have a comment bla=one di.bla=two + meh: very + much = not much + much: much + empty "); assert(config.get("bla") == "one"); assert(config.get("di.bla") == "two"); + assert(config.get("meh") == "very"); + assert(config.get("much") == "much"); + assert(config.get("empty") == ""); } @("Parse java properties file") @@ -79,11 +88,6 @@ version (unittest) { assertThrown!ConfigCreationException(parseJavaProperties("one=two=three")); } - @("Fail to parse when value assignment is missing") - unittest { - assertThrown!ConfigCreationException(parseJavaProperties("answertolife")); - } - @("Substitute env vars") unittest { environment["MIRAGE_TEST_ENVY"] = "Much"; diff --git a/source/mirage/keyvalue.d b/source/mirage/keyvalue.d index 4a13143..fa2d7dc 100644 --- a/source/mirage/keyvalue.d +++ b/source/mirage/keyvalue.d @@ -25,6 +25,7 @@ alias SupportSections = Flag!"SupportSections"; alias NormalizeQuotedValues = Flag!"NormalizeQuotedValues"; alias SupportEqualsSeparator = Flag!"SupportEqualsSeparator"; alias SupportColonSeparator = Flag!"SupportColonSeparator"; +alias SupportKeysWithoutValues = Flag!"SupportKeysWithoutValues"; /** * A generic reusable key/value config factory that can be configured to parse @@ -36,7 +37,8 @@ class KeyValueConfigFactory( SupportSections supportSections = SupportSections.no, NormalizeQuotedValues normalizeQuotedValues = NormalizeQuotedValues.no, SupportEqualsSeparator supportEqualsSeparator = SupportEqualsSeparator.no, - SupportColonSeparator supportColonSeparator = SupportColonSeparator.no + SupportColonSeparator supportColonSeparator = SupportColonSeparator.no, + SupportKeysWithoutValues supportKeysWithoutValues = SupportKeysWithoutValues.no ) : ConfigFactory { /** @@ -98,9 +100,11 @@ class KeyValueConfigFactory( enforce!ConfigCreationException(parts.length <= 2, "Line has too many equals signs and cannot be parsed (L" ~ index .to!string ~ "): " ~ processedLine); - enforce!ConfigCreationException(parts.length == 2, "Missing value assignment (L" ~ index.to!string ~ "): " ~ processedLine); + enforce!ConfigCreationException(supportKeysWithoutValues || parts.length == 2, "Missing value assignment (L" ~ index + .to!string ~ "): " ~ processedLine); + + auto value = supportKeysWithoutValues && parts.length == 1 ? "" : parts[1].strip; - auto value = parts[1].strip; if (normalizeQuotedValues && value.length > 1 && (value.startsWith('"') || value.startsWith('\'')) && @@ -126,7 +130,8 @@ version (unittest) { SupportSections.no, NormalizeQuotedValues.no, SupportEqualsSeparator.yes, - SupportColonSeparator.no + SupportColonSeparator.no, + SupportKeysWithoutValues.no ) { } @@ -171,6 +176,21 @@ version (unittest) { .parseConfig("answertolife")); } + @("Succeed to parse when value assignment is missing and SupportKeysWithoutValues = yes") + unittest { + auto config = new KeyValueConfigFactory!( + SupportHashtagComments.no, + SupportSemicolonComments.no, + SupportSections.no, + NormalizeQuotedValues.no, + SupportEqualsSeparator.yes, + SupportColonSeparator.no, + SupportKeysWithoutValues.yes + )().parseConfig("answertolife"); + + assert(config.get("answertolife") == ""); + } + @("Substitute env vars") unittest { environment["MIRAGE_TEST_ENVY"] = "Much";