Skip to content

Commit 4cf263a

Browse files
author
Matt Landis
committed
add TwitterObjectHandler back to scala-extensions-2.10
1 parent 954666b commit 4cf263a

4 files changed

Lines changed: 61 additions & 3 deletions

File tree

scala-extensions/scala-extensions-2.10/build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ lazy val `scala-extensions-2-10` = project
66
libraryDependencies ++= Seq(
77
"com.github.spullara.mustache.java" % "compiler" % "0.8.17-SNAPSHOT",
88
"junit" % "junit" % "4.8.2" % "test",
9-
"com.twitter" % "util-core" % "6.12.1"
9+
"com.twitter" % "util-core" % "6.25.0"
1010
)
1111
)

scala-extensions/scala-extensions-2.10/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@
6262
</dependency>
6363
<dependency>
6464
<groupId>com.twitter</groupId>
65-
<artifactId>util-core</artifactId>
66-
<version>6.12.1</version>
65+
<artifactId>util-core_2.10</artifactId>
66+
<version>6.25.0</version>
6767
<scope>provided</scope>
6868
</dependency>
6969
</dependencies>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.twitter.mustache
2+
3+
import com.twitter.util.Future
4+
import java.util.concurrent.Callable
5+
6+
class TwitterObjectHandler extends ScalaObjectHandler {
7+
8+
override def coerce(value: Object) = {
9+
value match {
10+
case f: Future[_] => {
11+
new Callable[Any]() {
12+
def call() = {
13+
val value = f.get().asInstanceOf[Object]
14+
coerce(value)
15+
}
16+
}
17+
}
18+
case _ => super.coerce(value)
19+
}
20+
}
21+
}

scala-extensions/scala-extensions-2.10/src/test/scala/com/twitter/mustache/ObjectHandlerTest.scala

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,43 @@ class ObjectHandlerTest {
2121
Assert.assertEquals("fred", sw.toString())
2222
}
2323

24+
@Test
25+
def testTwitterHandler() {
26+
val pool = Executors.newCachedThreadPool()
27+
val futurePool = FuturePool(pool)
28+
val mf = new DefaultMustacheFactory()
29+
mf.setObjectHandler(new TwitterObjectHandler)
30+
mf.setExecutorService(pool)
31+
val m = mf.compile(
32+
new StringReader("{{#list}}{{optionalHello}}, {{futureWorld}}!" +
33+
"{{#test}}?{{/test}}{{^test}}!{{/test}}{{#num}}?{{/num}}{{^num}}!{{/num}}" +
34+
"{{#map}}{{value}}{{/map}}\n{{/list}}"),
35+
"helloworld"
36+
)
37+
val sw = new StringWriter
38+
val writer = m.execute(sw, new {
39+
val list = Seq(new {
40+
lazy val optionalHello = Some("Hello")
41+
val futureWorld = futurePool {
42+
"world"
43+
}
44+
val test = true
45+
val num = 0
46+
}, new {
47+
val optionalHello = Some("Goodbye")
48+
val futureWorld = futurePool {
49+
"thanks for all the fish"
50+
}
51+
lazy val test = Future { false }
52+
val map = Map(("value", "test"))
53+
val num = 1
54+
})
55+
})
56+
// You must use close if you use concurrent latched writers
57+
writer.close()
58+
Assert.assertEquals("Hello, world!?!\nGoodbye, thanks for all the fish!!?test\n", sw.toString)
59+
}
60+
2461
@Test
2562
def testScalaHandler() {
2663
val pool = Executors.newCachedThreadPool()

0 commit comments

Comments
 (0)