Comments (2)
Hey there!
I assume you have some basic setup that looks like this (or the equivalent that uses records):
@Configuration
public static final class Config {
private Settings settings = new Settings();
}
@Configuration
public static final class Settings {
@Comment("How much money...")
private int reward = 100; // default value
}
and that you create and load the configuration using:
Path path = Paths.get("/tmp/config.yml");
Config config = YamlConfigurations.update(path, Config.class);
What you can do is remove some field and add a field with a different name:
@Configuration
public static final class Settings {
@Comment("How much money...")
private List<Integer> rewards = List.of(100, 70, 50); // default value
}
This is supported and the changes will be reflected in your configuration file when you call update
. However, the previously set value(s) will not be carried over. That means that the new field will have its value set to the default value you assigned to it and the old field will simply be removed.
Just changing the type of a field without changing its name is currently not supported and will result in a type mismatch when loading the config, hence throwing an exception. (From perspective of this library, this case is the same as somebody setting a configuration value to a string while your class definition defines and integer or some other type.)
The exception thrown when somebody misconfigures the configuration file is hopefully detailed enough let one figure out where the problem is. For example, the exception that is thrown when somebody changes the first value in the list to a string looks like this:
de.exlll.configlib.ConfigurationException: Deserialization of value '{rewards=[SOME STRING, 70, 50]}' with type 'class java.util.LinkedHashMap' for field 'private de.exlll.configlib.ExampleTest$Settings de.exlll.configlib.ExampleTest$Config.settings' failed.
... 77 more
Caused by: de.exlll.configlib.ConfigurationException: Deserialization of value '[SOME STRING, 70, 50]' with type 'class java.util.ArrayList' for field 'private java.util.List de.exlll.configlib.ExampleTest$Settings.rewards' failed.
The type of the object to be deserialized does not match the type the deserializer expects.
... 77 more
Caused by: java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Number (java.lang.String and java.lang.Number are in module java.base of loader 'bootstrap')
... 80 more
What happens when a user doesn't define a value for a field is discussed in the Handling of missing and null values section.
I've been thinking about adding some versioning functionality that allows both of these cases: Changing the type of a field while allowing to carry over old values. But because this feature hasn't been requested yet, I haven't spent time on implementing it.
If you have any suggestions for improvements or would like any functionality to be added, let me know!
from configlib.
Closing this issue since there are no further questions from its creator.
from configlib.
Related Issues (20)
- Add option to ignore fields from serialization HOT 1
- GitHub authentication problem HOT 7
- [Help] Unknown class deserialization HOT 16
- Add support for java 16+ HOT 5
- [BUG] Unable to Initialize "Configuration class 'ConfigYAML' does not contain any (de-)serializable fields." HOT 2
- Achieve cross-version support by making the library compatible with Java 8 HOT 5
- Shade ConfigLib using Java 8 HOT 2
- Charset string problem HOT 6
- Add better support for updating configurations HOT 2
- Allow loading configs from internal JAR resources via an InputStream HOT 5
- Native support for Adventure API Component HOT 2
- Config value POST load handler support HOT 12
- Not working jitpack.io HOT 4
- Will comments be added in lists/maps etc? HOT 2
- Publish to maven central HOT 3
- Polymorphic problem HOT 5
- Enums as Keys in Maps HOT 2
- Comments are not processed HOT 1
- Server is offline HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from configlib.