in scio-avro/src/main/scala/com/spotify/scio/avro/types/SchemaUtil.scala [29:80]
def toPrettyString1(schema: Schema, indent: Int = 0): String =
toPrettyString(schema.getName, schema, indent)
/** Convert schema to case class definitions. */
def toPrettyString(className: String, schema: Schema, indent: Int): String =
getCaseClass(className, schema, indent)
private def getFieldType(
className: String,
fieldName: String,
fieldSchema: Schema,
indent: Int
): (String, Seq[String]) =
fieldSchema.getType match {
case BOOLEAN => ("Boolean", Seq.empty)
case INT => ("Int", Seq.empty)
case LONG => ("Long", Seq.empty)
case FLOAT => ("Float", Seq.empty)
case DOUBLE => ("Double", Seq.empty)
case STRING | ENUM => ("String", Seq.empty)
case BYTES => ("ByteString", Seq.empty)
case ARRAY =>
val (fieldType, nested) =
getFieldType(className, fieldName, fieldSchema.getElementType, indent)
(s"List[$fieldType]", nested)
case MAP =>
val (fieldType, nested) =
getFieldType(className, fieldName, fieldSchema.getValueType, indent)
(s"Map[String,$fieldType]", nested)
case UNION =>
val unionTypes = fieldSchema.getTypes.asScala.map(_.getType).distinct
if (unionTypes.size != 2 || !unionTypes.contains(NULL)) {
throw new IllegalArgumentException(
s"type: ${fieldSchema.getType} is not supported. " +
s"Union type needs to contain exactly one 'null' type and one non null type."
)
}
val (fieldType, nested) =
getFieldType(
className,
fieldName,
fieldSchema.getTypes.asScala.filter(_.getType != NULL).head,
indent
)
(s"Option[$fieldType] = None", nested)
case RECORD =>
val nestedClassName = s"$className$$${fieldSchema.getName}"
val nested =
getCaseClass(nestedClassName, fieldSchema, indent)
(nestedClassName, Seq(nested))
case t => throw new IllegalArgumentException(s"Type: $t not supported")
}