You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've been thinking about adding some form of Composite Properties. This feature would enable configuration Properties to be strung together to form things like JDBC connection strings, URIs and URLs. Even better, validation could take place at the composite level, better expressing the required validation.
Here is a hypothetical simple of connecting to a database via a JDBC connection string:
//Normal AndHow Properties
static final StrProp DB_HOST = StrProp.builder().build();
static final IntProp DB_PORT = IntProp.builder().build();
static final IntProp DB_NAME = StrProp.builder().build();
static final IntProp DB_SCHEMA = StrProp.builder().build();
//Propose composite property that would bundle several properties into one
static final CompStrProp DB_CONNECT = CompStrProp.builder().compose(
"jdbc:postgresql:" + DB_HOST + ":" + DB_PORT + "/" + DB_NAME + "?currentSchema=" + DB_SCHEMA
).mustBeNonNull().build();
CompStrProp... mustBeNonNull() in this context would mean that all the referenced properties would need to be non-null for the property to be valid, otherwise app startup would be aborted.
Handy - String formatting. Nice.
But wait - We can do way more!
The 'currentSchema' part of the connection string is optional. So is the host and port: If not included, 'localhost:5432' is assumed. That makes it really hard to express what properties are actually required and the formatting logic get complicated as well. Composite Properties neatly solves these issues if we accept multiple composites and use the first one for which all the individual properties are non-null:
//Normal AndHow Properties
...[same four DB_XXX properties from above]...
//Composite property with multiple composites - accept the first for which all the properties are non-null
static final CompStrProp DB_CONNECT = CompStrProp.builder().addComposite(
"jdbc:postgresql:" + DB_HOST + ":" + DB_PORT + "/" + DB_NAME + "?currentSchema=" + DB_SCHEMA
).addComposite(
"jdbc:postgresql:" + DB_HOST + ":" + DB_PORT + "/" + DB_NAME
).addComposite(
"jdbc:postgresql:" + DB_NAME
).mustBeNonNull().build();
Here, the connection string has different formats depending on which parameters are specified. By accepting multiple composite strings and using the first one for which all the properties are non-null, its easy to create conditional formatting. The Property requirements also become more expressive because they are combinatory requirements, not just null/non-null requirements on the individual properties.
How does it work?
The composite strings, eg: "jdbc:postgresql:" + DB_HOST +... are more complex than just assembling a string. We cannot request the value of a Property while we are constructing properties, so calling DB_HOST.toString() while in the context of a addComposite method would need to place a unique String marker into the string to identify the Property (not the value). Perhaps something like: [[[org.mycorp.DB_HOST:1234RandomGenNumber4321]]], which would indicate the canonical property name and a unique key to prevent 'rogue code' from spoofing the string to read out property values.
The addComposite method would need to add & remove a ThreadLocal variable to change the behavior of Property.toString() for this purpose.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I've been thinking about adding some form of Composite Properties. This feature would enable configuration Properties to be strung together to form things like JDBC connection strings, URIs and URLs. Even better, validation could take place at the composite level, better expressing the required validation.
Here is a hypothetical simple of connecting to a database via a JDBC connection string:
CompStrProp... mustBeNonNull()
in this context would mean that all the referenced properties would need to be non-null for the property to be valid, otherwise app startup would be aborted.Handy - String formatting. Nice.
But wait - We can do way more!
The 'currentSchema' part of the connection string is optional. So is the host and port: If not included, 'localhost:5432' is assumed. That makes it really hard to express what properties are actually required and the formatting logic get complicated as well. Composite Properties neatly solves these issues if we accept multiple composites and use the first one for which all the individual properties are non-null:
Here, the connection string has different formats depending on which parameters are specified. By accepting multiple composite strings and using the first one for which all the properties are non-null, its easy to create conditional formatting. The Property requirements also become more expressive because they are combinatory requirements, not just null/non-null requirements on the individual properties.
How does it work?
The composite strings, eg:
"jdbc:postgresql:" + DB_HOST +...
are more complex than just assembling a string. We cannot request the value of a Property while we are constructing properties, so callingDB_HOST.toString()
while in the context of aaddComposite
method would need to place a unique String marker into the string to identify the Property (not the value). Perhaps something like:[[[org.mycorp.DB_HOST:1234RandomGenNumber4321]]]
, which would indicate the canonical property name and a unique key to prevent 'rogue code' from spoofing the string to read out property values.The
addComposite
method would need to add & remove a ThreadLocal variable to change the behavior of Property.toString() for this purpose.Thoughts?
Beta Was this translation helpful? Give feedback.
All reactions