I am working on a code where I need to access the fields of a case class based on field name in a method. Explaining in the below code
Case Class Department(
projectId: String,
name: Option[String],
id1: Option[String],
id2: Option[String],
id3: Option[String],
age: Option[Int]
){
def logicOnId(): Boolean = {
???
}
}
I want to write the same logic on all the three ids. I can write it as follows:
def logicOnId1(): Boolean = {
someLogic(this.id1)
}
def logicOnId2(): Boolean = {
someLogic(this.id2)
}
def logicOnId3(): Boolean = {
someLogic(this.id3)
}
But this is not an ideal way since it is the same logic on different columns. So, I want the process/way to write a method so that it can be used on multiple fields like
def logicOnId(input: String): Boolean = {
someLogic(input)
}
where this input is id1 or id2 or id3 as a String.