Comments (13)
Yes. You can even do this without using a value serializer:
public interface Config {
static Map<Integer, Level> defaultLevels() {
Map<Integer, Level> levelsMap = new HashMap<>();
levelsMap.put(1, Level.of(5, 10, 1, 10, 0));
levelsMap.put(2, Level.of(7, 12, 2, 11, 10));
levelsMap.put(3, Level.of(9, 14, 3, 12, 20));
levelsMap.put(4, Level.of(12, 16, 4, 13, 30));
levelsMap.put(5, Level.of(15, 18, 5, 14, 40));
return levelsMap;
}
@ConfDefault.DefaultObject("defaultLevels")
Map<Integer, @SubSection Level> levels();
}
public interface Level {
int minSpawnDelay();
int maxSpawnDelay();
int spawnCount();
int spawnRange();
int price();
static Level of(int minSpawnDelay, int maxSpawnDelay, int spawnCount, int spawnRange, int price) {
return new Level() {
@Override
public int minSpawnDelay() { return minSpawnDelay; }
@Override
public int maxSpawnDelay() { return maxSpawnDelay; }
@Override
public int spawnCount() { return spawnCount; }
@Override
public int spawnRange() { return spawnRange; }
@Override
public int price() { return price; }
};
}
}
from dazzleconf.
Note that you need @SubSection
here:
@ConfDefault.DefaultObject("defaultLevels")
Map<Integer, @SubSection Level> levels();
from dazzleconf.
Thanks. This is a bug with the snakeyaml extension. The core should support non-string keys.
Also, I tested that this bug is specific to CommentMode.alternateWriter
as well as CommentMode.fullComments
. For now, to avoid it, you can use CommentMode.headerOnly
, which lacks comment support except for the header.
If you need comments and can't wait until the next release, let me know and I will publish a SNAPSHOT release with the bug fix.
from dazzleconf.
You use ValueSerialiser so you can use a custom type in your configuration. Let's say you had the following:
public class CustomType {
private final String value;
public CustomType(String value) {
this.value = value;
}
public String stringValue() {
return value;
}
}
- First implement the ValueSerialiser:
public class MyCustomSerializer implements ValueSerialiser<CustomType> {
@Override
public Class<CustomType> getTargetClass() {
return CustomType.class;
}
@Override
public CustomType deserialise(FlexibleType flexibleType) throws BadValueException {
// Convert from a String -> CustomType
String value = flexibleType.getString();
return new CustomType(value);
}
@Override
public Object serialise(CustomType value, Decomposer decomposer) {
// Convert from a CustomType -> String
return value.stringValue();
}
}
- Then add the serializer to the ConfigurationOptions:
ConfigurationOptions options = new ConfigurationOptions.Builder()
.addSerialiser(new MyCustomSerializer())
.build();
Use the ConfigurationOptions when you create your config factory (GsonConfigurationFactory, SnakeYamlConfigurationFactory, etc.)
from dazzleconf.
How i can use this for object with more variables?
from dazzleconf.
I'm not exactly sure what you mean by that. If you need to use this type in your configuration, you can very easily:
public interface Config {
@DefaultString("default value")
CustomType something();
// You can even use it in a List, Set, or Collection
@DefaultStrings({"value1", "value2"})
List<CustomType> listOfThings();
}
from dazzleconf.
I need put this to config:
static Map<Integer, Level> defaultLevels() {
Map<Integer, Level> levelsMap = new HashMap<>();
levelsMap.put(1, new Level(5, 10, 1, 10, 0));
levelsMap.put(2, new Level(7, 12, 2, 11, 10));
levelsMap.put(3, new Level(9, 14, 3, 12, 20));
levelsMap.put(4, new Level(12, 16, 4, 13, 30));
levelsMap.put(5, new Level(15, 18, 5, 14, 40));
return levelsMap;
}
@ConfDefault.DefaultObject("defaultLevels")
Map<Integer, Level> levels();
from dazzleconf.
I see. For that, you can treat a Level
as a list of integers and implement your serializer like this:
public class LevelSerializer implements ValueSerialiser<Level> {
@Override
public Class<Level> getTargetClass() {
return Level.class;
}
@Override
public Level deserialise(FlexibleType flexibleType) throws BadValueException {
List<Integer> values = new ArrayList<>();
for (FlexibleType element : flexibleType.getList()) {
values.add(element.getInteger());
}
return new Level(values);
}
@Override
public Object serialise(Level value, Decomposer decomposer) {
return value.values();
}
}
public class Level {
private final List<Integer> values;
public Level(List<Integer> values) {
this.values = List.copyOf(values);
if (this.values.size() != 5) {
throw new IllegalArgumentException("Size of Level values must be 5");
}
}
public List<Integer> values() {
return values;
}
}
There are other ways you could do this, like by treating a Level as a configuration section, but I choose this way because it's relatively simple.
from dazzleconf.
But you can show my how i can do ths like by treating a level as configuration section? Your solution is not convenient to configuration.
Level:
public class Level {
int minSpawnDelay;
int maxSpawnDelay;
int spawnCount;
int spawnRage;
int price;
}
from dazzleconf.
Thank you wery much <3
from dazzleconf.
You're welcome. A thanks to you too - I've added this information to the wiki now:
https://github.com/A248/DazzleConf/wiki/Examples-of-custom-types-in-your-configuration
from dazzleconf.
Map key need be String :(
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at space.arim.dazzleconf.ext.snakeyaml.CommentedWriter.writeRemainingMapEntries(CommentedWriter.java:99) ~[?:?]
at space.arim.dazzleconf.ext.snakeyaml.CommentedWriter.writeMap(CommentedWriter.java:93) ~[?:?]
at space.arim.dazzleconf.ext.snakeyaml.CommentedWriter.lambda$writeValue$0(CommentedWriter.java:188) ~[?:?]
at space.arim.dazzleconf.ext.snakeyaml.CommentedWriter.descendAndDo(CommentedWriter.java:157) ~[?:?]
at space.arim.dazzleconf.ext.snakeyaml.CommentedWriter.writeValue(CommentedWriter.java:188) ~[?:?]
at space.arim.dazzleconf.ext.snakeyaml.CommentedWriter.writeRemainingMapEntries(CommentedWriter.java:112) ~[?:?] at space.arim.dazzleconf.ext.snakeyaml.CommentedWriter.writeMap(CommentedWriter.java:93) ~[?:?]
at space.arim.dazzleconf.ext.snakeyaml.CommentedWriter.lambda$writeValue$0(CommentedWriter.java:188) ~[?:?]
at space.arim.dazzleconf.ext.snakeyaml.CommentedWriter.descendAndDo(CommentedWriter.java:157) ~[?:?]
at space.arim.dazzleconf.ext.snakeyaml.CommentedWriter.writeValue(CommentedWriter.java:188) ~[?:?]
at space.arim.dazzleconf.ext.snakeyaml.CommentedWriter.writeRemainingMapEntries(CommentedWriter.java:112) ~[?:?] at space.arim.dazzleconf.ext.snakeyaml.CommentedWriter.writeMap(CommentedWriter.java:93) ~[?:?]
at space.arim.dazzleconf.ext.snakeyaml.CommentedWriter.writeData(CommentedWriter.java:84) ~[?:?]
at space.arim.dazzleconf.ext.snakeyaml.SnakeYamlConfigurationFactory.writeMapToWriter(SnakeYamlConfigurationFactory.java:148) ~[?:?]
at space.arim.dazzleconf.factory.AbstractConfigurationFactory$ConfigFactoryDelegate.writeMap(AbstractConfigurationFactory.java:153) ~[?:?]
at space.arim.dazzleconf.factory.HumanReadableConfigurationFactory.bufferedWriteMap(HumanReadableConfigurationFactory.java:125) ~[?:?]
at space.arim.dazzleconf.factory.HumanReadableConfigurationFactory.writeMap(HumanReadableConfigurationFactory.java:114) ~[?:?]
at space.arim.dazzleconf.factory.ConfigurationFormatFactory.write(ConfigurationFormatFactory.java:184) ~[?:?]
at space.arim.dazzleconf.factory.DelegatingConfigurationFactory.write(DelegatingConfigurationFactory.java:72) ~[?:?]
at space.arim.dazzleconf.factory.AbstractConfigurationFactory.write(AbstractConfigurationFactory.java:42) ~[?:?] at space.arim.dazzleconf.helper.ConfigurationHelper.reloadConfigData(ConfigurationHelper.java:102) ~[?:?]
at pl.minecodes.minespawner.configuration.ConfigurationLoader.reloadConfig(ConfigurationLoader.java:35) ~[?:?]
at pl.minecodes.minespawner.MineSpawner.loadConfiguration(MineSpawner.java:33) ~[?:?]
at pl.minecodes.minespawner.MineSpawner.onEnable(MineSpawner.java:41) ~[?:?]
from dazzleconf.
Released in 1.2.1
from dazzleconf.
Related Issues (20)
- Add documentation on how to use file-loaded defaults HOT 2
- Type-Safe Placeholders/Templating with ValueSerialiser integration HOT 4
- Optionals let you leave out keys HOT 7
- Recursive Serializer HOT 6
- Feature: Implement nested map deserialization to configuration calls as a native exposed functionality
- Entire map of nested sections is reset to default when one section is missing a required key HOT 4
- Consider deprecating @ConfSerialisers? HOT 1
- Suggestions for a better DazzleConf HOT 1
- Bytecode-powered optional enhancements HOT 1
- Please Read: Requiring default values for 2.0 + related changes
- Type-safe configuration migration
- Map method names to configuration keys automatically (deprecate @ConfKey?)
- Turn all serialization to use the value serializer model HOT 2
- Define a structure for configuration definitions
- make ConfigManager from documentation part of api HOT 1
- Handle invalid config values individually with cleanup callback
- Map Key Requirement for Serializer (Hocon)
- Long Config Value Locked at Lower Value HOT 3
- How to add a generic type? HOT 2
- Is there a way to set/add values in a configuration? HOT 2
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 dazzleconf.