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

[vulcan-generic] Include default values for generated schemas #579

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/core/src/main/scala/vulcan/Codec.scala
Original file line number Diff line number Diff line change
@@ -999,7 +999,7 @@ object Codec extends CodecCompanionCompat {
override def encode(a: Option[A]): Either[AvroError, Any] = {
a match {
case Some(value) => codec.encode(value)
case None => Codec.none.encode(None)
case None | null => Codec.none.encode(None)
}
}

13 changes: 10 additions & 3 deletions modules/generic/src/main/scala-2/vulcan/generic/package.scala
Original file line number Diff line number Diff line change
@@ -48,7 +48,6 @@ package object generic {
selector: Selector[C, A]
): Prism[C, A] =
Prism.instance(selector(_))(inject(_))

implicit final class MagnoliaCodec private[generic] (
private val codec: Codec.type
) extends AnyVal {
@@ -90,15 +89,23 @@ package object generic {
}

implicit val codec = param.typeclass
val defautlValue: Option[param.PType] = param.default
.map {
case null => None.asInstanceOf[param.PType]
case other => other
}
.orElse {
(if (codec.schema.exists(_.isNullable) && nullDefaultField) Some(None)
else None).asInstanceOf[Option[param.PType]] // TODO: remove cast
}

f(
name = renamedField.getOrElse(param.label),
access = param.dereference,
doc = param.annotations.collectFirst {
case AvroDoc(doc) => doc
},
default = (if (codec.schema.exists(_.isNullable) && nullDefaultField) Some(None)
else None).asInstanceOf[Option[param.PType]] // TODO: remove cast
default = defautlValue
).widen
}
.map(caseClass.rawConstruct(_))
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ import org.apache.avro.Schema
import shapeless.{:+:, CNil, Coproduct}
import vulcan._
import vulcan.generic.examples._
import org.apache.avro.generic.GenericData

final class CoproductCodecSpec extends CodecBase {
describe("Codec") {
@@ -178,6 +179,18 @@ final class CoproductCodecSpec extends CodecBase {
"Error decoding Coproduct: Missing schema CaseClassField in union"
)
}

it("should encode as record using default values") {
assertEncodeIs[CaseClassDefaultFields](
CaseClassDefaultFields(age = Some(7)),
Right {
val record = new GenericData.Record(unsafeSchema[CaseClassDefaultFields])
record.put(0, unsafeEncode("Pikachu"))
record.put(1, unsafeEncode(7))
record
}
)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -267,6 +267,12 @@ final class GenericDerivationCodecSpec extends CodecBase {
Right(CaseClassInSealedTrait(0))
)
}

it("should use default constructor parameters for default values") {
assertSchemaIs[CaseClassDefaultFields](
"""{"type":"record","name":"CaseClassDefaultFields","namespace":"vulcan.generic.examples","fields":[{"name":"name","type":"string","default":"Pikachu"},{"name":"age","type":["null","int"],"default":null},{"name":"weight","type":["null","float"],"default":null}]}"""
)
}
}
}
}