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

Optionals let you leave out keys #28

Open
auriium2 opened this issue Nov 7, 2021 · 7 comments
Open

Optionals let you leave out keys #28

auriium2 opened this issue Nov 7, 2021 · 7 comments
Labels
enhancement New feature or request
Milestone

Comments

@auriium2
Copy link

auriium2 commented Nov 7, 2021

Title: If you have an Optional annotated as a config value, not including a key for the value will simply make the Optional return empty.

A configuration with

@ConfigKey("some.key")
Optional<Integer> lol();

some:
  otherKey: otherValue
  
  # note the lack of some.key

should load lol() as an empty opt

@BlueTree242
Copy link

Ye but how the config gonna look if its not there

@BlueTree242
Copy link

don't think yaml would accept null

@auriium2
Copy link
Author

auriium2 commented Nov 7, 2021

What do you mean how would the config look if it were not there, there would simply not be a key entry.
some.otherKey is not the same as some.key

@BlueTree242
Copy link

Oh i got it, i think this should be added it would allow more abilities to do with the library

@auriium2
Copy link
Author

auriium2 commented Nov 8, 2021

Not sure if this is supported but i'd pay/donate/support to have this implemented by either the repo maintainer or by someone else

@ghost
Copy link

ghost commented Nov 8, 2021

I dont think that this suggestion respects one of the main ideas of library - no nullability.
https://github.com/A248/DazzleConf/blob/master/docs/MainIdeas.md

@A248
Copy link
Owner

A248 commented Nov 8, 2021

@m4nyA raised a good point. I've always avoided null values because the concept of null is ambiguous.

It's usually a bad idea to instruct users to leave a configuration option 'blank'. An empty string and a null value would be treated differently, but from the perspective of a user who knows nothing of Java, this would be quite the surprise.

Instead, I recommend using a configuration section which contains an "enable" boolean option:

some:
  otherKey:
    enabled: true
    value: 'hello'

If you really needed Optional in your code, you could then use a default method which returns an Optional:

default Optional<String> optValue() {
  return enable() ? Optional.of(value()) : Optional.empty();
}

@A248 A248 added the enhancement New feature or request label Nov 8, 2021
@A248 A248 added this to the 2.0.0 milestone Jul 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants