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

Ability to specify defaults by an annotation #528

Open
bdrobinson opened this issue Jul 30, 2020 · 10 comments
Open

Ability to specify defaults by an annotation #528

bdrobinson opened this issue Jul 30, 2020 · 10 comments
Labels

Comments

@bdrobinson
Copy link

I'd like to be able to specify a field as having a default value without having to use default arguments. Ie, currently I have to do:

case class MyRecord(
  some_field: Option[String] = None
)

whereas I'd like to be able to do something like:

case class MyRecord(
  @AvroDefault(None)
  some_field: Option[String]
)

The reason being that having the default value in the case class means it's very easy to forget to specify an argument in my application code when I'm instantiating the case class (which actually led to me nearly shipping a bug to production the other day). Would this be a difficult feature to add? I'd potentially be able to work on a PR – I work with @alexjg who's contributed to this codebase before.

Thanks very much!

@stale
Copy link

stale bot commented Sep 28, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Sep 28, 2020
@bdrobinson
Copy link
Author

Not stale – I'd still be interested to see how hard this feature would be to add.

@stale stale bot removed the wontfix label Sep 29, 2020
@stale
Copy link

stale bot commented Nov 28, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Nov 28, 2020
@stale stale bot closed this as completed Dec 5, 2020
@janjaali
Copy link

janjaali commented Sep 1, 2021

Could we re-open this feature-request, having the same issue, or maybe a explanation why this is not do-able?

@sksamuel sksamuel reopened this Sep 12, 2021
@stale
Copy link

stale bot commented Nov 12, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the abandoned label Nov 12, 2021
@janjaali
Copy link

Please don't stale, trying to find some time trying to handle this issue.

@stale stale bot removed the abandoned label Nov 12, 2021
@sksamuel sksamuel added pinned and removed wontfix labels Nov 15, 2021
@sksamuel
Copy link
Owner

@janjaali if you want to add this, then look here: https://github.com/sksamuel/avro4s/blob/release/4.0.x/avro4s-core/src/main/scala/com/sksamuel/avro4s/Records.scala#L200-L205

You need to update that first case statement to look for an annotation on the field. You can add the lookup code in AnnotationExtractors.

@janjaali
Copy link

@janjaali if you want to add this, then look here: https://github.com/sksamuel/avro4s/blob/release/4.0.x/avro4s-core/src/main/scala/com/sksamuel/avro4s/Records.scala#L200-L205

You need to update that first case statement to look for an annotation on the field. You can add the lookup code in AnnotationExtractors.

Thanks @sksamuel, I will try to come up with a PR in the upcoming holiday season. Wish you happy holidays and a good start into the new year ;)

@erikvanoosten
Copy link

In my situation almost every field is optional. I am looking for a way to automatically add "null" as the default value for every optional. Is something like that supported?

@erikvanoosten
Copy link

Somewhat related feature request: #714.

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

No branches or pull requests

4 participants