Advertisement
mitrakov

Contextual JSON parsing

Feb 3rd, 2020
763
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.08 KB | None | 0 0
  1. import com.elama.bidmanager.settings.Step
  2. import io.circe.{HCursor, Json}
  3. import scala.annotation.tailrec
  4.  
  5. object AstToSettingsUtils extends App {
  6.   /** Extracts maxBidMaxPriceLimit from AST */
  7.   def extractLimit(json: Json): Option[Int] = {
  8.     @tailrec
  9.     def f(json: Json, parent: String = "", result: Option[Int] = None): (Json, Option[Int]) = {
  10.       val cursor = json.hcursor
  11.       cursor.downField("kind").as[String] match {
  12.         case Right("If")       => f(cursor.downField("body").focus.get)
  13.         case Right("SetBid")   => f(cursor.downField("bid").focus.get)
  14.         case Right(x@"Min")    => f(cursor.downField("right").focus.get, x)
  15.         case Right("Constant") => Json.Null -> Some(toPercent(cursor))
  16.         case Right("Multiply") if parent == "Min" => f(cursor.downField("right").focus.get)
  17.         case _ => cursor.focus.get -> result
  18.       }
  19.     }
  20.  
  21.     f(json)._2
  22.   }
  23.  
  24.   /** Extracts Step value and Step Type (amount/percent) from AST */
  25.   def extractStep(json: Json): Option[Step] = {
  26.     @tailrec
  27.     def f(json: Json, parent: String = "", result: Option[Step] = None): (Json, Option[Step]) = {
  28.       val cursor = json.hcursor
  29.       cursor.downField("kind").as[String] match {
  30.         case Right("If")         => f(cursor.downField("body").focus.get)
  31.         case Right("SetBid")     => f(cursor.downField("bid").focus.get)
  32.         case Right("Min")        => f(cursor.downField("left").focus.get)
  33.         case Right(x@"Add")      => f(cursor.downField("right").focus.get, x)
  34.         case Right(x@"Multiply") => f(cursor.downField("right").focus.get, x)
  35.         case Right("Constant") if parent == "Multiply" => (Json.Null, Some(Step.Percent(toPercent(cursor))))
  36.         case Right("Constant") if parent == "Add"      => (Json.Null, Some(Step.Amount(toAmount(cursor))))
  37.         case _ => (cursor.focus.get, result)
  38.       }
  39.     }
  40.  
  41.     f(json)._2
  42.   }
  43.  
  44.   private def toPercent(c: HCursor): Int = (c.downField("value").as[Double].right.get * 100).toInt - 100
  45.   private def toAmount(c: HCursor): Double = c.downField("value").as[Double].right.get / 1000000
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement