Skip to content

Commit

Permalink
#31 Nullable properties inside @Emdedded support
Browse files Browse the repository at this point in the history
  • Loading branch information
pjagielski committed Mar 4, 2021
1 parent 0309fa3 commit 57b3ec8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
Expand Up @@ -62,12 +62,14 @@ abstract class MappingsGenerator : SourceGenerator {
val embeddableName = embeddable.propertyName.asVariable()

if (embeddable.nullable) {
val embeddableMapping = embeddable.getPropertyNames().joinToString(", \n") { name ->
val embeddableMapping = embeddable.properties.joinToString(", \n") { property ->
val name = property.name
val tablePropName = embeddable.propertyName.asVariable() + name.asVariable().capitalize()
"\t\t$name = this[${entity.name}Table.${tablePropName}]!!"
val denull = if (!property.nullable) "!!" else ""
"\t\t$name = this[${entity.name}Table.${tablePropName}]$denull"
}
val condition = embeddable.getPropertyNames().map { name ->
val tablePropName = embeddable.propertyName.asVariable() + name.asVariable().capitalize()
val condition = embeddable.properties.filterNot(PropertyDefinition::nullable).map { property ->
val tablePropName = embeddable.propertyName.asVariable() + property.name.asVariable().capitalize()
"\t\tthis[${entity.name}Table.${tablePropName}] != null"
}.joinToString(" &&\n")
"\t$embeddableName = if (\n$condition\n\t) ${embeddable.qualifiedName}(\n$embeddableMapping\n\t) else null"
Expand Down
Expand Up @@ -139,8 +139,8 @@ class TablesGenerator : SourceGenerator {
}

private fun addEmbeddedTableProperty(embeddable: EmbeddableDefinition, column: PropertyDefinition, entity: EntityDefinition, tableSpec: TypeSpec.Builder, fileSpec: FileSpec.Builder, typeEnvironment: TypeEnvironment) {
val name = typeEnvironment.elementUtils.getName(embeddable.propertyName.asVariable() + column.name.asVariable().capitalize())
val embeddedProperty = column.copy(name = name, nullable = embeddable.nullable)
val name = typeEnvironment.elementUtils.getName(embeddable.propertyName.asVariable() + column.name.asVariable().capitalize())
val embeddedProperty = column.copy(name = name, nullable = embeddable.nullable || column.nullable)
addTableProperty(embeddedProperty, entity, tableSpec, fileSpec)
}

Expand Down
9 changes: 7 additions & 2 deletions example/src/main/kotlin/pl/touk/krush/embeddable/User.kt
Expand Up @@ -14,7 +14,9 @@ data class User(
@Embedded
@AttributeOverrides(
AttributeOverride(name = "city", column = Column(name = "inv_city")),
AttributeOverride(name = "street", column = Column(name = "inv_street"))
AttributeOverride(name = "street", column = Column(name = "inv_street")),
AttributeOverride(name = "houseNumber", column = Column(name = "inv_house_no")),
AttributeOverride(name = "apartmentNumber", column = Column(name = "inv_apartment_no"))
)
val invoiceAddress: Address? = null
)
Expand All @@ -29,5 +31,8 @@ data class Address(
val street: String,

@Column(name = "house_number")
val houseNumber: Int
val houseNumber: Int,

@Column(name = "apartment_number")
val apartmentNumber: Int? = null
)

0 comments on commit 57b3ec8

Please sign in to comment.