Skunk - Type Mismatch

0

I am getting an error with the code below

      val query: Query[(String *: Int), ValueObject] =
        sql"""
          SELECT field1, field2
          FROM   sample_table
          WHERE  field1 = $text
          AND field2 = $int4
        """.query(ValueObject.decoder)

  database.use(s => s.option(query)((param1, param2)))

And the decoder is here:

case class ValueObject(field1: Int, field2: String)

object ValueObject {
  val decoder: Decoder[ValueObject] =
    (int4 ~ varchar).map { case (f1,  f2) => ValueObject(f1, f2) }
}

And the error is:

type mismatch;
 found   : skunk.Query[String *: Int *: org.typelevel.twiddles.EmptyTuple,br.com.ValueObject]
    (which expands to)  skunk.Query[String :: Int :: shapeless.HNil,br.com.ValueObject]
 required: skunk.Query[String *: Int,br.com.ValueObject]
    (which expands to)  skunk.Query[String :: Int,br.com.ValueObject]
        """.query(ValueObject.decoder)

Where is actually the problem?

Not sure exactly, but I think that the issue is due to the type signature mismatch. I had faced something similar before. I believe there was some breaking changes in one of the new releases that introduced twiddles.
String *: Int *: EmptyTuple
Try something like this with explicit EmptyTuple as well.

Here is the solution

  • Fixing imports
import skunk.codec.all.*
import skunk.implicits.*
import skunk.*
  • Fixing Query type as explained here
Query[String *: Int *: EmptyTuple, ValueObject]
  • Fixing decoder
val decoder: Decoder[ValueObject] =
  (int4 ~ varchar).map { case (f1, f2) =>
    ValueObject(f1, f2) 
  }