Search code examples
arraysscalaobjectmappingcase-class

Instead of Scala Map objects, can we use any other proper objects for below type of structure?(Case Class)


I have just shifted to the scala and there I have the Map object as per the below structure.

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"
  )
)

Like this, I have 1000s of query details in this map object.

I am looking for a way to use some other objects to make it more readable and understandable code.


Solution

  • As commented by @LuisMiguelMejíaSuárez, you could use a case class instead of a Map[String, Any] and it will be more readable and better typed.

    Something like this:

      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(???)
        )