Skip to content

Commit

Permalink
Merge pull request #14 from tamimattafi/bug/nullable_entities_#11
Browse files Browse the repository at this point in the history
fix: nullable and non-nullable compounds conflict with each other

Close #11
  • Loading branch information
tamimattafi authored Apr 18, 2024
2 parents f651e85 + b80b7aa commit 7cb1891
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -677,15 +677,16 @@ class DaoGenerator(
directEntityColumn.declaration.type.toTypeName()
)

val functionName = compoundSpec.declaration
.getQueryByColumnsName(entityColumnAccess.toSortedSet())

actualFunctionName = if (relationSpec.property.dataTypeSpec.dataType is DataTypeSpec.DataType.Collection) {
val functionName = compoundSpec.declaration
.getQueryByColumnsName(entityColumnAccess.toSortedSet())

buildString {
append(functionName, "Collection")
}
} else {
functionName
compoundReturnType.getQueryByColumnsName(entityColumnAccess.toSortedSet())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ fun KSClassDeclaration.getQueryByColumnsName(columns: Set<ColumnSpec>): String =
}
}

fun DataTypeSpec.getQueryByColumnsName(columns: Set<ColumnSpec>): String =
if (columns.isEmpty()) {
declaration.getQueryByNoParametersName(isNullable)
} else declaration.buildQueryFunctionName(isNullable) {
columns.forEach { columnSpec ->
if (columnSpec.typeSpec.isNullable) {
append("Optional")
}

append(columnSpec.declaration.simpleNameString.toPascalCase())
}
}


fun KSClassDeclaration.getQueryByParametersName(parameters: Set<DaoParameterSpec>): String =
if (parameters.isEmpty()) {
getQueryByNoParametersName()
Expand All @@ -140,14 +154,23 @@ fun KSClassDeclaration.getQueryByParametersName(parameters: Set<DaoParameterSpec
}
}

fun KSClassDeclaration.getQueryByNoParametersName(): String =
buildQueryFunctionName {
append("NoParameters")
}
fun KSClassDeclaration.getQueryByNoParametersName(
isNullable: Boolean = false
): String = buildQueryFunctionName(isNullable) {
append("NoParameters")
}

fun KSClassDeclaration.buildQueryFunctionName(builder: StringBuilder.() -> Unit): String =
fun KSClassDeclaration.buildQueryFunctionName(
isNullable: Boolean = false,
builder: StringBuilder.() -> Unit
): String =
buildString {
append("query")

if (isNullable) {
append("Optional")
}

append(simpleNameString.toPascalCase())
append("By")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ data class SchoolCompound(
@Embedded
val entity: SchoolEntity,

@Relation(
entity = StudentEntity::class,
parentColumn = "name",
entityColumn = "fullName"
)
val luckyStudent: StudentCompound?,

@Relation(
entity = StudentEntity::class,
parentColumn = "address",
entityColumn = "id"
)
val otherLuckyStudent: StudentCompound,

@Relation(
entity = StudentEntity::class,
parentColumn = "identity",
Expand Down

0 comments on commit 7cb1891

Please sign in to comment.