DazzleConf icon indicating copy to clipboard operation
DazzleConf copied to clipboard

Optionals let you leave out keys

Open auriium2 opened this issue 3 years ago • 7 comments

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

auriium2 avatar Nov 07 '21 05:11 auriium2

Ye but how the config gonna look if its not there

BlueTree242 avatar Nov 07 '21 21:11 BlueTree242

don't think yaml would accept null

BlueTree242 avatar Nov 07 '21 21:11 BlueTree242

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

auriium2 avatar Nov 07 '21 21:11 auriium2

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

BlueTree242 avatar Nov 07 '21 22:11 BlueTree242

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

auriium2 avatar Nov 08 '21 03:11 auriium2

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

ghost avatar Nov 08 '21 06:11 ghost

@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 avatar Nov 08 '21 12:11 A248