Octavian R.
Octavian R.

Reputation: 1271

Shapeless: could not find implicit for Coproduct mapping

I have the following code which uses circe to deserialize a json which can have two shapes (see the values of jsonPersonalDetails and jsonPersonalAddress). When I try to call the method transform from SearchCriteria, I get:

Error:(38, 26) could not find implicit value for parameter mapper: shapeless.ops.coproduct.Mapper.Aux[com.example.circe.CirceShapeApp.transformer.type,com.example.circe.CirceShapeApp.SearchBy,com.example.circe.CirceShapeApp.JsonPayload]

Can you help me to figure out why the implicit mapper is not resolved?

Thank you!

    import io.circe.generic.auto._
    import io.circe.shapes._
    import io.circe.parser._
    import shapeless._
    import shapeless.ops.coproduct.Mapper

    object CirceShapeApp extends App {

      val jsonPersonalDetails = """{"searchCriteria": {"birthDate":"01-01-1980", "lastName":"John"}}""".stripMargin
      val jsonPersonalAddress = """{"searchCriteria": {"postalCode":"10776", "houseNumber":"34"}}""".stripMargin

      class SearchCriteria(val searchCriteria: SearchBy) {

        def transform(implicit mapper: Mapper.Aux[transformer.type, SearchBy, JsonPayload]) = {
          searchCriteria map transformer
        }
      }

      case class PersonalDetails(birthDate: String, lastName: String)

      case class PersonalAddress(postalCode: String, houseNumber: String)

      type SearchBy = PersonalDetails :+: PersonalAddress :+: CNil
      type JsonPayload = String :+: CNil

      object transformer extends Poly1 {

        implicit def casePersonalDetails = at[PersonalDetails](v => s"""{"bd": "${v.birthDate}", "ln":"${v.lastName}"}""")

        implicit def casePersonalAddress = at[PersonalAddress](v => s"""{"pc": "${v.postalCode}", "hn":"${v.houseNumber}"}""")

      }

      val result = decode[SearchCriteria](jsonPersonalDetails).right.get
      println(result.searchCriteria.map(transformer)) //WORKS
      println(result.transform) //DOESN'T WORK => COMPILATION ERROR
    }

Upvotes: 2

Views: 554

Answers (1)

Octavian R.
Octavian R.

Reputation: 1271

I found the problem. The JsonPayload type was incorrect defined. Instead of:

type JsonPayload = String :+: CNil

I should have:

type JsonPayload = String :+: String :+: CNil

Upvotes: 2

Related Questions