2022-10-08 22:46:34 +02:00
|
|
|
/**
|
|
|
|
* Utilities for loading Java properties files.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Mike Bierlee, m.bierlee@lostmoment.com
|
|
|
|
* Copyright: 2022 Mike Bierlee
|
|
|
|
* License:
|
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* The full terms of the license can be found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
module mirage.java;
|
|
|
|
|
2022-10-11 19:29:26 +02:00
|
|
|
import mirage.config : ConfigDictionary;
|
2022-10-12 23:31:01 +02:00
|
|
|
import mirage.keyvalue : KeyValueConfigFactory, SupportHashtagComments, SupportSemicolonComments,
|
2022-10-12 23:53:50 +02:00
|
|
|
SupportExclamationComments, SupportSections, NormalizeQuotedValues, SupportEqualsSeparator,
|
|
|
|
SupportColonSeparator, SupportKeysWithoutValues;
|
2022-10-08 22:46:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates configuration files from Java properties.
|
2022-10-13 00:05:27 +02:00
|
|
|
*
|
|
|
|
* Format specifications:
|
|
|
|
* https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Properties.html#load(java.io.Reader)
|
2022-10-08 22:46:34 +02:00
|
|
|
*/
|
2022-10-11 19:49:32 +02:00
|
|
|
class JavaPropertiesFactory : KeyValueConfigFactory!(
|
|
|
|
SupportHashtagComments.yes,
|
2022-10-11 20:07:08 +02:00
|
|
|
SupportSemicolonComments.no,
|
2022-10-12 23:53:50 +02:00
|
|
|
SupportExclamationComments.yes,
|
2022-10-11 20:32:33 +02:00
|
|
|
SupportSections.no,
|
2022-10-12 23:31:01 +02:00
|
|
|
NormalizeQuotedValues.no,
|
|
|
|
SupportEqualsSeparator.yes,
|
2022-10-12 23:42:01 +02:00
|
|
|
SupportColonSeparator.yes,
|
|
|
|
SupportKeysWithoutValues.yes
|
2022-10-11 19:49:32 +02:00
|
|
|
) {
|
2022-10-08 22:46:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse Java properties from the given Java properties string.
|
|
|
|
|
|
|
|
* Params:
|
2022-10-09 00:10:05 +02:00
|
|
|
* properties = Text contents of the config to be parsed.
|
2022-10-08 22:46:34 +02:00
|
|
|
* Returns: The parsed configuration.
|
|
|
|
*/
|
|
|
|
ConfigDictionary parseJavaProperties(const string properties) {
|
|
|
|
return new JavaPropertiesFactory().parseConfig(properties);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a Java properties file from disk.
|
|
|
|
*
|
|
|
|
* Params:
|
|
|
|
* filePath = Path to the Java properties file.
|
|
|
|
* Returns: The loaded configuration.
|
|
|
|
*/
|
|
|
|
ConfigDictionary loadJavaProperties(const string filePath) {
|
|
|
|
return new JavaPropertiesFactory().loadFile(filePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
version (unittest) {
|
|
|
|
import std.exception : assertThrown;
|
|
|
|
import std.process : environment;
|
2022-10-11 19:29:26 +02:00
|
|
|
import mirage.config : ConfigCreationException;
|
2022-10-08 22:46:34 +02:00
|
|
|
|
|
|
|
@("Parse java properties")
|
|
|
|
unittest {
|
|
|
|
auto config = parseJavaProperties("
|
|
|
|
# I have a comment
|
|
|
|
bla=one
|
|
|
|
di.bla=two
|
2022-10-13 00:08:07 +02:00
|
|
|
meh: very # except when meh=not very
|
2022-10-12 23:42:01 +02:00
|
|
|
much = not much
|
2022-10-12 23:53:50 +02:00
|
|
|
much: much !important!!!!!!!!
|
2022-10-12 23:42:01 +02:00
|
|
|
empty
|
2022-10-08 22:46:34 +02:00
|
|
|
");
|
|
|
|
|
|
|
|
assert(config.get("bla") == "one");
|
|
|
|
assert(config.get("di.bla") == "two");
|
2022-10-12 23:42:01 +02:00
|
|
|
assert(config.get("meh") == "very");
|
|
|
|
assert(config.get("much") == "much");
|
|
|
|
assert(config.get("empty") == "");
|
2022-10-08 22:46:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@("Parse java properties file")
|
|
|
|
unittest {
|
|
|
|
auto config = loadJavaProperties("testfiles/java.properties");
|
|
|
|
assert(config.get("bla") == "one");
|
|
|
|
assert(config.get("di.bla") == "two");
|
|
|
|
}
|
|
|
|
|
|
|
|
@("Substitute env vars")
|
|
|
|
unittest {
|
|
|
|
environment["MIRAGE_TEST_ENVY"] = "Much";
|
|
|
|
auto config = parseJavaProperties("envy=$MIRAGE_TEST_ENVY");
|
|
|
|
|
|
|
|
assert(config.get("envy") == "Much");
|
|
|
|
}
|
|
|
|
|
|
|
|
@("Use value from other key")
|
|
|
|
unittest {
|
|
|
|
auto config = parseJavaProperties("
|
|
|
|
one=money
|
|
|
|
two=${one}
|
|
|
|
");
|
|
|
|
|
|
|
|
assert(config.get("two") == "money");
|
|
|
|
}
|
2022-10-08 22:53:56 +02:00
|
|
|
|
|
|
|
@("Values and keys are trimmed")
|
|
|
|
unittest {
|
|
|
|
auto config = parseJavaProperties("
|
|
|
|
one = money
|
|
|
|
");
|
|
|
|
|
|
|
|
assert(config.get("one") == "money");
|
|
|
|
}
|
2022-10-08 23:17:20 +02:00
|
|
|
|
2022-10-11 20:32:33 +02:00
|
|
|
@("Quotes in values are preserved")
|
|
|
|
unittest {
|
|
|
|
auto config = parseJavaProperties("
|
|
|
|
one=\"two\"
|
|
|
|
three='four'
|
|
|
|
");
|
|
|
|
|
|
|
|
assert(config.get("one") == "\"two\"");
|
|
|
|
assert(config.get("three") == "'four'");
|
|
|
|
}
|
2022-10-08 22:46:34 +02:00
|
|
|
}
|