Skip to content
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

Variable replacement #118

Closed
Emily-Jiang opened this issue Apr 3, 2017 · 5 comments · Fixed by #577
Closed

Variable replacement #118

Emily-Jiang opened this issue Apr 3, 2017 · 5 comments · Fixed by #577
Assignees
Labels
use case 💡 An issue which illustrates a desired use case for the specification
Milestone

Comments

@Emily-Jiang
Copy link
Member

The issue is from Jeremie:
my.property.version.major=1

my.property.version.minor=0
my.property.version=${my.property.version.major}.{my.property.version.minor}
my.combined.property=Version ${my.property.version}

my.combined.property.withdefault=Version ${my.undefined.property:1.0}

This is very effective for example the file structure of a service. The root directory configuration is the domain of the installation whereas the subdirectory is the domain of application.

This has an impact on the implementation because all properties must be known before solving them. In addition, all properties dependent on a mutable property become mutable as well.

@struberg
Copy link
Contributor

struberg commented Apr 3, 2017

This already has been proposed in July last year
https://github.com/struberg/javaConfig/blob/configValue/api/src/main/java/javx/config/ConfigValue.java#L55

Note that we cannot ALWAYS evaluate those variables because it's certainly valid that they might be part of the payload a user wants to resolve natively. So there must be a switch to enable or disable this.

@jerr
Copy link

jerr commented Apr 4, 2017

Mark, you're absolutely right, in most cases that I've met, I also had properties with variables that were not to be computed.

But we can have two approaches for this:

  • The first one, we can manage this on the API user side. As the code you specify, it is the user who specifies the management or how to calculate the property.

  • The second, the management is transparent to the API user. You can restructure your properties hierarchy without changing a single line of code. This approach is perhaps also to be considered. It makes the properties management more flexible concerning the code.

Some examples that may be appropriate for the second approach:

my.propery.rootdirectory = /opt/myservice/data
my.propery.productdirectory = ${my.propery.rootdirectory}/${my.property.product.category:products}
my.property.product.templatefilename = ${product.id}-${product.name}.pdf
my.property.product.file = ${my.propery.productdirectory}/${my.property.product.templatefilename}

The result for the property my.property.product.file is "/opt/myservice/data/products/${product.id}-${product.name}.pdf"
With the default value of my.propery.productdirectory and undefined properties of my.property.product.file we can manage that.

Sometimes you want to clean up your configuration files without affect the code. This situation has happened to me in several projects.

What do you think about this?

@sgalles
Copy link

sgalles commented Jun 16, 2018

Hello,
Would this also work for replacement in the default values ?
In this case, I think this would automatically also allow this : #339
because you could simply define your alias as a second property and just set the default value equal to the initial property

@ConfigProperty(name = "my.foo")
...
@ConfigProperty(name = "my.fooalias", defaultValue = "${my.foo}")

@Emily-Jiang
Copy link
Member Author

@sgalles good suggestion.
One thing I would like to add is that we should just do one-level variable replacement not doing nested replacement in the first release.
${foo.${bar}} => will search for foo.${bar} even though bar is a valid variable.

@Emily-Jiang
Copy link
Member Author

Use case:
As a developer, I would like my config value to refer to other config names such as ${foo.${bar}}

@dmlloyd dmlloyd added use case 💡 An issue which illustrates a desired use case for the specification and removed ConfigValue labels Feb 27, 2020
@radcortez radcortez linked a pull request Sep 21, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
use case 💡 An issue which illustrates a desired use case for the specification
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants