Scala方式选择关于不同的环境的配置

时间:2018-02-22 06:48:52

标签: scala

我的应用程序需要从资源目录或从s3读取配置文件。

对于本地开发,我需要从本地资源目录中读取它。因此,在构建项目时,我不会将配置文件config.properties放入我的应用程序jar文件中。在这种情况下,它应该从S3读取配置。当我能想到这样做时,scala就像我用java做的那样

val stream : InputStream = getClass.getResourceAsStream("/config.properties")
if (stream != null) {
  val lines = scala.io.Source.fromInputStream( stream ).getLines
} else {
  /*read it from S3*/
}

但我认为scala提供了更具功能性的编程sytax。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

可能有更好的方法来处理您之后的内容,但这里有一个或多或少直接翻译的已发布代码。

val lines:Iterator[String] = Option(getClass.getResourceAsStream("/config.properties"))
                            .fold{/*read from S3
                                  return Iterator*/}(io.Source.fromInputStream(_).getLines)