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.

How to avoid the need to import custom ProductHint on loading config

See original GitHub issue

Hi, I am consistently using camelCase for config keys, and I use a single entry point to read config from, used in multiple places (tests etc) (provided bellow). I currently need to consistently import a camelCase product hint in every place that uses that method (or loadConfig directly for that matter). Config parser method:

import pureconfig.{ConfigConvert, loadConfig}

object Parser {
  def parse[T](conf: Config)(implicit convert: ConfigConvert[T]): T = {
    val loaded = loadConfig[T](conf)(convert)
    loaded.left.foreach(_.toList.map(_.toString).foreach(f => logError(f)))
    loaded.right.get
  }
  import pureconfig.{ProductHint, CamelCase, ConfigFieldMapping}
  implicit def camelCaseHint[T]: ProductHint[T] = ProductHint[T](ConfigFieldMapping(CamelCase, CamelCase))
}

You can see I tried a few naive things around trying to avoid having ProductHint imported in the scope of the method.

Usage:

import Parser.camelCaseHint //Will look for kebab-case keys without this!
val fromConfig = Parser.parse[MyConfigClass](config)

Picking up an implicit value that is not explicitly included in the list of implicit parameters of the function (loadConfig in this case, or even my parse function) is quite dark magic to me. In fact, I didn’t know Scala allowed this! And I had a look at the classic SO question on this.

How would you approach this?

I can guess this goes down to the fact that the ProductHint companion object defines a default implicit instead of having this imported explicitly (e.g. by import pureconfig._)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
ruippeixotogcommented, Mar 17, 2019

Closing this due to inactivity and because it’s more of a design decision than a bug/issue.

2reactions
eneveucommented, Jan 21, 2019

In our project, we use a trait to import the implicit ProductHint:

trait ConfigSupport {
  implicit def hint[T]: ProductHint[T] = ProductHint[T](ConfigFieldMapping(CamelCase, CamelCase))
}

And we use it like this:

object XxxAppConfig extends ConfigSupport {
  def loadConfigOrThrow(config: Config = ConfigFactory.load()): XxxAppConfig = {
    pureconfig.loadConfigOrThrow[XxxAppConfig](config, "some.namespace")
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Import Data Into Salesforce - Complete Guide
This guide will cover typical common data loading challenges, how to choose the right data import tool for your Salesforce project, and six ......
Read more >
Configurations - PortSwigger
You can use Burp's configuration library to manage different Burp ... Or you might need to load a particular configuration when working on...
Read more >
PyLint "Unable to import" error - how to set PYTHONPATH?
I'm running PyLint from inside Wing IDE on Windows. I have a sub-directory (package) in ...
Read more >
Environment Variables - Sanity.io
You can use environment variables to dynamically configure the Studio depending on what context it's in.
Read more >
Custom Settings for the Sony FX6 - YouTube
I this video I show you my custom set settings or the Sony FX6 and how you can simply add them to your...
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