Code Monkey home page Code Monkey logo

Comments (4)

yanns avatar yanns commented on June 29, 2024

case d: BigDecimal does not handle java.math.BigDecimal?

from sangria.

majjhima avatar majjhima commented on June 29, 2024

They are different types. I used the following code to test this:

  private case class TimestampCoercionViolation(value: String, typeName: String) extends ValueCoercionViolation(
    s"Positive Long value expected, but got $value of type $typeName"
  )

  implicit val TimestampType: ScalarType[Long] = ScalarType[Long](
    "Timestamp",
    description = Some(
      "The `Timestamp` scalar type represents non-fractional unsigned whole numeric values."
    ),
    coerceOutput = valueOutput,
    coerceUserInput = {
      case i: Int if i >= 0 => Right(i: Long)
      case i: Long if i >= 0L => Right(i)
      case i: BigInt if !i.isValidLong => Left(BigLongCoercionViolation)
      case i: BigInt if i >= 0 => Right(i.longValue)
      case d: Double if d.isWhole && d >= 0.0 => Right(d.toLong)
      case d: BigDecimal if d.isValidLong && d >= 0.0 => Right(d.longValue)
      case d: java.math.BigDecimal if BigDecimal(d).isValidLong && d.longValue >= 0.0 => Right(d.longValue)
      case x => Left(TimestampCoercionViolation(x.toString, x.getClass.toString))
    },
    coerceInput = {
      case ast.IntValue(i, _, _) => Right(i: Long)
      case ast.BigIntValue(i, _, _) if !i.isValidLong => Left(BigLongCoercionViolation)
      case ast.BigIntValue(i, _, _) => Right(i.longValue)
      case x => Left(TimestampCoercionViolation(x.toString, x.getClass.toString))
    }
  )

Without the java.math.BigDecimal condition, I get the exception:

Positive Long value expected, but got 1680746052401 of type java.math.BigDecimal

from sangria.

yanns avatar yanns commented on June 29, 2024

OK. Would you have time/do you want to open a PR for that?

from sangria.

majjhima avatar majjhima commented on June 29, 2024

BTW, I found that in Scala 2.11, the class implicit conversion of java.math.BigDecimal to the Scala BigDecimal was moved to the object definition in this change replacing it with the following comment:

  // There was an implicit to cut down on the wrapper noise for BigDec -> BigDecimal.
  // However, this may mask introduction of surprising behavior (e.g. lack of rounding
  // where one might expect it).  Wrappers should be applied explicitly with an
  // eye to correctness.

We are using Scala 2.13.

Yes, I think I can submit a PR to address this.

from sangria.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.