Configuration object mapper camelCase to hyphen-separated

Of course I want to follow the best practices. That’s how I do configs. @ConfigSerializable for the classes and optionally @Setting for comments.
The keys in the config are the same like in code. Is there an automatic way to convert the camelCase to hyphen-separated-keys in the actual config?

The value() field in @Setting represents the config pathname.

Yeah, I am looking for an automatic way. Like a formatter that turns the key name to hyphen separated.

You could do this by creating a custom ObjectMapper. I did something similar in Nucleus, but instead of transforming the name of the setting, I was trying to transform the comment to make it fit with my localisation system a bit better.

I did what I needed to do like this - you’d put your own logic there to split the camel case to hyphen separated keys.

https://github.com/NucleusPowered/Nucleus/blob/master/src/main/java/io/github/nucleuspowered/nucleus/configurate/objectmapper/NucleusObjectMapper.java

(this is just the standard ObjectMapper method from Configurate with lines 35-38 added.)

I then created a ObjectMapperFactory to go with it (this was a lot of copy and paste from the standard factory, there might be a better way to do it):

https://github.com/NucleusPowered/Nucleus/blob/master/src/main/java/io/github/nucleuspowered/nucleus/configurate/objectmapper/NucleusObjectMapperFactory.java

then just had to register the factory when building the configuration loader.

https://github.com/NucleusPowered/Nucleus/blob/master/src/main/java/io/github/nucleuspowered/nucleus/Nucleus.java#L141-L144

1 Like