TheElectronWill / night-config

Powerful java configuration library for toml, yaml, hocon, json and in-memory configurations. Serialization/deserialization framework.
GNU Lesser General Public License v3.0
242 stars 28 forks source link

[question] How to use configspec to correct (Commented)FileConfig? #135

Closed PORTB closed 1 year ago

PORTB commented 1 year ago

Hello I'm trying to use ConfigSpec to correct any invalid values in a FileConfig for a project I'm making. Here is a simplified version of what I'm doing:

var spec = new ConfigSpec();

spec.define("stuff", false);

var config = CommentedFileConfig.of("somefile.toml");

config.load(); //com.electronwill.nightconfig.core.io.ParsingException: Invalid value: TRUE

spec.correct(config);

System.out.println((boolean)config.get("stuff")); //should be false

somefile.toml

stuff=TRUE #invalid

However, it causes an exception when I try to load the file, so the configspec can't correct the file.

How do I achieve this?

TheElectronWill commented 1 year ago

Hello!

The error isn't in your use of ConfigSpec, it's your toml file that is incorrect, which causes the ParsingException at load(). A config cannot be loaded if it cannot be parsed.

Instead of TRUE it should be true in lowercase.

edit: to be clear, the goal of ConfigSpec is not to fix unparseable configurations, but to correct the values of a configuration.

PORTB commented 1 year ago

Oh, I misunderstood the purpose of the configspec. Thanks for your response