2017-10-16 19 views
0

Typesafe Config 및 scala의 pureconfig를 사용하여 다음 수준의 추상화를 사용하여 다음 메소드를 생성 할 수 있습니까? 구성 판독기가 follows으로 지정되어야하는 정의 된 사례 클래스가 있음을 알고 있습니다. 왜냐하면 다음은 limitations이기 때문입니다 ... 모든 유형의 사례 클래스는 ... 모든 경우에 ConfigReaders가 구현되어 있습니까?은 매개 변수 판독기에 대한 암시 적 값을 찾을 수 없습니다. pureconfig.ConfigReader [T]

/** 
     * @param path the.path.to.the.branch 
     * @param config the com.typesafe.config obj 
     * @tparam T - the type of the case class obj 
     * @return the filled-in obj of type T 
     * 
     */ 
    def getConfigType[T](path: String, config :Config) :Option[T] = { 

     val renderOpts = ConfigRenderOptions.defaults 
     .setOriginComments(false).setComments(false).setJson(true) 
     val levelConfig :Config = config.getConfig(path) 
     val strConfig: String = config.getConfig(path).root().render(renderOpts) 

     loadConfig[T](ConfigFactory.parseString(strConfig)) match { 
     case Right(objFilledCaseClass) => Some(objFilledCaseClass) 
     case Left(errors) => throw new RuntimeException(s"Invalid configuration: $errors") 
     } 
    } 
    } 

답변

1

난 당신이 같은 시간 오류를 구축 할 수 있다고 가정 "오류 : (17, 18) 매개 변수 리더에 대한 암시 적 가치를 찾을 수 없습니다 : pureconfig.ConfigReader [T] loadConfig [T] ... "

오류는 pureconfig의 loadConfig 메소드가 reader 매개 변수에 대한 암시 적 값을 찾지 못한다는 것입니다. 한 가지 해결책은 명시 적 리더 매개 변수를 명시 적으로 제공하는 것입니다. getConfigType 메소드는 암시 적 reader를 매개 변수로 취할 수 있으므로 getConfigType 인터페이스는 loadConfig가 작동하는 것처럼 작동합니다.

내가 희망
case class YourCustomType() 
import eu.timepit.refined.pureconfig._ 
getConfigType[YourCustomType](path, config) 

이이 문제

해결 :

import pureconfig.{ConfigReader, loadConfig} 
// Is there a reason why return type is Option? Method either returns Some or throws exception 
def getConfigType[ConfigType](path: String, config :Config)(implicit reader: ConfigReader[ConfigType]):ConfigType = 
{ 
... 
loadConfig(ConfigFactory.parseString(strConfig))(reader) match { 
... 
} 
... 
} 

그런 다음 당신이 그것을 호출

그래서 솔루션으로 인터페이스를 정의 할 것