Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend LOAD_KEYS_CASE_INSENSITIVELY to support something like snake_case #40

Closed
yukukotani opened this issue Oct 7, 2019 · 5 comments
Closed

Comments

@yukukotani
Copy link

Please think about following example code.

object ExampleSpec : ConfigSpec("") {
    private val exampleKey by required<String>()
    
    fun loadExampleKey(): String {
        val config = Config { addSpec(ExampleSpec) }
        	.enable(Feature.LOAD_KEYS_CASE_INSENSITIVELY)
            .withSource(
            	Source.from.json.file(File("config.json"))
            )
		
        return config[exampleKey]
    }
}

With the code above, We can load:

{
  "exampleKey": "value"
}

But we cannot load with error:
Exception in thread "main" java.util.concurrent.ExecutionException: com.uchuhimo.konf.UnsetValueException: item indentStyle is unset

  "example_key": "value"

It's very nice if both CamelCase and snake_case are supported, especially when we load config from multiple file type such as json and yaml.

@uchuhimo
Copy link
Owner

uchuhimo commented Oct 7, 2019

@monchi What's your preferred solution? What about adding a pair of functions source.toSnakeCase() and source.toCamelCase() to convert between these two conventions?

P.S. You can explicitly specify the name to workaround it now:

private val exampleKey by required<String>("example_key")

@yukukotani
Copy link
Author

yukukotani commented Oct 7, 2019

@uchuhimo
I want to present users options of using snake case or camel case. So I need example_key and exampleKey to be mapped to config[exampleKey] implicitly.

@uchuhimo
Copy link
Owner

uchuhimo commented Oct 7, 2019

@monchi I got it. I will consider implementing it in the next release. A pull request is welcome if you are willing to contribute. Since I maintain Konf part-time, there may be some delay before the next release.

@stale
Copy link

stale bot commented Jan 5, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.

@stale stale bot added the stale label Jan 5, 2020
@uchuhimo uchuhimo added 0.23.0 and removed stale labels Oct 10, 2020
@uchuhimo
Copy link
Owner

uchuhimo commented Oct 12, 2020

@monchi I have released a new version v0.23.0 that implements the desired behavior. You can try it now. Any further feedback is welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants