Created
February 13, 2014 10:25
-
-
Save markhibberd/8972849 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Generated by Scrooge | |
* version: 3.12.0 | |
* rev: acdb2135b6b8ab36c09a93fb3d502295ba9a27b6 | |
* built at: 20140110-113906 | |
*/ | |
package com.example | |
import com.twitter.scrooge.{ | |
TFieldBlob, ThriftException, ThriftStruct, ThriftStructCodec3, ThriftUtil} | |
import org.apache.thrift.protocol._ | |
import org.apache.thrift.transport.{TMemoryBuffer, TTransport} | |
import java.nio.ByteBuffer | |
import java.util.Arrays | |
import scala.collection.immutable.{Map => immutable$Map} | |
import scala.collection.mutable.Builder | |
import scala.collection.mutable.{ | |
ArrayBuffer => mutable$ArrayBuffer, Buffer => mutable$Buffer, | |
HashMap => mutable$HashMap, HashSet => mutable$HashSet} | |
import scala.collection.{Map, Set} | |
object Enumish extends ThriftStructCodec3[Enumish] { | |
private val NoPassthroughFields = immutable$Map.empty[Short, TFieldBlob] | |
val Struct = new TStruct("Enumish") | |
val ValueField = new TField("value", TType.ENUM, 1) | |
val ValueFieldI32 = new TField("value", TType.I32, 1) | |
val ValueFieldManifest = implicitly[Manifest[SomethingOrOther]] | |
/** | |
* Checks that all required fields are non-null. | |
*/ | |
def validate(_item: Enumish) { | |
} | |
override def encode(_item: Enumish, _oproto: TProtocol) { | |
_item.write(_oproto) | |
} | |
override def decode(_iprot: TProtocol): Enumish = { | |
var value: SomethingOrOther = null | |
var _passthroughFields: Builder[(Short, TFieldBlob), immutable$Map[Short, TFieldBlob]] = null | |
var _done = false | |
_iprot.readStructBegin() | |
while (!_done) { | |
val _field = _iprot.readFieldBegin() | |
if (_field.`type` == TType.STOP) { | |
_done = true | |
} else { | |
var _readField = false | |
_field.id match { | |
case 1 => | |
_field.`type` match { | |
case TType.I32 | TType.ENUM => { | |
value = readValueValue(_iprot) | |
_readField = true | |
} | |
case _ => // skip | |
} | |
case _ => | |
if (_passthroughFields == null) | |
_passthroughFields = immutable$Map.newBuilder[Short, TFieldBlob] | |
_passthroughFields += (_field.id -> TFieldBlob.read(_field, _iprot)) | |
_readField = true | |
} | |
if (!_readField) { | |
TProtocolUtil.skip(_iprot, _field.`type`) | |
} | |
_iprot.readFieldEnd() | |
} | |
} | |
_iprot.readStructEnd() | |
new Immutable( | |
value, | |
if (_passthroughFields == null) | |
NoPassthroughFields | |
else | |
_passthroughFields.result() | |
) | |
} | |
def apply( | |
value: SomethingOrOther | |
): Enumish = | |
new Immutable( | |
value | |
) | |
def unapply(_item: Enumish): Option[SomethingOrOther] = Some(_item.value) | |
private def readValueValue(_iprot: TProtocol): SomethingOrOther = { | |
com.example.SomethingOrOther(_iprot.readI32()) | |
} | |
private def writeValueField(value_item: SomethingOrOther, _oprot: TProtocol) { | |
_oprot.writeFieldBegin(ValueFieldI32) | |
writeValueValue(value_item, _oprot) | |
_oprot.writeFieldEnd() | |
} | |
private def writeValueValue(value_item: SomethingOrOther, _oprot: TProtocol) { | |
_oprot.writeI32(value_item.value) | |
} | |
object Immutable extends ThriftStructCodec3[Enumish] { | |
override def encode(_item: Enumish, _oproto: TProtocol) { _item.write(_oproto) } | |
override def decode(_iprot: TProtocol): Enumish = Enumish.decode(_iprot) | |
} | |
/** | |
* The default read-only implementation of Enumish. You typically should not need to | |
* directly reference this class; instead, use the Enumish.apply method to construct | |
* new instances. | |
*/ | |
class Immutable( | |
val value: SomethingOrOther, | |
override val _passthroughFields: immutable$Map[Short, TFieldBlob] | |
) extends Enumish { | |
def this( | |
value: SomethingOrOther | |
) = this( | |
value, | |
Map.empty | |
) | |
} | |
/** | |
* This Proxy trait allows you to extend the Enumish trait with additional state or | |
* behavior and implement the read-only methods from Enumish using an underlying | |
* instance. | |
*/ | |
trait Proxy extends Enumish { | |
protected def _underlying_Enumish: Enumish | |
override def value: SomethingOrOther = _underlying_Enumish.value | |
override def _passthroughFields = _underlying_Enumish._passthroughFields | |
} | |
} | |
trait Enumish | |
extends ThriftStruct | |
with Product1[SomethingOrOther] | |
with java.io.Serializable | |
{ | |
import Enumish._ | |
def value: SomethingOrOther | |
def _passthroughFields: immutable$Map[Short, TFieldBlob] = immutable$Map.empty | |
def _1 = value | |
/** | |
* Gets a field value encoded as a binary blob using TCompactProtocol. If the specified field | |
* is present in the passthrough map, that value is returend. Otherwise, if the specified field | |
* is known and not optional and set to None, then the field is serialized and returned. | |
*/ | |
def getFieldBlob(_fieldId: Short): Option[TFieldBlob] = { | |
lazy val _buff = new TMemoryBuffer(32) | |
lazy val _oprot = new TCompactProtocol(_buff) | |
_passthroughFields.get(_fieldId) orElse { | |
val _fieldOpt: Option[TField] = | |
_fieldId match { | |
case 1 => | |
if (value ne null) { | |
writeValueValue(value, _oprot) | |
Some(Enumish.ValueField) | |
} else { | |
None | |
} | |
case _ => None | |
} | |
_fieldOpt match { | |
case Some(_field) => | |
val _data = Arrays.copyOfRange(_buff.getArray, 0, _buff.length) | |
Some(TFieldBlob(_field, _data)) | |
case None => | |
None | |
} | |
} | |
} | |
/** | |
* Collects TCompactProtocol-encoded field values according to `getFieldBlob` into a map. | |
*/ | |
def getFieldBlobs(ids: TraversableOnce[Short]): immutable$Map[Short, TFieldBlob] = | |
(ids flatMap { id => getFieldBlob(id) map { id -> _ } }).toMap | |
/** | |
* Sets a field using a TCompactProtocol-encoded binary blob. If the field is a known | |
* field, the blob is decoded and the field is set to the decoded value. If the field | |
* is unknown and passthrough fields are enabled, then the blob will be stored in | |
* _passthroughFields. | |
*/ | |
def setField(_blob: TFieldBlob): Enumish = { | |
var value: SomethingOrOther = this.value | |
var _passthroughFields = this._passthroughFields | |
_blob.id match { | |
case 1 => | |
value = readValueValue(_blob.read) | |
case _ => _passthroughFields += (_blob.id -> _blob) | |
} | |
new Immutable( | |
value, | |
_passthroughFields | |
) | |
} | |
/** | |
* If the specified field is optional, it is set to None. Otherwise, if the field is | |
* known, it is reverted to its default value; if the field is unknown, it is subtracked | |
* from the passthroughFields map, if present. | |
*/ | |
def unsetField(_fieldId: Short): Enumish = { | |
var value: SomethingOrOther = this.value | |
_fieldId match { | |
case 1 => | |
value = null | |
case _ => | |
} | |
new Immutable( | |
value, | |
_passthroughFields - _fieldId | |
) | |
} | |
/** | |
* If the specified field is optional, it is set to None. Otherwise, if the field is | |
* known, it is reverted to its default value; if the field is unknown, it is subtracked | |
* from the passthroughFields map, if present. | |
*/ | |
def unsetValue: Enumish = unsetField(1) | |
override def write(_oprot: TProtocol) { | |
Enumish.validate(this) | |
_oprot.writeStructBegin(Struct) | |
if (value ne null) writeValueField(value, _oprot) | |
_passthroughFields.values foreach { _.write(_oprot) } | |
_oprot.writeFieldStop() | |
_oprot.writeStructEnd() | |
} | |
def copy( | |
value: SomethingOrOther = this.value, | |
_passthroughFields: immutable$Map[Short, TFieldBlob] = this._passthroughFields | |
): Enumish = | |
new Immutable( | |
value, | |
_passthroughFields | |
) | |
override def canEqual(other: Any): Boolean = other.isInstanceOf[Enumish] | |
override def equals(other: Any): Boolean = | |
_root_.scala.runtime.ScalaRunTime._equals(this, other) && | |
_passthroughFields == other.asInstanceOf[Enumish]._passthroughFields | |
override def hashCode: Int = _root_.scala.runtime.ScalaRunTime._hashCode(this) | |
override def toString: String = _root_.scala.runtime.ScalaRunTime._toString(this) | |
override def productArity: Int = 1 | |
override def productElement(n: Int): Any = n match { | |
case 0 => value | |
case _ => throw new IndexOutOfBoundsException(n.toString) | |
} | |
override def productPrefix: String = "Enumish" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#@namespace scala com.example | |
enum SomethingOrOther { | |
SOME, OTHER | |
} | |
struct Enumish { | |
1: SomethingOrOther value | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Generated by Scrooge | |
* version: 3.12.0 | |
* rev: acdb2135b6b8ab36c09a93fb3d502295ba9a27b6 | |
* built at: 20140110-113906 | |
*/ | |
package com.example | |
import com.twitter.scrooge.ThriftEnum | |
@javax.annotation.Generated(value = Array("com.twitter.scrooge.Compiler"), date = "2014-02-13T21:07:58.778+1100") | |
case object SomethingOrOther { | |
case object Some extends com.example.SomethingOrOther { | |
val value = 0 | |
val name = "Some" | |
} | |
case object Other extends com.example.SomethingOrOther { | |
val value = 1 | |
val name = "Other" | |
} | |
/** | |
* Find the enum by its integer value, as defined in the Thrift IDL. | |
* @throws NoSuchElementException if the value is not found. | |
*/ | |
def apply(value: Int): com.example.SomethingOrOther = { | |
value match { | |
case 0 => com.example.SomethingOrOther.Some | |
case 1 => com.example.SomethingOrOther.Other | |
case _ => throw new NoSuchElementException(value.toString) | |
} | |
} | |
/** | |
* Find the enum by its integer value, as defined in the Thrift IDL. | |
* Returns None if the value is not found | |
*/ | |
def get(value: Int): Option[com.example.SomethingOrOther] = { | |
value match { | |
case 0 => scala.Some(com.example.SomethingOrOther.Some) | |
case 1 => scala.Some(com.example.SomethingOrOther.Other) | |
case _ => scala.None | |
} | |
} | |
def valueOf(name: String): Option[com.example.SomethingOrOther] = { | |
name.toLowerCase match { | |
case "some" => scala.Some(com.example.SomethingOrOther.Some) | |
case "other" => scala.Some(com.example.SomethingOrOther.Other) | |
case _ => scala.None | |
} | |
} | |
lazy val list: List[com.example.SomethingOrOther] = scala.List[com.example.SomethingOrOther]( | |
com.example.SomethingOrOther.Some, | |
com.example.SomethingOrOther.Other | |
) | |
} | |
@javax.annotation.Generated(value = Array("com.twitter.scrooge.Compiler"), date = "2014-02-13T21:07:58.778+1100") | |
sealed trait SomethingOrOther extends ThriftEnum with Serializable |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment