Search code examples
scalaapache-spark

Scala partial initialization of a case class


I've been building some tests based on manually initialized case classes. The issue is, my case class is pretty big - all types are Optional. I'd like to initialize it with only a few fields (other are null). Is this possible. So, for example

case class Employee(Name:Option(String), Age:Option(Int), Designation:Option(String), Salary:Option(Int), ZipCode:Option(Int))
val EmployeesData = Seq( Employee("Anto",   21, "Software Engineer", 2000, 56798))
val Employee_DataFrame = EmployeesData.toDF
Employee_DataFrame.show()

Would be full init, but I'd like to do e.g.,

val EmployeesData = Seq( Employee("Anto"))

Or event better, do this for specific keys somehow. Any ideas?


Solution

  • As suggested by Dima:

    Give every field a default value:

    case class Employee(
      name: Option[String] = None,
      age: Option[Int] = None,
      ...
    )
    

    Now you can do: Employee().copy(name = Some("Name")).