-
Notifications
You must be signed in to change notification settings - Fork 149
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
Support for enum / sealed traits without hints nor discrimination fields #749
Comments
@carlos-verdes BEWARE: Default implementations for |
Map is only an example to show the expected output, it's obviously not a good practice to load full document in memory |
I end up with something like that (but there are lot of things to improve: given credentialsDecoder: JsonDecoder[Credentials] = new JsonDecoder[Credentials]:
override def unsafeDecode(trace: List[JsonError], in: RetractReader): Credentials =
Lexer.char(trace, in, '{')
Lexer.firstField(trace, in)
val firstKey = Lexer.string(trace, in).toString
Lexer.char(trace, in, ':')
if firstKey == JWT then Token(Lexer.string(trace, in).toString)
else if firstKey == USERNAME then
val username = Lexer.string(trace, in).toString
if Lexer.nextField(trace, in) && Lexer.string(trace, in).toString == PASSWORD then
Lexer.char(trace, in, ':')
val password = Lexer.string(trace, in).toString
UserPassword(username, password)
else unsafeDecodeMissing(trace)
else if firstKey == PASSWORD then
val password = Lexer.string(trace, in).toString
if Lexer.nextField(trace, in) && Lexer.string(trace, in).toString == USERNAME then
Lexer.char(trace, in, ':')
val username = Lexer.string(trace, in).toString
UserPassword(username, password)
else unsafeDecodeMissing(trace)
else unsafeDecodeMissing(trace) |
What if the subclasses have the same fields? |
I made something similar by having explicit case class for |
@wadouk this doesn't cover the need to gather credentials with different models The idea is to do something like "getCredentialsFromJson" without the need of discriminator fields that are not part of the business domain |
@gcnyin in the case there are same fields normally you use the first which match, so the general rule is to put more specific cases first and more generic ones at the end... but in a business domain it's unlikely to happen, normally you can solve the problem with optional fields |
@carlos-verdes haven´t seen the token case. As you mention, using options would to the trick. no need of discriminator. Use a case cass with options for json codec |
That would be a workaround option but it just shows a lack of a feature don't you think? |
As a workaround you can derive zio-json codecs using zio-schema-json and its @noDicriminator annotation for that: |
Taking an enum like this:
I need to be able to parse the next 2 jsons:
Taking as input the field names of each option we can implement something like this (manual approach):
Which pass the next test:
I think this implementation can be done getting the definition of each
case
and trying to match with the json provided.Something like this on Circe:
https://circe.github.io/circe/codecs/adt.html
The text was updated successfully, but these errors were encountered: