对于下面的结构类型,我们可以使用任何其他合适的对象来代替ScalaMap对象吗?(CASE类)

iyfjxgzm  于 2022-11-09  发布在  Scala
关注(0)|答案(1)|浏览(87)

我刚刚转到了Scala,根据下面的结构,我有了Map对象。

object report {

  def report_sn(flag : Boolean = false) : Map[String, Map[String,Any]]  = Map(
      "100"->Map("reportName"->"XYZ",
      "queryColumns"->Array("title", "startDate", "endDate"),
      "groupBy"->Array(),
      "groupByFunctions"->Array(),
      "query"->("SELECT * "+
        "FROM "+
        " abctable limit 10 "),
      "queryParams"->Array(),
      "xmlTemplate"->"xyz",
      "processFunction"->"pqrFun"
    ),
    "101"-> Map("reportName"->"XYZ1",
      "queryColumns"->Array("title", "startDate", "endDate"),
      "groupBy"->Array(),
      "groupByFunctions"->Array(),
      "query"->("SELECT * "+
        "FROM "+
        " abc1table limit 10 "),
      "queryParams"->Array(),
      "xmlTemplate"->"xyz1",
      "processFunction"->"pqr1Fun"
  )
)

就像这样,我在这个map对象中有1000个查询详细信息。
我正在寻找一种方法来使用一些其他对象,使其更容易阅读和理解的代码。

t5fffqht

t5fffqht1#

正如@LuisMiguelMejíasuárez评论的那样,您可以使用case class而不是Map[String, Any],它将更具可读性,打字也更好。
大概是这样的:

case class Report(reportName: String,
                    queryColumns: List[String],
                    groupBy: List[String],
                    groupByFunctions: List[String],
                    query: String,
                    queryParams: List[String],
                    xmlTemplate: String,
                    processFunction: String
  )

  def report_sn(flag: Boolean = false): Map[String, Report] =
    Map(
      "100" -> Report(
        reportName = "XYZ",
        queryColumns = List("title", "startDate", "endDate"),
        groupBy = List(),
        groupByFunctions = List(),
        query = "SELECT * FROM abctable limit 10",
        queryParams = List(),
        xmlTemplate = "xyz",
        processFunction = "pqrFun"
      ),
      "101" -> Report(???)
    )

相关问题