All Downloads are FREE. Search and download functionalities are using the official Maven repository.

lucuma.itc.input.odb.DecimalInput.scala Maven / Gradle / Ivy

There is a newer version: 0.22.3
Show newest version
// Copyright (c) 2016-2023 Association of Universities for Research in Astronomy, Inc. (AURA)
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause

package lucuma.odb.graphql
package input

import cats.syntax.all.*
import grackle.Result
import lucuma.odb.graphql.binding.*

/**
 * Constructor for matchers of the pattern:
 *
 * input FooDecimalnput { # integral value in associated units value: BigDecimal!
 *
 * # units for associated value units: FooUnits! }
 */
object DecimalInput {

  def apply[A](
    name: String
  )(handler: PartialFunction[(BigDecimal, String), Result[A]]): Matcher[A] =
    ObjectFieldsBinding.rmap {
      case List(
            BigDecimalBinding("value", rValue),
            EnumBinding("units", rEnum)
          ) =>
        (rValue, rEnum).parTupled.flatMap { case (value, label) =>
          handler.lift((value, label)) match {
            case Some(r) => r
            case None    => Result.failure(s"Unexpected ${name}Units value: $label")
          }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy