Skip to content

Commit

Permalink
Updated record generator to not include unused schemas and encoders
Browse files Browse the repository at this point in the history
  • Loading branch information
sksamuel committed Apr 28, 2024
1 parent 5da24f4 commit 5321e2f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
Expand Up @@ -28,9 +28,11 @@ class RecordEncoderGenerator {
appendLine(" override fun encode(schema: Schema): (${kclass.java.simpleName}) -> GenericRecord {")
appendLine()
kclass.declaredMemberProperties.forEach { property ->
appendLine(" val ${property.name}Schema = schema.getField(\"${property.name}\").schema()")
if (!isDirect(property))
appendLine(" val ${property.name}Schema = schema.getField(\"${property.name}\").schema()")
appendLine(" val ${property.name}Pos = schema.getField(\"${property.name}\").pos()")
appendLine(" val ${property.name}Encode = ${encode(property)}")
if (!isDirect(property))
appendLine(" val ${property.name}Encode = ${encode(property)}")
}
appendLine()
appendLine(" return { value ->")
Expand All @@ -51,19 +53,30 @@ class RecordEncoderGenerator {
return "$wrapped.encode(${property.name}Schema)"
}

private fun encoderInvocation(property: KProperty1<out Any, *>): String {
val getValue = "value.${property.name}"
/**
* Returns true if the generator will bypass any Encoder and use the value directly.
* This is used for primitives where the Encoder is just a pass through.
*/
private fun isDirect(property: KProperty1<out Any, *>): Boolean {
return when (property.returnType.classifier) {
Boolean::class -> getValue
Double::class -> getValue
Float::class -> getValue
Int::class -> getValue
Long::class -> getValue
String::class -> getValue
else -> "${property.name}Encode.invoke($getValue)"
Boolean::class -> true
Double::class -> true
Float::class -> true
Int::class -> true
Long::class -> true
String::class -> true
else -> false
}
}

private fun encoderInvocation(property: KProperty1<out Any, *>): String {
val getValue = "value.${property.name}"
return if (isDirect(property))
getValue
else
"${property.name}Encode.invoke($getValue)"
}

private fun encoderFor(type: KType): String {
return when (val classifier = type.classifier) {
Boolean::class -> "BooleanEncoder"
Expand Down
Expand Up @@ -36,30 +36,18 @@ object MyFooEncoder : Encoder<MyFoo> {
override fun encode(schema: Schema): (MyFoo) -> GenericRecord {
val bSchema = schema.getField("b").schema()
val bPos = schema.getField("b").pos()
val bEncode = BooleanEncoder.encode(bSchema)
val cSchema = schema.getField("c").schema()
val cPos = schema.getField("c").pos()
val cEncode = LongEncoder.encode(cSchema)
val dSchema = schema.getField("d").schema()
val dPos = schema.getField("d").pos()
val dEncode = DoubleEncoder.encode(dSchema)
val fSchema = schema.getField("f").schema()
val fPos = schema.getField("f").pos()
val fEncode = FloatEncoder.encode(fSchema)
val iSchema = schema.getField("i").schema()
val iPos = schema.getField("i").pos()
val iEncode = IntEncoder.encode(iSchema)
val listsSchema = schema.getField("lists").schema()
val listsPos = schema.getField("lists").pos()
val listsEncode = ListEncoder(IntEncoder).encode(listsSchema)
val mapsSchema = schema.getField("maps").schema()
val mapsPos = schema.getField("maps").pos()
val mapsEncode = MapEncoder(StringEncoder, DoubleEncoder).encode(mapsSchema)
val sSchema = schema.getField("s").schema()
val sPos = schema.getField("s").pos()
val sEncode = NullEncoder(StringEncoder).encode(sSchema)
val setsSchema = schema.getField("sets").schema()
val setsPos = schema.getField("sets").pos()
val setsEncode = SetEncoder(StringEncoder).encode(setsSchema)
Expand Down

0 comments on commit 5321e2f

Please sign in to comment.