2

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
    }
Octavian R.
  • 1,231
  • 8
  • 12
  • Apparently, if I replace **Mapper.Aux[transformer.type, SearchBy, JsonPayload]** with **Mapper[transformer.type, SearchBy]** the code compiles. – Octavian R. May 16 '18 at 00:01
  • Check out this question as well: https://stackoverflow.com/questions/29898637/shapeless-map-from-coproduct-to-different-coproduct/29954974 – Kolmar May 16 '18 at 07:53

1 Answers1

2

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

type JsonPayload = String :+: CNil

I should have:

type JsonPayload = String :+: String :+: CNil
Octavian R.
  • 1,231
  • 8
  • 12