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

Option to always derive enums as if they were sealed traits #710

Open
mrdziuban opened this issue Jul 1, 2024 · 2 comments · May be fixed by #713
Open

Option to always derive enums as if they were sealed traits #710

mrdziuban opened this issue Jul 1, 2024 · 2 comments · May be fixed by #713

Comments

@mrdziuban
Copy link

Hi, thank you for this great library!

I'm curious if you would consider adding an option to always derive enums as if they were sealed traits/sealed abstract classes? That is, even a simple enum with only singleton cases would still produce a JSON object and could be configured with AdtEncodingStrategy.

For example:

import io.bullet.borer.{Encoder, Json}
import io.bullet.borer.derivation.MapBasedCodecs.*

enum Foo derives Encoder {
  case Bar
}
Json.encode(Foo.Bar).toUtf8String // "Bar"

sealed trait Baz derives Encoder.All
object Baz {
  case object Quux extends Baz
}
Json.encode(Baz.Quux: Baz).toUtf8String // {"Quux":{}}

I would like Json.encode(Foo.Bar) to produce {"Baz":{}} so I can use enums and sealed traits interchangeably

@sirthias
Copy link
Owner

sirthias commented Jul 2, 2024

Thanks, @mrdziuban, for this proposal.
I was thinking about the distinction between enum instances and singleton objects myself.
Conceptionally they are pretty much the same but technically they are very different, which case objects defining their own type extending Product0 while enum cases simply being instances of their enum type.

If we were to implement the option you are asking for it'd have to be opt-in configurable, since we can't break existing encodings. One way could be to add a enumCasesAsProduct flag to AdtEncodingStrategy.
Would sth like that work for you?

@mrdziuban
Copy link
Author

Yeah a new config option on AdtEncodingStrategy would be great, thank you!

@mrdziuban mrdziuban linked a pull request Jul 11, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants