gestalt-config / gestalt

A Java configuration library
https://gestalt-config.github.io/gestalt/
Apache License 2.0
84 stars 2 forks source link

Allow nullable for Records #199

Closed brainbytes42 closed 4 months ago

brainbytes42 commented 5 months ago

I love working with Optionals, and now they are working perfectly with Gestalt - but unfortunately, Optional isn't serializable... So a new question arose... ;-)

In https://github.com/gestalt-config/gestalt?tab=readme-ov-file#decoders it's stated, that for java Records @Nullable isn't an Option to have values omitted... Is there any chance to make that possible, so that a record's field, if annotated as @Nullable, may be missing and then set to null?

conf = {
   foo = "Hello"
// bar = "World"
}
record Conf (String foo, @Nullable String bar){}

// => conf.foo = "Hello"
// => conf.bar = null

Thank you :-)

credmond-git commented 5 months ago

Thank you for the feature request.

Which nullable annotation library are you using? I was looking at jetbrains annotations but it has @Retention(RetentionPolicy.CLASS) so the annotation is not available in the runtime. I am not familiar with a library that includes a Nullable annotation with @Retention(RetentionPolicy.RUNTIME)

credmond-git commented 5 months ago

Have you tried treatMissingDiscretionaryValuesAsErrors. It should allow records to have null values, but it will disable all null value checking. Which may not be what you are looking for.

credmond-git commented 5 months ago

I found https://mvnrepository.com/artifact/jakarta.annotation/jakarta.annotation-api/3.0.0 which looks like it fits the bill. I am adding support for records and objects. It will be treated as an MISSING_OPTIONAL_VALUE

brainbytes42 commented 5 months ago

Have you tried treatMissingDiscretionaryValuesAsErrors. It should allow records to have null values, but it will disable all null value checking. Which may not be what you are looking for.

Yeah, I've tried this one, but it didn't change the behaviour.

I found https://mvnrepository.com/artifact/jakarta.annotation/jakarta.annotation-api/3.0.0 which looks like it fits the bill. I am adding support for records and objects. It will be treated as an MISSING_OPTIONAL_VALUE

Yay! :-)

That's what I'm using - I should hav said that. Jakarta, formerly known as javax, is one of the usual libraries for that, I think.

I have seen libraries checking for Nullable / NonNull / NotNull / ... just by String-comparison of the class name. At first, I thought it would be odd, but later I understood, it makes the library more robust / loosely coupled. Maybe this also worth thinking about? [Edit: I just saw that you already have a commit and that you are using this technique already. :-D ]

For the docs, it would be useful, though, to give a heads up regarding the needed runtime retention and maybe reference jakarta as viable solution.

Thank you! :-)

credmond-git commented 5 months ago

I released update v0.31.1 last night that checks for a nullable annotation and changes the error from a missing value to a missing Discretionary Values. As long as you have not enabled treatMissingDiscretionaryValuesAsErrors it should give you the expected results. As you mentioned i checked for the annotation by name so i dont need to have a dependency on any annotation libraries. I still have to add some documentations and integration tests. Please let me know if this works for you.

brainbytes42 commented 4 months ago

Works perfectly fine, thanks for your quick support! 👍