Skip to content

Commit

Permalink
Fix UnexpectedNullValueInColumn exception in caseClassParser if first…
Browse files Browse the repository at this point in the history
… parameter in case class is optional (#98)

Co-authored-by: Aleksandr Shabalin <[email protected]>
  • Loading branch information
alexshabal and Aleksandr Shabalin authored Sep 19, 2024
1 parent af42780 commit 5cfb807
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/it/scala/com/ringcentral/cassandra4io/cql/CqlSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,13 @@ trait CqlSuite {
} yield expect(row.isDefined && row.get.data.isEmpty)
}

test("decoding from null should return None for optional case class first parameter") { session =>
case class OptDataReverse(data: Option[String], id: Long)
for {
row <- cql"select data, id FROM cassandra4io.test_data WHERE id = 0".as[OptDataReverse].selectFirst(session)
} yield expect(row.isDefined && row.get.data.isEmpty)
}

test("decoding from null should raise error String field in case class") { session =>
for {
result <- cql"select id, data FROM cassandra4io.test_data WHERE id = 0".as[Data].selectFirst(session).attempt
Expand Down
12 changes: 8 additions & 4 deletions src/main/scala/com/ringcentral/cassandra4io/cql/Reads.scala
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,12 @@ trait ReadsLowestPriority {
implicit def caseClassParser[A, R <: HList](implicit
gen: Generic[A] { type Repr = R },
reprParser: Reads[R]
): Reads[A] = (row: Row, index: Int) => {
val rep = reprParser.read(row, index)
gen.from(rep)
}
): Reads[A] =
new Reads[A] {
override def readNullable(row: Row, index: Int): A = {
val rep = reprParser.read(row, index)
gen.from(rep)
}
override def read(row: Row, index: Int): A = readNullable(row, index)
}
}

0 comments on commit 5cfb807

Please sign in to comment.