From CSV to objects with Scala's pattern matching March 7, 2012
One or the primary reasons I liked scala so much is pattern matching. In combination with Case classes its an extremely powerful tool.
Any software project includes some sort of migration. Most common way that data comes in is text files with some delimiters. So lets say we pick up a file that contains lines like the bellow:
Here text fields are surrounded by ~ and all columns separated by ^
So how do you import this into Scala?
The idea is to create a small regular expression for each of the fields you expect to get. For example a string regular expression for the strings:
val text = """~([^~]*)~"""
Then you compose these fields in a line regular expression like the following:
val RegEx = List(digit, text, text, text, double).mkString(separator).r
and finally read the lines and for each one match on the full regular expression:
heres the full code for the example:
Note here that the full code that actually reads and extracts the information from the file is less than 10 lines, and only 2-3 of them actually are where the magic is happening. If you are doing more than one tables of the same format you should be able to abstract a lot of this code and simply have a function of 3-4 lines of scala code for each table. If you are using Squeryl or any other ORM it should be very very simple to insert into the database, especially if you are following the import table definition.