Support exclamation marks as comments in Java properties

This commit is contained in:
Mike Bierlee 2022-10-13 00:53:50 +03:00
parent 7f65ef9d9e
commit b82b50a16f
2 changed files with 25 additions and 13 deletions

View file

@ -13,8 +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, SupportExclamationComments, SupportSections, NormalizeQuotedValues, SupportEqualsSeparator,
SupportKeysWithoutValues; SupportColonSeparator, SupportKeysWithoutValues;
/** /**
* Creates configuration files from Java properties. * Creates configuration files from Java properties.
@ -22,6 +22,7 @@ import mirage.keyvalue : KeyValueConfigFactory, SupportHashtagComments, SupportS
class JavaPropertiesFactory : KeyValueConfigFactory!( class JavaPropertiesFactory : KeyValueConfigFactory!(
SupportHashtagComments.yes, SupportHashtagComments.yes,
SupportSemicolonComments.no, SupportSemicolonComments.no,
SupportExclamationComments.yes,
SupportSections.no, SupportSections.no,
NormalizeQuotedValues.no, NormalizeQuotedValues.no,
SupportEqualsSeparator.yes, SupportEqualsSeparator.yes,
@ -65,7 +66,7 @@ version (unittest) {
di.bla=two di.bla=two
meh: very meh: very
much = not much much = not much
much: much much: much !important!!!!!!!!
empty empty
"); ");

View file

@ -21,6 +21,7 @@ import std.typecons : Flag;
alias SupportHashtagComments = Flag!"SupportHashtagComments"; alias SupportHashtagComments = Flag!"SupportHashtagComments";
alias SupportSemicolonComments = Flag!"SupportSemicolonComments"; alias SupportSemicolonComments = Flag!"SupportSemicolonComments";
alias SupportExclamationComments = Flag!"SupportExclamationComments";
alias SupportSections = Flag!"SupportSections"; alias SupportSections = Flag!"SupportSections";
alias NormalizeQuotedValues = Flag!"NormalizeQuotedValues"; alias NormalizeQuotedValues = Flag!"NormalizeQuotedValues";
alias SupportEqualsSeparator = Flag!"SupportEqualsSeparator"; alias SupportEqualsSeparator = Flag!"SupportEqualsSeparator";
@ -34,6 +35,7 @@ alias SupportKeysWithoutValues = Flag!"SupportKeysWithoutValues";
class KeyValueConfigFactory( class KeyValueConfigFactory(
SupportHashtagComments supportHashtagComments = SupportHashtagComments.no, SupportHashtagComments supportHashtagComments = SupportHashtagComments.no,
SupportSemicolonComments supportSemicolonComments = SupportSemicolonComments.no, SupportSemicolonComments supportSemicolonComments = SupportSemicolonComments.no,
SupportExclamationComments supportExclamationComments = SupportExclamationComments.no,
SupportSections supportSections = SupportSections.no, SupportSections supportSections = SupportSections.no,
NormalizeQuotedValues normalizeQuotedValues = NormalizeQuotedValues.no, NormalizeQuotedValues normalizeQuotedValues = NormalizeQuotedValues.no,
SupportEqualsSeparator supportEqualsSeparator = SupportEqualsSeparator.no, SupportEqualsSeparator supportEqualsSeparator = SupportEqualsSeparator.no,
@ -58,19 +60,18 @@ class KeyValueConfigFactory(
foreach (size_t index, string line; lines) { foreach (size_t index, string line; lines) {
auto processedLine = line; auto processedLine = line;
if (supportHashtagComments) { void replaceComments(bool isTypeSupported, char commentToken) {
auto commentPosition = processedLine.indexOf('#'); if (isTypeSupported) {
if (commentPosition >= 0) { auto commentPosition = processedLine.indexOf(commentToken);
processedLine = processedLine[0 .. commentPosition]; if (commentPosition >= 0) {
processedLine = processedLine[0 .. commentPosition];
}
} }
} }
if (supportSemicolonComments) { replaceComments(supportHashtagComments, '#');
auto commentPosition = processedLine.indexOf(';'); replaceComments(supportSemicolonComments, ';');
if (commentPosition >= 0) { replaceComments(supportExclamationComments, '!');
processedLine = processedLine[0 .. commentPosition];
}
}
processedLine = processedLine.strip; processedLine = processedLine.strip;
@ -127,6 +128,7 @@ version (unittest) {
class TestKeyValueConfigFactory : KeyValueConfigFactory!( class TestKeyValueConfigFactory : KeyValueConfigFactory!(
SupportHashtagComments.no, SupportHashtagComments.no,
SupportSemicolonComments.no, SupportSemicolonComments.no,
SupportExclamationComments.no,
SupportSections.no, SupportSections.no,
NormalizeQuotedValues.no, NormalizeQuotedValues.no,
SupportEqualsSeparator.yes, SupportEqualsSeparator.yes,
@ -151,6 +153,7 @@ version (unittest) {
auto config = new KeyValueConfigFactory!( auto config = new KeyValueConfigFactory!(
SupportHashtagComments.yes, SupportHashtagComments.yes,
SupportSemicolonComments.yes, SupportSemicolonComments.yes,
SupportExclamationComments.yes,
SupportSections.no, SupportSections.no,
NormalizeQuotedValues.no, NormalizeQuotedValues.no,
SupportEqualsSeparator.yes, SupportEqualsSeparator.yes,
@ -158,6 +161,7 @@ version (unittest) {
)().parseConfig(" )().parseConfig("
# this is a comment # this is a comment
; this is another comment ; this is another comment
! this then is also a comment!
iamset=true iamset=true
"); ");
@ -181,6 +185,7 @@ version (unittest) {
auto config = new KeyValueConfigFactory!( auto config = new KeyValueConfigFactory!(
SupportHashtagComments.no, SupportHashtagComments.no,
SupportSemicolonComments.no, SupportSemicolonComments.no,
SupportExclamationComments.no,
SupportSections.no, SupportSections.no,
NormalizeQuotedValues.no, NormalizeQuotedValues.no,
SupportEqualsSeparator.yes, SupportEqualsSeparator.yes,
@ -223,6 +228,7 @@ version (unittest) {
auto config = new KeyValueConfigFactory!( auto config = new KeyValueConfigFactory!(
SupportHashtagComments.yes, SupportHashtagComments.yes,
SupportSemicolonComments.yes, SupportSemicolonComments.yes,
SupportExclamationComments.yes,
SupportSections.no, SupportSections.no,
NormalizeQuotedValues.no, NormalizeQuotedValues.no,
SupportEqualsSeparator.yes, SupportEqualsSeparator.yes,
@ -230,10 +236,12 @@ version (unittest) {
)().parseConfig(" )().parseConfig("
server=localhost #todo: change me. default=localhost when not set. server=localhost #todo: change me. default=localhost when not set.
port=9876; I think this port = right? port=9876; I think this port = right?
timeout=36000 ! pretty long!
"); ");
assert(config.get("server") == "localhost"); assert(config.get("server") == "localhost");
assert(config.get("port") == "9876"); assert(config.get("port") == "9876");
assert(config.get("timeout") == "36000");
} }
@("Support sections when enabled") @("Support sections when enabled")
@ -241,6 +249,7 @@ version (unittest) {
auto config = new KeyValueConfigFactory!( auto config = new KeyValueConfigFactory!(
SupportHashtagComments.no, SupportHashtagComments.no,
SupportSemicolonComments.yes, SupportSemicolonComments.yes,
SupportExclamationComments.no,
SupportSections.yes, SupportSections.yes,
NormalizeQuotedValues.no, NormalizeQuotedValues.no,
SupportEqualsSeparator.yes, SupportEqualsSeparator.yes,
@ -275,6 +284,7 @@ version (unittest) {
auto config = new KeyValueConfigFactory!( auto config = new KeyValueConfigFactory!(
SupportHashtagComments.yes, SupportHashtagComments.yes,
SupportSemicolonComments.no, SupportSemicolonComments.no,
SupportExclamationComments.no,
SupportSections.no, SupportSections.no,
NormalizeQuotedValues.yes, NormalizeQuotedValues.yes,
SupportEqualsSeparator.yes, SupportEqualsSeparator.yes,
@ -299,6 +309,7 @@ version (unittest) {
auto config = new KeyValueConfigFactory!( auto config = new KeyValueConfigFactory!(
SupportHashtagComments.no, SupportHashtagComments.no,
SupportSemicolonComments.no, SupportSemicolonComments.no,
SupportExclamationComments.no,
SupportSections.no, SupportSections.no,
NormalizeQuotedValues.no, NormalizeQuotedValues.no,
SupportEqualsSeparator.yes, SupportEqualsSeparator.yes,