阅读包含嵌套对象的播放应用程序配置文件

时间:2016-09-20 11:59:33

标签: scala playframework playframework-2.0

我想根据提供的fileType和companyName

获取电子邮件ID列表
def getEmailAddresses(fileType: String, companyName: String): List[String] = {
  val xz = Play.application.configuration.getConfigList("email_list")
  println(xz)
}

我的上面的函数给了我一些复杂的配置列表,它不容易遍历。

我基本上想要List[String],这只是电子邮件ID列表。

例如,给定参数:

  • fileType = test_2
  • companyName = company2

我想获得字符串"user1@gmail.com, user2@gmail.com" 可以通过List[String]

转换为.split(",").toList

这可以简化吗?

以下是我在scala play应用程序中的application.conf文件

email_list = [
  {
    file0 : "user1@gmail.com,user2@gmail.com"
  },
  {
    file1 : [
      {"company1" : "user1@gmail.com"},
      {"company2" : "user1@gmail.com"},
      {"company3" : "user1@gmail.com"}
    ]
  },
  {
    top2 = [
      {"company1": "user1@gmail.com,user2@gmail.com"},
      {"company2": "user1@gmail.com,user2@gmail.com"},
      {"company3": "user1@gmail.com,user2@gmail.com"}
    ]
  },
  {
    test_2 = [
      {"company1": "user1@gmail.com,user2@gmail.com"},
      {"company2": "user1@gmail.com,user2@gmail.com"},
      {"company3": "user1@gmail.com,user2@gmail.com"}
    ]
  },
  {
    xyz_7 = [
      {"company1": "user1@gmail.com,user2@gmail.com"},
      {"company2": "user1@gmail.com,user2@gmail.com"},
      {"company3": "user1@gmail.com,user2@gmail.com"}
    ]
  },
  {
    abc_def = [
      {"company1": "user1@gmail.com,user2@gmail.com"},
      {"company2": "user1@gmail.com,user2@gmail.com"},
      {"company3": "user1@gmail.com,user2@gmail.com"}
    ]
  }
]

1 个答案:

答案 0 :(得分:0)

我已经对HOCON语法进行了一些试验,发现以下代码+配置正常工作

    def getEmailAddresses(fileType: String, companyName: String): List[String] = {
        // fileType = "file1"
        // companyName = "company1"

        val file0 = Play.application.configuration.getString("email_list.file0")
        println(file0)

        val file1_company1 = Play.application.configuration.getString(s"email_list.${fileType}.${companyName}")
        println(file1_company1)
    }

application conf

email_list {

    file0 = "user1@gmail.com,user2@gmail.com"

    file1 {
      company1 = "user1@gmail.com"
      company2 = "user1@gmail.com"
      company3 = "user1@gmail.com"
    }

    top2 {
      company1 = "user1@gmail.com,user2@gmail.com"
      company2 = "user1@gmail.com,user2@gmail.com"
      company3 = "user1@gmail.com,user2@gmail.com"
    }

    test_2 {
      company1 = "user1@gmail.com,user2@gmail.com"
      company2 = "user1@gmail.com,user2@gmail.com"
      company3 = "user1@gmail.com,user2@gmail.com"
    }

    xyz_7  {
      company1 = "user1@gmail.com,user2@gmail.com"
      company2 = "user1@gmail.com,user2@gmail.com"
      company3 = "user1@gmail.com,user2@gmail.com"
    }

    abc_def  {
      company1 = "user1@gmail.com,user2@gmail.com"
      company2 = "user1@gmail.com,user2@gmail.com"
      company3 = "user1@gmail.com,user2@gmail.com"
    }
}