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

String input to constructor should be trimmed #383

Open
jehrlich opened this issue Feb 1, 2020 · 0 comments
Open

String input to constructor should be trimmed #383

jehrlich opened this issue Feb 1, 2020 · 0 comments
Labels

Comments

@jehrlich
Copy link

jehrlich commented Feb 1, 2020

Quantity constructors taking String input are not tolerant of extra whitespace at beginning or end, such that Volume("3 ml") parses correctly but Volume("3 ml ") fails. It seems this could be fixed by changing parseString(s) to parseString(s.trim) in Dimension.parse(value: Any).

I haven't tested further but probably all the parseTuple((v, u)) should be parseTuple((v, u.trim)) (or maybe the regex takes care of that).

/**
  * Tries to map a string or tuple value to Quantity of this Dimension
  * @param value the source string (ie, "10 kW") or tuple (ie, (10, "kW"))
  * @return Try[A]
  */
 protected def parse(value: Any): Try[A] = value match {
   case s: String              ⇒ parseString(s)
   case (v: Byte, u: String)   ⇒ parseTuple((v, u))
   case (v: Short, u: String)  ⇒ parseTuple((v, u))
   case (v: Int, u: String)    ⇒ parseTuple((v, u))
   case (v: Long, u: String)   ⇒ parseTuple((v, u))
   case (v: Float, u: String)  ⇒ parseTuple((v, u))
   case (v: Double, u: String) ⇒ parseTuple((v, u))
   case _ ⇒ Failure(QuantityParseException(s"Unable to parse $name", value.toString))
 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants