-
-
Notifications
You must be signed in to change notification settings - Fork 213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Any chance of trimming returned values? #120
Comments
I'll look into it asap. |
Hi. The properties file format does not support inline comments:
In fact the "# secs" is taken as part of the property value, and I get this error:
Also my IDE (IntelliJ IDEA 14) shows the trailing "#" in green as value of the property: So I suggest to specify the information in a different way: # polling period must be specified in seconds
pollingPeriod = 300
pollingPeriodInSeconds = 300
polling.period.seconds = 300 If I implement the inline comment I break the compatibility with properties and I also need to provide an escaping mechanism for the "#" character, that is not needed in standard java properties. That said, I'll look into the possibility to trim values for primitives and numeric wrapper classes. |
Hi. I committed on master branch a new 'Preprocessor' feature. Here is an example on how to use it: public class PreprocessorTest {
@PreprocessorClasses({ SkipInlineComments.class, Trim.class })
public interface ConfigWithPreprocessors extends Config {
@DefaultValue(" 300 ")
Integer pollingPeriod();
@PreprocessorClasses(ToLowerCase.class)
@DefaultValue(" HelloWorld ")
String helloWorld();
@DefaultValue(" This is the value ! this is an inline comment # this is the inline comment too")
String skipsInlineComments();
@PreprocessorClasses(ToLowerCase.class)
String nullValue();
}
@Test
public void shouldReturnTrimmedValue() {
ConfigWithPreprocessors cfg = ConfigFactory.create(ConfigWithPreprocessors.class);
assertEquals(new Integer(300), cfg.pollingPeriod());
}
@Test
public void shouldReturnLowercasedTrimmedHelloWorld() {
ConfigWithPreprocessors cfg = ConfigFactory.create(ConfigWithPreprocessors.class);
assertEquals("helloworld", cfg.helloWorld());
}
@Test
public void shouldSkipInlineComments() {
ConfigWithPreprocessors cfg = ConfigFactory.create(ConfigWithPreprocessors.class);
assertEquals("This is the value", cfg.skipsInlineComments());
}
@Test
public void shouldNotThrowExceptions() {
ConfigWithPreprocessors cfg = ConfigFactory.create(ConfigWithPreprocessors.class);
assertNull(cfg.nullValue());
}
// preprocessors implementation
public static class Trim implements Preprocessor {
public String process(String input) {
return input.trim();
}
}
public static class ToLowerCase implements Preprocessor {
public String process(String input) {
return input.toLowerCase();
}
}
public static class SkipInlineComments implements Preprocessor {
public String process(String input) {
int hashTagIndex = input.indexOf('#');
int exclamationMarkIndex = input.indexOf("!");
if (hashTagIndex == -1 && exclamationMarkIndex == -1)
return input; // comments not present.
int commentIndex = min(hashTagIndex, exclamationMarkIndex);
if (commentIndex == -1)
commentIndex = max(hashTagIndex, exclamationMarkIndex);
return input.substring(0, commentIndex);
}
}
} full source code of this example is available here. Preprocessors are applied all together: first the ones specified on the method level, then the ones specified on the class level. Order specified in the |
The interface of Preprocessor may change to address also #118, I could include parameter values passed to the method, and eventually additional context objects (i.e. the internal properties representation for other properties.) |
Thanks Iviggiano! More importantly, thank you for a job well done on the Owner library. |
Hi! |
Thanks for your feedback. |
Today I release OWNER 1.0.9. Sorry for the long delay. |
First thank you for a great library - succinct and just right for the job.
We've been using the library a while and we LOVE it! However, QA was getting a funky error over the weekend and it turns out that one of the integer properties was throwing a conversion error.
The line in the properties file is
The intent is for a polling period of 5 minutes. We caught the first exception because of the #secs comment but it was hard to see why '300 ' was throwing an error.
By the way, it would be nice if in line comment is supported but not a must have.
Thanks again for a job well done.
The text was updated successfully, but these errors were encountered: