Is there a generic way of setting field values in mapper from list of values?

StackOverflow https://stackoverflow.com/questions/8223548

  •  06-03-2021
  •  | 
  •  

Вопрос

Is there a way of creating method for setting the value in the Model's fields without setting the values explicitly like -

ModelName.create.fieldName1("value").fieldName2("value2") and so on

Can we iterate through all available fields of that model and set their values form some list-of-values ?

something like ...

Model.allFields.foreach((fld)=> {
fld.set(valueList(indx)); indx+=1
}

Actually I want to set values into all models using some generic method that works for all models.

Это было полезно?

Решение

According to my comment:

val list = List(...)
val record = YourRecordClass.createRecord
record.allFields.zip(list).foreach {case(field,value) => field.setFromAny(value)}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top