通过 Pureconfig 从资源目录加载不同的文件

Loading Different files from resource directory via Pureconfig

我的资源文件夹中只有几个文件,我有一个相同的映射 class。现在我想要的是使用 pureconfig 在不同的配置 class 中加载每个文件。有没有办法通过只提供资源文件夹名称来加载它。

 - src
    - main
        - resources
            - configs
                - conf1.json
                - conf2.json

我想要这样的东西

ConfigSource.resources("configs")

应该return

List<Conf>

目前的做法是这样的

def main(args: Array[String]): Unit = {
    implicit def hint[A]: ProductHint[A] =
      ProductHint[A](ConfigFieldMapping(CamelCase, CamelCase))

    val resourceFiles = getResourceFolderFiles("configs")
    val configs = new ListBuffer[SampleConfig];
    resourceFiles.foreach(file =>
      configs.append(
        ConfigSource
          .file(file)
          .load[SampleConfig]
          .getOrElse(null)))
    println(configs.size)
  }

  private def getResourceFolderFiles(folder: String): Array[File] = {
    val loader = Thread.currentThread.getContextClassLoader
    val url = loader.getResource(folder)
    val path = url.getPath
    new File(path).listFiles
  }

有没有最简单的方法?

implicit def hint[A]: ProductHint[A] =
  ProductHint[A](ConfigFieldMapping(CamelCase, CamelCase))


val sampleConfigList =
  Try(Thread.currentThread().getContextClassLoader.getResource("configs").getPath)
    .flatMap(filePath => Try(new File(filePath).listFiles().toList))
    .map(fileList =>
      fileList.flatMap(file => ConfigSource.file(file).load[SampleConfig].toOption)
    )
    .getOrElse(List.empty[SampleConfig])