config4k icon indicating copy to clipboard operation
config4k copied to clipboard

Feature request: converting hyphen-case to camelCase

Open ov7a opened this issue 6 years ago • 5 comments

According to docs:

Config keys are encouraged to be hyphen-separated rather than camelCase.

So, it would be nice to have this type of conversion as option.

ov7a avatar Apr 06 '18 09:04 ov7a

Nice idea! I think the API should be like below.

object ConfigOption {
  // Default value is camelCase, but it can be changed to hyphen-separated
  var keyType: ConfigKeyType = ConfigKeyType.CAMEL_CASE
}

enum class ConfigKeyType {
  CAMEL_CASE, HYPHEN_SEPARATED
}

How do you think, @ov7a?

mmorihiro avatar Apr 08 '18 09:04 mmorihiro

In my experience, the common approach to this is to use annotations. (Take a look at jackson, for example). As user, the most convenient approach for me would be automatic conversion - unless the name is explicitly specified through annotation, first try the exact same name, then try camelCase variant. This would also allow to rename properties.

key = {
    camelCaseProperty = value
    hyphen-case-propery  = value2
    trashy_value = value3 
}

data class MixedConfig(
    val camelCaseProperty: String, //exact name
    val hyphenCaseProperty: String, //converted automatically
    @ConfigName("trashy_value") //explicit name
    val annotatedProperty: String
)

ov7a avatar Apr 08 '18 16:04 ov7a

For serializing data class to HOCON it would be logical to make an inverse conversion: transform all property names to hyphen-case, unless exact name is specified by annotation.

ov7a avatar Apr 08 '18 16:04 ov7a

Thank you for your advice. This change is quite big and may break compatibility. So, it will be implemented in version 4.

mmorihiro avatar Apr 09 '18 07:04 mmorihiro

https://github.com/config4k/config4k/pull/56 addresses the deserializing aspect of this issue. For serialization I would suggest looking at the ConfigSyntax enum when implementing. According to HOCON standards, properties should be lowercase and hyphenated. It makes sense to serialize in the correct/strict format but have a more flexible deserializer

martintreurnicht avatar Aug 16 '18 19:08 martintreurnicht

Unfortunately, we don't have a resources for implement this feature, but if someone want to do that, PRs are welcome anytime! 😄

ihostage avatar Aug 29 '22 14:08 ihostage