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

Support oneof fields #30

Merged
merged 9 commits into from Dec 27, 2019
Merged

Support oneof fields #30

merged 9 commits into from Dec 27, 2019

Conversation

cb372
Copy link
Member

@cb372 cb372 commented Dec 27, 2019

Support for reading/writing shapeless coproducts as protobuf oneof fields.

The field must be annotated with a @pbIndex annotation containing the field numbers for each branch of the coproduct. Also the coproduct type must be wrapped in Option so that we can handle missing fields sensibly.

e.g.

case class CoproductMessage(
  @pbIndex(1) a: Int,
  @pbIndex(2, 3, 4) b: Option[String :+: Boolean :+: SomeOtherMessage :+: CNil]
)

Also updated the readme to document both this feature and the new support for default values/missing fields.

This PR also removes a feature that I didn't think should exist: reading and writing sealed traits of messages, e.g.

sealed trait CouldBeThisOrThat
case class This(a: Int, b: String) extends CouldBeThisOrThat
case class That(c: Boolean) extends CouldBeThisOrThat

The same functionality can now be achieved more safely and protobuf-compliantly using a oneof field.

Don't skip writing a oneof field even if it has a default scalar value.

The implementation for this is a bit nasty. I added a boolean parameter
to the signature of `PBFieldWriter` that is only used when writing a
scalar field and is ignored in all other cases. Will have a think about
a cleaner solution.
This is so we can set the field to None when it is missing when reading
a message from protobuf. If it was a plain coproduct type (e.g.
`Int :+: String :+: CNil`), there is no sensible default value we can
set it to.  We could arbitrarily set it to `Inr(0)`, but we would lose
the information that the field was missing from the message.
Copy link
Member

@juanpedromoreno juanpedromoreno left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Outstanding job :shipit:

gen: Generic.Aux[A, R],
repr: Lazy[PBMessageReader[R]]): PBMessageReader[A] = instance { (bytes: Array[Byte]) =>
gen.from(repr.value.read(bytes))
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🔥

@cb372 cb372 merged commit 219dc93 into master Dec 27, 2019
@cb372 cb372 deleted the support-oneof branch December 27, 2019 13:54
Copy link

@BenFradet BenFradet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

cb372 added a commit to higherkindness/skeuomorph that referenced this pull request Jan 2, 2020
This is for compatibility with pbdirect. See 47degrees/pbdirect#30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants