I’m just getting started with Anorm and parser combinators. It seems like there is an awful lot of boilerplate code. For example, I have
case class Model(
id:Int,
field1:String,
field2:Int,
// a bunch of fields omitted
)
val ModelParser:RowParser[RegdataStudentClass] = {
int("id") ~
str("field1") ~
int("field2") ~
// a bunch of fields omitted
map {
case id ~ field1 ~ field2 //more omissions
=> Model(id, field1, field2, // still more omissions
)
}
}
Each database field is repeated four (!) times before the whole thing is defined. It seems like the parser should be able to be deduced semi-automatically from the case class. Any tools or other techniques to suggest to reduce the work involved here?
Thanks for any pointers.
Here’s the solution I eventually developed. I currently have this as a class in my Play project; it could (should!) be turned into a stand-alone tool. To use it, change the
tableNameval to the name of your table. Then run it using themainat the bottom of the class. It will print a skeleton of the case class and the parser combinator. Most of the time these skeletons require very little tweaking.Byron