matteobaccan / owner

Get rid of the boilerplate code in properties based configuration.

Home Page:https://matteobaccan.github.io/owner/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Distinct between actual source of a merged property

strangelookingnerd opened this issue · comments

I have a Config that is stored in a file and can be overwritten by ENV

@HotReload
@LoadPolicy(LoadType.MERGE)
@Sources({  
	"system:env",
	"system:properties",
	"file:config/config.properties"
})
public interface TestConfig extends Mutable, Accessible  {
    @DefaultValue("42")
    int testProperty();
}

If I want to store the configuration like this

public static void main(String[] args) throws Exception {
	TestConfig cfg = ConfigFactory.create(TestConfig.class);
	cfg.store(new FileOutputStream(new File("config", "config.properties")), null);
}

I end up with a config.properties with all system properties, which not desired

#Sat Feb 06 17:27:12 CET 2021
java.specification.version=15
sun.cpu.isalist=amd64
sun.jnu.encoding=Cp1252
sun.arch.data.model=64
...
java.version=15.0.1
java.vm.specification.name=Java Virtual Machine Specification
...
java.vm.vendor=Oracle Corporation
testProperty=42
...

To work around that I tried to remove the system properties before storing to only end up with my testProperty

public static void main(String[] args) throws Exception {
	TestConfig cfg = ConfigFactory.create(TestConfig.class);
	for (String key : System.getenv().keySet()) {
		cfg.removeProperty(key);
	}
	for (String key : System.getProperties().stringPropertyNames()) {
		cfg.removeProperty(key);
	}
	cfg.store(new FileOutputStream(new File("config", "config.properties")), null);
}

This workaround fails if the property is defined in both, ENV and the config.properties.
What I would like is to only store properties that are not defined in ENV.

Is there any easy way to do this? Would it be possible to find out from which source a property (value) originates?
Or is my approach itself wrong?