-
Notifications
You must be signed in to change notification settings - Fork 42
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
#30 Add dropAlways annotation #31
Conversation
aggregate: TermName, | ||
omitJson: Boolean |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is definitely the right way to solve it, but you will hit the same binary compatibility issues that I hit the last time I attempted to change the Arguments structure. I have a refactoring in #24 that take all these structures private, but it includes other changes I don't think we want to make now. I'll remove those changes and just include the refactoring.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, Russ. I'll hold off on begging for stamps for this PR until yours is in. 🙂 Then build off your changes.
@@ -192,6 +192,10 @@ object Annotated { | |||
object C { | |||
implicit def rw: RW[C] = WeePickle.macroFromTo | |||
} | |||
case class D(a: String, @jsonIgnore b: Option[String], c: String) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How the case class instance can be parsed parsed back if @jsonIgnore
or @dropAlways
is used for a non-optional field without default values?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, good catch. I was wondering about this, whether it should be ignored on both serialization and deserialization, or require a default or Option
type. I've previously used Jackson's @JsonIgnore
annotation for fields that a team's frontend consumers no longer supported, but another team's direct backend consumers still used (in a monorepo). I'll have to check my current use case: lib-pokitdok-client#PokitdokResponse has a lot of redacted fields with Play Json...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dropalways, not @JsonIgnore? Note that @dropalways is more generic -- you may be serializing to YAML and not Json.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, Doug recommended I be more generic since it looks like these macros handle several serialization types. Would make behavior more consistent as well across types. (Though I guess we could get granular on types in the future, if the need arises.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The macro doesn't have any idea if your case class will become json, yaml, msgpack, cbor, xml, csv, etc.
We already have @dropDefault
. Reusing the "drop" term made sense to me here, since it's a similar effect.
@@ -192,6 +192,10 @@ object Annotated { | |||
object C { | |||
implicit def rw: RW[C] = WeePickle.macroFromTo | |||
} | |||
case class D(a: String, @jsonIgnore b: Option[String], c: String) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dropalways, not @JsonIgnore? Note that @dropalways is more generic -- you may be serializing to YAML and not Json.
test("ignoreAnnotatedFieldsWhenSerializing") { | ||
import Annotated.D | ||
val w = FromScala(D("a", Some("b"), "c")).transform(ToJson.string) | ||
assert(w == """{"a":"a","c":"c"}""") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test FromJson
too. If b
was a String
rather than an Option[String]
(which has an assumed default of None), how would that work? Seems like a potentially problematic asymmetry. Perhaps we should require that all @dropalways fields have a default (assumed or explicit) to avoid the issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, definitely thinking along the same vein about requiring a default. #31 (comment)
Closing due to age. Please reopen if you want to revive it. |
Lets you annotate a case class field to flag it as something to ignore during serialization.
Resolves #30