in elitzur-avro/src/main/scala/com/spotify/elitzur/converters/avro/AvroConverter.scala [276:315]
override def fromAvro(v: Any, schema: Schema, doc: Option[String] = None): T = {
val ps = caseClass.parameters
val cs = new Array[Any](ps.length)
var i = 0
while (i < ps.length) {
val p = ps(i)
// Since our labels are camelCase in scala and snake_case in avro, we convert to snake to
// retrieve the param ref
val fieldName = if (schema.getField(p.label) == null) {
SharedUtils.camelToSnake(p.label)
} else {
p.label
}
val field = schema.getField(fieldName)
val innerSchema = field.schema()
val doc = Option(field.doc())
// We assume union types are used for optional fields only, can be adjusted in the future
val schemaParam = innerSchema.getType match {
case Schema.Type.UNION =>
val candidateSchema = innerSchema.getTypes.asScala.find(_.getType != Schema.Type.NULL)
candidateSchema.map(_.getType) match {
case Some(Schema.Type.RECORD) => candidateSchema.get
case Some(Schema.Type.ARRAY) => candidateSchema.get
case _ => innerSchema
}
case _ => innerSchema
}
cs.update(i,
p.typeclass.fromAvro(
v.asInstanceOf[GenericRecord].get(fieldName),
schemaParam,
doc
)
)
i += 1
}
caseClass.rawConstruct(ArraySeq.unsafeWrapArray(cs))
}