Skip to content

generate "nullable properties" only where needed #597

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

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,26 @@ internal open class ExtensionsCodeGeneratorImpl(
return "${visibility}val$typeParameters $typeName.$name: $propertyType @JvmName(\"${renderStringLiteral(jvmName)}\") get() = $getter as $propertyType"
}

protected fun generateExtensionProperties(marker: IsolatedMarker): Code {
/**
* nullable properties can be needed when *DECLARED* schema is referenced with nullability:
* ```
* @DataSchema
* data class Schema(val i: Int)
*
* @DataSchema
* data class A(
* val prop: Schema?
* )
* ```
* When converted `listOf<A>().toDataFrame(maxDepth=2)` actual schema is
* ```
* prop:
* i: Int?
* ```
* So this sudden `i: Int?` must be somehow handled.
* However, REPL code generator will not create such a situation. Nullable properties are not needed then
*/
protected fun generateExtensionProperties(marker: IsolatedMarker, withNullable: Boolean = true): Code {
val markerName = marker.name
val markerType = "$markerName${marker.typeArguments}"
val visibility = renderTopLevelDeclarationVisibility(marker)
Expand Down Expand Up @@ -278,7 +297,6 @@ internal open class ExtensionsCodeGeneratorImpl(

declarations.addAll(
listOf(
// non nullable
generatePropertyCode(
marker = marker,
shortMarkerName = shortMarkerName,
Expand All @@ -296,29 +314,33 @@ internal open class ExtensionsCodeGeneratorImpl(
propertyType = fieldType,
getter = getter,
visibility = visibility,
),

// nullable
generatePropertyCode(
marker = marker,
shortMarkerName = nullableShortMarkerName,
typeName = nullableDfTypename,
name = name.quotedIfNeeded,
propertyType = nullableColumnType,
getter = getter,
visibility = visibility,
),
generatePropertyCode(
marker = marker,
shortMarkerName = nullableShortMarkerName,
typeName = nullableRowTypename,
name = name.quotedIfNeeded,
propertyType = nullableFieldType,
getter = getter,
visibility = visibility,
),
)
)
)
if (withNullable) {
declarations.addAll(
listOf(
generatePropertyCode(
marker = marker,
shortMarkerName = nullableShortMarkerName,
typeName = nullableDfTypename,
name = name.quotedIfNeeded,
propertyType = nullableColumnType,
getter = getter,
visibility = visibility,
),
generatePropertyCode(
marker = marker,
shortMarkerName = nullableShortMarkerName,
typeName = nullableRowTypename,
name = name.quotedIfNeeded,
propertyType = nullableFieldType,
getter = getter,
visibility = visibility,
)
)
)
}
}
return declarations.joinToString("\n")
}
Expand Down Expand Up @@ -421,7 +443,7 @@ internal class CodeGeneratorImpl(typeRendering: TypeRenderingStrategy = FullyQua
context.generatedMarkers.forEach { itMarker ->
declarations.add(generateInterface(itMarker, fields, readDfMethod.takeIf { marker == itMarker }))
if (extensionProperties) {
declarations.add(generateExtensionProperties(itMarker))
declarations.add(generateExtensionProperties(itMarker, withNullable = false))
}
}
val code = createCodeWithConverter(declarations.joinToString("\n\n"), marker.name)
Expand Down
Loading