Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Magnolia fastparse #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Magnolia fastparse #2

wants to merge 4 commits into from

Conversation

aborg0
Copy link
Owner

@aborg0 aborg0 commented Feb 11, 2020

Examples

Same content with FastParse (FastParseMagnoliaParseCaseClass)

Intro

There might be multiple implementations of ParseCaseClass, currently only RegexParseCaseClass and FastParseParseCaseClass are supported, but only one should be used in a scope (in this case FastParseParseCaseClass):

import com.github.aborg0.caseyclassy.FastParseMagnoliaParseCaseClass
import com.github.aborg0.caseyclassy.FastParseMagnoliaParseCaseClass._
import java.time.{LocalDate, LocalTime}

It is 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.

Simple primitives

LocalDate:

scala> val date: LocalDate = FastParseMagnoliaParseCaseClass.to[LocalDate]("2018-04-01")
date: java.time.LocalDate = 2018-04-01

or it is also possible to create a parser and reuse it:

scala> val dateParser = FastParseMagnoliaParseCaseClass[LocalDate]
dateParser: com.github.aborg0.caseyclassy.FastParseParse[java.time.LocalDate] = com.github.aborg0.caseyclassy.FastParseMagnoliaParseCaseClass$$anonfun$dateParse$4@7436564e

scala> dateParser.parse("2018-04-01")
res0: java.time.LocalDate = 2018-04-01

scala> dateParser.parse("2018-04-22")
res1: java.time.LocalDate = 2018-04-22

Tuple2 of String and Int:

scala> FastParseMagnoliaParseCaseClass.to[(String, Int)]("(   hello,4)")
res2: (String, Int) = ("   hello",4)

Or in the other order:

scala> val (i, s) = FastParseMagnoliaParseCaseClass.to[(Int, String)]("(4,   hello)")
i: Int = 4
s: String = "   hello"

The error messages are not very good:

scala> val dateTuple1 = FastParseMagnoliaParseCaseClass.to[Tuple1[LocalDate]]("2018-04-01")
java.lang.IllegalArgumentException: Expected: "(" at position: 0
  at com.github.aborg0.caseyclassy.FastParseParse.$anonfun$parse$1(FastParseMagnoliaParseCaseClass.scala:129)
  at com.github.aborg0.caseyclassy.FastParseParse.$anonfun$parse$1$adapted(FastParseMagnoliaParseCaseClass.scala:128)
  at fastparse.core.Parsed.fold(Parsing.scala:40)
  at fastparse.core.Parsed.fold$(Parsing.scala:38)
  at fastparse.core.Parsed$Failure.fold(Parsing.scala:74)
  at com.github.aborg0.caseyclassy.FastParseParse.parse(FastParseMagnoliaParseCaseClass.scala:129)
  at com.github.aborg0.caseyclassy.FastParseParse.parse$(FastParseMagnoliaParseCaseClass.scala:128)
  at com.github.aborg0.caseyclassy.FastParseMagnoliaParseCaseClass$$anonfun$combine$9.parse(FastParseMagnoliaParseCaseClass.scala:62)
  at com.github.aborg0.caseyclassy.FastParseMagnoliaParseCaseClass$.to(FastParseMagnoliaParseCaseClass.scala:95)
  ... 42 elided

Algebraic data types

With help of shapeless the following constructs are supported:

  • case classes
  • case objects
  • sealed hierarchies
  • tuples
  • a few Seq types

Case classes

scala> case class Example(a: Int, s: String)
defined class Example

scala> FastParseMagnoliaParseCaseClass.to[Example]("Example(-3, Hello)")
res3: Example = Example(-3, Hello)
scala> case object Dot
defined object Dot

scala> FastParseMagnoliaParseCaseClass.to[Dot.type]("Dot")
res4: Dot.type = Dot

Sealed hierarchies

Either

scala> FastParseMagnoliaParseCaseClass.to[Either[Short, Boolean]]("Left(-1111)")
res5: Either[Short,Boolean] = Left(-1111)

scala> FastParseMagnoliaParseCaseClass.to[Either[Short, Boolean]]("Right(false)")
res6: Either[Short,Boolean] = Right(false)

Option

scala> FastParseMagnoliaParseCaseClass.to[Option[Option[Int]]]("Some(None)")
res7: Option[Option[Int]] = Some(None)

scala> FastParseMagnoliaParseCaseClass.to[Option[Option[Int]]]("None")
res8: Option[Option[Int]] = None

scala> FastParseMagnoliaParseCaseClass.to[Option[Either[String, Seq[Boolean]]]]("Some(Right(List()))")
res9: Option[Either[String,Seq[Boolean]]] = Some(Right(ArrayBuffer()))

scala> FastParseMagnoliaParseCaseClass.to[Option[Either[String, Seq[Boolean]]]]("Some(Right(List(false, true)))")
res10: Option[Either[String,Seq[Boolean]]] = Some(Right(ArrayBuffer(false, true)))

Also removed requirement for `AnyRef` in type param.
Removed other implementations

ScalaTest 3.1.0
tut 0.6.12
Scala-js: 1.0.0
sbt-crossproject: 1.0.0
laika-sbt: 0.11.0
scoverage: 1.6.0
coveralls: 1.2.7
sbt-osgi: 0.9.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant