Allow values to be empty in Java config

This commit is contained in:
Mike Bierlee 2022-10-13 00:42:01 +03:00
parent 46fb406cad
commit 7f65ef9d9e
2 changed files with 35 additions and 11 deletions

View file

@ -13,7 +13,8 @@ module mirage.java;
import mirage.config : ConfigDictionary; import mirage.config : ConfigDictionary;
import mirage.keyvalue : KeyValueConfigFactory, SupportHashtagComments, SupportSemicolonComments, import mirage.keyvalue : KeyValueConfigFactory, SupportHashtagComments, SupportSemicolonComments,
SupportSections, NormalizeQuotedValues, SupportEqualsSeparator, SupportColonSeparator; SupportSections, NormalizeQuotedValues, SupportEqualsSeparator, SupportColonSeparator,
SupportKeysWithoutValues;
/** /**
* Creates configuration files from Java properties. * Creates configuration files from Java properties.
@ -24,7 +25,8 @@ class JavaPropertiesFactory : KeyValueConfigFactory!(
SupportSections.no, SupportSections.no,
NormalizeQuotedValues.no, NormalizeQuotedValues.no,
SupportEqualsSeparator.yes, SupportEqualsSeparator.yes,
SupportColonSeparator.yes SupportColonSeparator.yes,
SupportKeysWithoutValues.yes
) { ) {
} }
@ -61,10 +63,17 @@ version (unittest) {
# I have a comment # I have a comment
bla=one bla=one
di.bla=two di.bla=two
meh: very
much = not much
much: much
empty
"); ");
assert(config.get("bla") == "one"); assert(config.get("bla") == "one");
assert(config.get("di.bla") == "two"); assert(config.get("di.bla") == "two");
assert(config.get("meh") == "very");
assert(config.get("much") == "much");
assert(config.get("empty") == "");
} }
@("Parse java properties file") @("Parse java properties file")
@ -79,11 +88,6 @@ version (unittest) {
assertThrown!ConfigCreationException(parseJavaProperties("one=two=three")); assertThrown!ConfigCreationException(parseJavaProperties("one=two=three"));
} }
@("Fail to parse when value assignment is missing")
unittest {
assertThrown!ConfigCreationException(parseJavaProperties("answertolife"));
}
@("Substitute env vars") @("Substitute env vars")
unittest { unittest {
environment["MIRAGE_TEST_ENVY"] = "Much"; environment["MIRAGE_TEST_ENVY"] = "Much";

View file

@ -25,6 +25,7 @@ alias SupportSections = Flag!"SupportSections";
alias NormalizeQuotedValues = Flag!"NormalizeQuotedValues"; alias NormalizeQuotedValues = Flag!"NormalizeQuotedValues";
alias SupportEqualsSeparator = Flag!"SupportEqualsSeparator"; alias SupportEqualsSeparator = Flag!"SupportEqualsSeparator";
alias SupportColonSeparator = Flag!"SupportColonSeparator"; alias SupportColonSeparator = Flag!"SupportColonSeparator";
alias SupportKeysWithoutValues = Flag!"SupportKeysWithoutValues";
/** /**
* A generic reusable key/value config factory that can be configured to parse * A generic reusable key/value config factory that can be configured to parse
@ -36,7 +37,8 @@ class KeyValueConfigFactory(
SupportSections supportSections = SupportSections.no, SupportSections supportSections = SupportSections.no,
NormalizeQuotedValues normalizeQuotedValues = NormalizeQuotedValues.no, NormalizeQuotedValues normalizeQuotedValues = NormalizeQuotedValues.no,
SupportEqualsSeparator supportEqualsSeparator = SupportEqualsSeparator.no, SupportEqualsSeparator supportEqualsSeparator = SupportEqualsSeparator.no,
SupportColonSeparator supportColonSeparator = SupportColonSeparator.no SupportColonSeparator supportColonSeparator = SupportColonSeparator.no,
SupportKeysWithoutValues supportKeysWithoutValues = SupportKeysWithoutValues.no
) : ConfigFactory { ) : ConfigFactory {
/** /**
@ -98,9 +100,11 @@ class KeyValueConfigFactory(
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 ~ "): " ~ processedLine); .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 && if (normalizeQuotedValues &&
value.length > 1 && value.length > 1 &&
(value.startsWith('"') || value.startsWith('\'')) && (value.startsWith('"') || value.startsWith('\'')) &&
@ -126,7 +130,8 @@ version (unittest) {
SupportSections.no, SupportSections.no,
NormalizeQuotedValues.no, NormalizeQuotedValues.no,
SupportEqualsSeparator.yes, SupportEqualsSeparator.yes,
SupportColonSeparator.no SupportColonSeparator.no,
SupportKeysWithoutValues.no
) { ) {
} }
@ -171,6 +176,21 @@ version (unittest) {
.parseConfig("answertolife")); .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") @("Substitute env vars")
unittest { unittest {
environment["MIRAGE_TEST_ENVY"] = "Much"; environment["MIRAGE_TEST_ENVY"] = "Much";