question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Clarifying nested object conversion

See original GitHub issue

I’m trying to understand how I can achieve the following:

sealed trait TC
case class ITC(count:Int, work:TC) extends TC
case class SSTC(query:String, args:List[AnyRef]) extends TC

case class Global(work:List[TC], runtime:Option[FiniteDuration)

I can’t seem to understand the process that would be required to achieve something like this. I get the exception listed in the documentation (i.e could not find implicit value for parameter conv: pureconfig.ConfigConvert[Global]) but I can’t for the life of me figure out what’s causing it or what steps I can use to fix it.

I’ve tried to create a custom converter, but I’m not sure how to leverage pureconfig to load the types it already knows about.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9

github_iconTop GitHub Comments

2reactions
melriefcommented, Jan 30, 2017

The error means that pureconfig cannot find the ConfigConvert instance for one or more types in Global and consequently it cannot derive the ConfigConvert for Global itself. Now the question is which type. There is a way to check the type by using a scala console. Basically you can check every type until you get an error

> import pureconfig.ConfigConvert
> ConfigConvert[Int]
res0: pureconfig.ConfigConvert[Int] = pureconfig.ConfigConvert$$anon$5@4737d730
> // Int has a ConfigConvert, no error
> ConfigConvert[String]
res1: pureconfig.ConfigConvert[String] = pureconfig.ConfigConvert$$anon$5@1683a9f5
> // same for String
> ConfigConvert[FiniteDuration]
res2: pureconfig.ConfigConvert[FiniteDuration] = pureconfig.ConfigConvert$$anon$6@489b26b1
> // same for FiniteDuration...
> ConfigConvert[AnyVal]
<console>:17: error: could not find implicit value for parameter conv: pureconfig.ConfigConvert[AnyVal]
       ConfigConvert[AnyVal]
> // ...but not for AnyVal! The issue is AnyVal

The issue is that AnyVal doesn’t have a ConfigConvert because it’s an abstract class and not a sealed family. The good news is that AnyVal contains all types that can easily be converted from/to String and it should be easy to implement a ConfigConverter for it. Scala defines nine types that are subclasses of AnyVal, which are Double, Float, Long, Int, Char, Short, Byte, Unit and Boolean. pureconfig has ConfigConvert instances for Double, Float, Long, Int, Short and Boolean. You can either decide to ignore the remaining types, which are Char, Byte and Unit, or create ConfigConvert for them. It’s up to you but here I’m going to ignore them and show how to create the ConfigConvert of AnyVal from existing ConfigConvert instances for the subclasses. The code that I’m going to use can be enhanced with the missing ConfigConvert instances. The code for AnyVal would be:

import com.typesafe.config.{ConfigFactory,ConfigValue}
import pureconfig._
import pureconfig.ConfigConvert._
import scala.util.Try

implicit val anyValConfigConvert: ConfigConvert[AnyVal] = new ConfigConvert[AnyVal] {

  def from(cv: ConfigValue): Try[AnyVal] =
      ConfigConvert[Double].from(cv) orElse
      ConfigConvert[Int].from(cv)    orElse
      ConfigConvert[Long].from(cv)   orElse
      ConfigConvert[Float].from(cv)  orElse
      ConfigConvert[Short].from(cv)  orElse
      ConfigConvert[Boolean].from(cv)

  def to(a: AnyVal): ConfigValue = ???
}

Now that there is an instance of ConfigConvert for AnyVal, pureconfig can derive Global:

> val config = ConfigFactory.parseString(
      """{
        | work: [
        |   { type: "sstc",
        |     query: "na",
        |     args: [ 1, 2.4, "3", false ]
        |    }
        | ],
        | runtime: 1s
        |}""".stripMargin)
> println(loadConfig[Global](config))
Success(Global(List(SSTC(na,List(1.0, 2.4, 3.0, false))),Some(1 second)))
0reactions
sidhant92commented, Aug 2, 2017

@melrief What workaround has been found for this? Im having the same issue. My Nested Case Classes is not compiling. Its not related to AnyVal.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to simplify conversion of nested object into array of objects?
Try following. let arr = [{"children":[{"children":[{"children":[],"Id":1,"Name":"A","Image":"http://imgUrl"}],"Id":2,"Name":"B" ...
Read more >
Flat JSON To Nested Objects | Swift 4, Xcode 10 - YouTube
Today go over how to convert flat JSON (non- nested ) into nested Swift objects with Decodable/Codable. Following this approach will make you ......
Read more >
Converting types of keys of nested object to respective CSS ...
Converting types of keys of nested object to respective CSS key type. Give an object which would look something like:
Read more >
How to handle a bunch of nested ValueObjects?
Currently the objects provide long constructors, that get a structured array data passed in and convert / map it to their plain properties...
Read more >
Map and object: converting nested objects - Xul.fr
Map and object: converting nested objects ... When we want to save the contents of a JavaScript Map structure in a file, we...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found