Skip to content

Commit

Permalink
fully converted to mustache
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Cloud committed Aug 15, 2011
1 parent 6fee6f7 commit 10fba50
Show file tree
Hide file tree
Showing 34 changed files with 503 additions and 361 deletions.
3 changes: 1 addition & 2 deletions project/build/ScroogeProject.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class ScroogeProject(info: ProjectInfo) extends StandardServiceProject(info)
val finagleVersion = "1.8.2"
val utilVersion = "1.11.1"

val util_core = "com.twitter" % "util-core" % utilVersion
val util_eval = "com.twitter" % "util-eval" % utilVersion
val libThrift = "thrift" % "libthrift" % "0.5.0"
val cmdLine = "net.scala0" % "scala0.cmdline_2.8.1" % "1.1.1"
val mustache = "org.monkey" % "mustache" % "1.0.1-SNAPSHOT"
Expand All @@ -33,6 +31,7 @@ class ScroogeProject(info: ProjectInfo) extends StandardServiceProject(info)
val asm = "asm" % "asm" % "1.5.3" % "test"
val objenesis = "org.objenesis" % "objenesis" % "1.1" % "test"
val scroogeRuntime = "com.twitter" % "scrooge-runtime" % "1.0.0" % "test"
val util_eval = "com.twitter" % "util-eval" % utilVersion % "test"
val finagleOstrich4 = "com.twitter" % "finagle-ostrich4" % finagleVersion % "test"

override def mainClass = Some("com.twitter.scrooge.Main")
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/scalagen/consts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ object Constants {
val {{name}}: {{type}} = {{value}}
{{/constants}}
}
{{/hasConstants}}
{{/hasConstants}}
2 changes: 1 addition & 1 deletion src/main/resources/scalagen/enum.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ object {{enum}} {

abstract class {{enum}}(val value: Int) extends TEnum {
def getValue = value
}
}
15 changes: 15 additions & 0 deletions src/main/resources/scalagen/finagleClient.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// ----- finagle client

import com.twitter.finagle.{Service => FinagleService}
import com.twitter.finagle.thrift.ThriftClientRequest
import com.twitter.scrooge.FinagleThriftClient

class FinagledClient(
val service: FinagleService[ThriftClientRequest, Array[Byte]],
val protocolFactory: TProtocolFactory)
extends FinagleThriftClient with FutureIface
{
{{#functions}}
{{function}}
{{/function}}
}
7 changes: 7 additions & 0 deletions src/main/resources/scalagen/finagleClientFunction.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{functionDecl}} = {
encodeRequest("{{name}}", {{name}}_args({{argNames}})) flatMap { this.service } flatMap {
decodeResponse(_, {{name}}_result.decoder)
} flatMap { result =>
{{resultUnwrapper}}
}
}
13 changes: 13 additions & 0 deletions src/main/resources/scalagen/finagleService.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// ----- finagle service

import com.twitter.scrooge.FinagleThriftService

class FinagledService(
iface: FutureIface,
val protocolFactory: TProtocolFactory)
extends FinagleThriftService
{
{{#functions}}
{{function}}
{{/function}}
}
26 changes: 26 additions & 0 deletions src/main/resources/scalagen/finagleServiceFunction.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
functionMap("{{name}}") = { (iprot: TProtocol, seqid: Int) =>
try {
val args = {{name}}_args.decoder(iprot)
iprot.readMessageEnd()
(try {
iface.{{name}}({{argNames}})
} catch {
case e: Exception => Future.exception(e)
}) flatMap { value: {{scalaType}} =>
reply("{{name}}", seqid, {{name}}_result({{resultNamedArg}}))
} rescue {
{{#exceptions}}
case e: {{exceptionType}} =>
reply("{{name}}", seqid, {{name}}_result({{fieldName}} = Some(e)))
{{/exception}}
case e: Throwable =>
exception("{{name}}", seqid, TApplicationException.INTERNAL_ERROR, "Internal error processing {{name}}")
}
} catch {
case e: TProtocolException =>
iprot.readMessageEnd()
exception("{{name}}", seqid, TApplicationException.PROTOCOL_ERROR, e.getMessage)
case e: Exception =>
Future.exception(e)
}
}
3 changes: 3 additions & 0 deletions src/main/resources/scalagen/function.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{#throws}}
@throws(classOf[{{scalaType}}])
{{/throws}}def {{name}}({{fieldArgs}}): {{scalaType}}
2 changes: 1 addition & 1 deletion src/main/resources/scalagen/header.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ import org.apache.thrift.protocol._
import org.apache.thrift.transport.{TMemoryInputTransport, TMemoryBuffer}
{{#imports}}
import {{namespace}}._
{{/imports}}
{{/imports}}
32 changes: 32 additions & 0 deletions src/main/resources/scalagen/ostrichService.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// ----- ostrich service

import com.twitter.finagle.builder.{Server, ServerBuilder}
import com.twitter.finagle.stats.OstrichStatsReceiver
import com.twitter.finagle.thrift.ThriftServerFramedCodec
import com.twitter.logging.Logger
import com.twitter.ostrich.admin.Service

trait ThriftServer extends Service with FutureIface {
val log = Logger.get(getClass)

def thriftCodec = ThriftServerFramedCodec()
val thriftProtocolFactory = new TBinaryProtocol.Factory()
val thriftPort: Int
val serverName: String

var server: Server = null

def start() {
val thriftImpl = new FinagledService(this, thriftProtocolFactory)
val serverAddr = new InetSocketAddress(thriftPort)
server = ServerBuilder().codec(thriftCodec).name(serverName).reportTo(new OstrichStatsReceiver).bindTo(serverAddr).build(thriftImpl)
}

def shutdown() {
synchronized {
if (server != null) {
server.close(0.seconds)
}
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/scalagen/readBasic.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_iprot.{{protocolReadMethod}}()
1 change: 1 addition & 0 deletions src/main/resources/scalagen/readEnum.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{name}}(_iprot.readI32())
11 changes: 11 additions & 0 deletions src/main/resources/scalagen/readField.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
case {{id}} => { /* {{name}} */
_field.`type` match {
case TType.{{constType}} => {
{{name}} = {{optionality}}{
{{valueReader}}
}
{{#required}}_got_{{name}} = true{{/required}}
}
case _ => TProtocolUtil.skip(_iprot, _field.`type`)
}
}
11 changes: 11 additions & 0 deletions src/main/resources/scalagen/readList.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
val _list = _iprot.readListBegin()
val _rv = new mutable.ArrayBuffer[{{eltType}}](_list.size)
var _i = 0
while (_i < _list.size) {
_rv += {
{{eltReader}}
}
_i += 1
}
_iprot.readListEnd()
_rv.toSeq
15 changes: 15 additions & 0 deletions src/main/resources/scalagen/readMap.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
val _map = _iprot.readMapBegin()
val _rv = new mutable.HashMap[{{keyType}}, {{valueType}}]
var _i = 0
while (_i < _map.size) {
val _key = {
{{keyReader}}
}
val _value = {
{{valueReader}}
}
_rv(_key) = _value
_i += 1
}
_iprot.readMapEnd()
_rv
11 changes: 11 additions & 0 deletions src/main/resources/scalagen/readSet.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
val _set = _iprot.readSetBegin()
val _rv = new mutable.HashSet[{{eltType}}]
var _i = 0
while (_i < _set.size) {
_rv += {
{{eltReader}}
}
_i += 1
}
_iprot.readSetEnd()
_rv
1 change: 1 addition & 0 deletions src/main/resources/scalagen/readStruct.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{name}}.decoder(_iprot)
21 changes: 21 additions & 0 deletions src/main/resources/scalagen/service.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
object {{name}} {
trait Iface {{extends}}{
{{#syncFunctions}}
{{> function}}
{{/syncFunctions}}
}

trait FutureIface {{extends}}{
{{#asyncFunctions}}
{{> function}}
{{/asyncFunctions}}
}

{{#functionStructs}}
{{> struct}}
{{/functionStructs}}

{{finagleClient}}
{{finagleService}}
{{ostrichServer}}
}
55 changes: 55 additions & 0 deletions src/main/resources/scalagen/struct.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
object {{name}} {
private val STRUCT_DESC = new TStruct("{{name}}")
{{#fields}}
private val {{fieldConst}} = new TField("{{name}}", TType.{{constType}}, {{id}})
{{/fields}}

object decoder extends (TProtocol => {{name}}) {
override def apply(_iprot: TProtocol) = {
var _field: TField = null
{{#fields}}
var {{name}}: {{scalaType}} = {{defaultValue}}
{{#required}}var _got_{{name}} = false{{/required}}
{{/fields}}
var _done = false
_iprot.readStructBegin()
while (!_done) {
_field = _iprot.readFieldBegin
if (_field.`type` == TType.STOP) {
_done = true
} else {
_field.id match {
{{#fields}}
{{reader}}
{{/fields}}
case _ => TProtocolUtil.skip(_iprot, _field.`type`)
}
_iprot.readFieldEnd()
}
}
_iprot.readStructEnd()
{{#fields}}
{{#required}}
if (!_got_{{name}}) throw new TProtocolException("Required field '{{name}}' was not found in serialized data for struct {{struct}}")
{{/required}}
{{/fields}}
{{name}}({{fieldNames}})
}
}
}

case class {{name}}({{fieldArgs}}) extends {{parentType}} {
import {{name}}._

override def write(_oprot: TProtocol) {
validate()
_oprot.writeStructBegin(STRUCT_DESC)
{{#fields}}
{{writer}}
{{/fields}}
_oprot.writeFieldStop()
_oprot.writeStructEnd()
}

def validate() = true //TODO: Implement this
}
1 change: 1 addition & 0 deletions src/main/resources/scalagen/writeBasic.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_oprot.{{protocolWriteMethod}}(_item)
1 change: 1 addition & 0 deletions src/main/resources/scalagen/writeEnum.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_oprot.writeI32(_item.value)
6 changes: 6 additions & 0 deletions src/main/resources/scalagen/writeField.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
if ({{conditional}}) {
val _item = {{name}}{{getter}}
_oprot.writeFieldBegin({{fieldConst}})
{{valueWriter}}
_oprot.writeFieldEnd()
}
5 changes: 5 additions & 0 deletions src/main/resources/scalagen/writeList.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
_oprot.writeListBegin(new TList(TType.{{eltType}}, _item.size))
_item.foreach { _item =>
{{eltWriter}}
}
_oprot.writeListEnd()
12 changes: 12 additions & 0 deletions src/main/resources/scalagen/writeMap.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
_oprot.writeMapBegin(new TMap(TType.{{keyType}}, TType.{{valueType}}, _item.size))
_item.foreach { case (_key, _value) =>
{
val _item = _key
{{keyWriter}}
}
{
val _item = _value
{{valueWriter}}
}
}
_oprot.writeMapEnd()
5 changes: 5 additions & 0 deletions src/main/resources/scalagen/writeSet.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
_oprot.writeSetBegin(new TSet(TType.{{eltType}}, _item.size))
_item.foreach { _item =>
{{eltWriter}}
}
_oprot.writeSetEnd()
1 change: 1 addition & 0 deletions src/main/resources/scalagen/writeStruct.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_item.write(_oprot)
14 changes: 12 additions & 2 deletions src/main/scala/com/twitter/scrooge/Handlebar.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package com.twitter.scrooge

import java.io.Writer
import org.monkey.mustache._

class Handlebar[T](val mustache: Mustache, val f: T => Dictionary) extends Eval {
trait Handlebar[T] extends (T => String) {
}

class HandlebarMustache[T](val mustache: Mustache, val mkDictionary: T => Dictionary)
extends Eval with Handlebar[T]
{
def apply(item: T): String = {
mustache(f(item), this)
mustache(mkDictionary(item), this)
}

def apply(item: T, out: Writer) {
mustache(mkDictionary(item), this, out)
}

/** don't html escape strings */
Expand Down
Loading

0 comments on commit 10fba50

Please sign in to comment.