There might be multiple implementations of ParseCaseClass, currently only RegexParseCaseClass is supported:
import com.github.aborg0.caseyclassy.RegexParseCaseClass
import com.github.aborg0.caseyclassy.RegexParseCaseClass._
import java.time.{LocalDate, LocalTime}
import shapeless._
It might possible to parse simple values, like Boolean, Byte, Short, Int, Long, Float, Double, LocalTime, LocalDate and Strings without comma (,) or closing parenthesis ()), but this library was designed to parse toString of algebraic data types (products -case classes, tuples- and coproducts) of them. Also some Seqs (List, Vector, WrappedArray (for varargs)) are supported.
LocalDate:
val date: LocalDate = RegexParseCaseClass.to[LocalDate]("2018-04-01")
or it is also possible to create a parser and reuse it:
val dateParser = RegexParseCaseClass[LocalDate]
dateParser.parse("2018-04-01")
dateParser.parse("2018-04-22")
Tuple2 of String and Int:
RegexParseCaseClass.to[(String, Int)]("( hello,4)")
Or in the other order:
val (i, s) = RegexParseCaseClass.to[(Int, String)]("(4, hello)")
The error messages are not very good:
val dateTuple1 = RegexParseCaseClass.to[Tuple1[LocalDate]]("2018-04-01")
With help of shapeless the following constructs are supported:
- case classes
- case objects
- sealed hierarchies
- tuples
- a few
Seqtypes
case class Example(a: Int, s: String)
RegexParseCaseClass.to[Example]("Example(-3, Hello)")
case object Dot
RegexParseCaseClass.to[Dot.type]("Dot")
RegexParseCaseClass.to[Either[Short, Boolean]]("Left(-1111)")
RegexParseCaseClass.to[Either[Short, Boolean]]("Right(false)")
RegexParseCaseClass.to[Option[Option[Int]]]("Some(None)")
RegexParseCaseClass.to[Option[Option[Int]]]("None")
RegexParseCaseClass.to[Option[Either[String, Seq[Boolean]]]]("Some(Right(List()))")
RegexParseCaseClass.to[Option[Either[String, Seq[Boolean]]]]("Some(Right(List(false, true)))")
String handling is not ideal:
RegexParseCaseClass.to[Option[Either[String, Seq[Boolean]]]]("Some(Left(List(false, true)))")
Please note that the String part contains only till the first , (List(false) within and no error is reported currently.