slick - Scala Play Forms: using HLists to get around the 22 fields limitation -
slick uses kind of hack hlists manage tables have more 22 columns, instance (auto-generated) row model constructor:
type samplesrow = hcons[int,hcons[string,hcons[option[string],hcons[int,hcons[string,hcons[int,hcons[int,hcons[option[int],hcons[option[float],hcons[option[float],hcons[option[string],hcons[option[string],hcons[boolean,hcons[option[string],hcons[option[string],hcons[option[string],hcons[option[string],hcons[option[string],hcons[option[int],hcons[option[float],hcons[option[float],hcons[option[float],hcons[option[string],hcons[option[string],hnil]]]]]]]]]]]]]]]]]]]]]]]] def samplesrow(id: int, name: string, shortname: option[string] = none, sampletypeid: int, receiveddate: string, projectid: int, taxoid: int, quantifmethodid: option[int] = none, concentration: option[float] = none, volume: option[float] = none, description: option[string] = none, comment: option[string] = none, istrashed: boolean = false, commentcustomer: option[string] = none, createdat: option[string] = none, updatedat: option[string] = none, createdby: option[string] = none, updatedby: option[string] = none, poid: option[int] = none, `260280ratio`: option[float] = none, `260230ratio`: option[float] = none, rin: option[float] = none, concupdatedat: option[string] = none, concupdatedby: option[string] = none): samplesrow = { id :: name :: shortname :: sampletypeid :: receiveddate :: projectid :: taxoid :: quantifmethodid :: concentration :: volume :: description :: comment :: istrashed :: commentcustomer :: createdat :: updatedat :: createdby :: updatedby :: poid :: `260280ratio` :: `260230ratio` :: rin :: concupdatedat :: concupdatedby :: hnil }
can use same trick have form hold 24 values? in
val tableform: form[samplesrow] = form( mapping( id -> ignored(-1), name -> text, short_name -> optional(text), sample_type_id -> number, received_date -> default(text, now), project_id -> number, taxo_id -> number, quantif_method_id -> optional(number), concentration -> optional(of[float]), volume -> optional(of[float]), description -> optional(text), comment -> optional(text), is_trashed -> default(boolean, false), comment_customer -> optional(text), created_at -> optional(text), updated_at -> optional(text), created_by -> optional(text), updated_by -> optional(text), po_id -> optional(number), ratio_260_280 -> optional(of[float]), ratio_260_230 -> optional(of[float]), rin -> optional(of[float]), conc_updated_at -> optional(text), conc_updated_by -> optional(text) )(samplesrow)(???) )
i feel constructor def samplesrow
above can used apply
method, can use "unapply
" ?
i know supposed use nested mappings, cannot map result directly slick row model , sounds silly write 100 lines of code implement conversion can insert.
i tempted rid of forms , replace them custom jsobject parsing:
// controller action: val content = request.body.asjson val row = rowfromjson(content) // somewhere else import play.api.libs.json._ def rowfromjson(j: jsobject): samplesrow = { samplesrow( (j \ "id").as[int], (j \ "name").as[string] ... ) }
where samplesrow
slick samples#tableelementtype
table samples <: abstracttable[_]
.
Comments
Post a Comment