Skip to content

Instantly share code, notes, and snippets.

@icejoywoo
Last active April 26, 2019 16:32
Show Gist options
  • Save icejoywoo/d3ed024af70d405c542a6dcb010eb05e to your computer and use it in GitHub Desktop.
Save icejoywoo/d3ed024af70d405c542a6dcb010eb05e to your computer and use it in GitHub Desktop.

Revisions

  1. icejoywoo revised this gist Apr 26, 2019. 1 changed file with 11 additions and 4 deletions.
    15 changes: 11 additions & 4 deletions InterpolationDemo.scala
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ object InterpolationDemo {
    def indicator(args: Any*)(implicit default: ExprImplicit): Indicator = {
    val strings = sc.parts.iterator
    val expressions = args.iterator
    var basetime: String = null
    var basetime: Option[String] = None
    var key: String = null
    var value: Double = -1

    @@ -21,7 +21,7 @@ object InterpolationDemo {
    val v = expressions.next
    k match {
    case "basetime" =>
    basetime = v.toString
    basetime = Some(v.toString)
    case "key" =>
    key = v.toString
    case "value" =>
    @@ -49,16 +49,23 @@ object InterpolationDemo {
    println("error: " + expressions.next)
    }

    Indicator(default.basetime, key, value)
    if (basetime.isDefined) {
    Indicator(basetime.get, key, value)
    } else {
    Indicator(default.basetime, key, value)
    }
    }
    }

    def main(args: Array[String]): Unit = {
    val basetime = "2019042805"

    implicit val expr = ExprImplicit(basetime, Map("a" -> 50, "b" -> 2300))
    implicit val expr = ExprImplicit(basetime, Map("a" -> 50, "b" -> 2300, "c" -> 230))


    println((indicator"basetime: $basetime, key: ${"ratio_1"} , value: ${"a/b"}"))
    println((indicator"basetime: ${"2019043009"}, key: ${"ratio_2"} , value: ${"c/b"}"))
    println((indicator"key: ${"ratio_1"} , value: ${"a/b"}"))
    println((indicator"key: ${"ratio_1"} , value: ${"d/b"}"))
    }
    }
  2. icejoywoo created this gist Apr 26, 2019.
    64 changes: 64 additions & 0 deletions InterpolationDemo.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    import com.twitter.util.Eval

    object InterpolationDemo {

    case class Indicator(basetime: String, key: String, value: Double)

    case class ExprImplicit(basetime: String, data: Map[String, Double])

    implicit class IndicatorExpressionHelper(val sc: StringContext) extends AnyVal {
    def indicator(args: Any*)(implicit default: ExprImplicit): Indicator = {
    val strings = sc.parts.iterator
    val expressions = args.iterator
    var basetime: String = null
    var key: String = null
    var value: Double = -1

    val env = default.data.map{ case (k, v) => s"val $k = $v" }.mkString("\n")

    while (strings.hasNext && expressions.hasNext) {
    val k = strings.next.trim.stripPrefix(",").stripSuffix(":").trim
    val v = expressions.next
    k match {
    case "basetime" =>
    basetime = v.toString
    case "key" =>
    key = v.toString
    case "value" =>
    val eval = new Eval
    val expr = s"""$env
    |${v.toString}
    |""".stripMargin
    try {
    value = eval[Double](expr)
    } catch {
    case e: com.twitter.util.Eval.CompilerException =>
    println(s"key[$key] expr[${expr.split("\n").mkString("; ")}] exception[${e.getMessage}]")
    }

    case _ =>
    println(v)
    }
    }

    if (strings.nonEmpty && strings.next.nonEmpty) {
    println("error: " + strings.next)
    }

    if (expressions.nonEmpty) {
    println("error: " + expressions.next)
    }

    Indicator(default.basetime, key, value)
    }
    }

    def main(args: Array[String]): Unit = {
    val basetime = "2019042805"

    implicit val expr = ExprImplicit(basetime, Map("a" -> 50, "b" -> 2300))

    println((indicator"basetime: $basetime, key: ${"ratio_1"} , value: ${"a/b"}"))
    println((indicator"basetime: ${"2019043009"}, key: ${"ratio_2"} , value: ${"c/b"}"))
    }
    }