here comes another beginners question: I am given a multiline String containing a key-value-pair per line. Now I’d like to parse the String generating a case class object from it. Assume the following case class:
case class Something(key1: String, key2: String, key3: Boolean)
and a String like:
key2: value2 key1: value1 key4: value4 key3: no
As can be seen, the String contains some hurdles:
- key-value pairs might not occur in a defined order.
- whitespaces and newlines might occur more often than required
- there might be keys inside, that can be neglected when the object is constructed
- There may be a need for implicit conversions such as key3 = yes -> True, False else
My question is: what is the most beautiful, scala-like approach for parsing. I considered to use pattern matching but using it in a loop, I would need to initially construct an “empty object” whose setters are called during parsing. This is not very scala-like.