
scalagen.struct.scala Maven / Gradle / Ivy
The newest version!
{{#public}}
package {{package}}
import com.twitter.scrooge.{ThriftException, ThriftStruct, ThriftStructCodec3}
import org.apache.thrift.protocol._
import java.nio.ByteBuffer
{{#withFinagleClient}}
import com.twitter.finagle.SourcedException
{{/withFinagleClient}}
import scala.collection.mutable
import scala.collection.{Map, Set}
{{/public}}
{{docstring}}
object {{StructName}} extends ThriftStructCodec3[{{StructName}}] {
val Struct = new TStruct("{{StructNameForWire}}")
{{#fields}}
val {{fieldConst}} = new TField("{{fieldNameForWire}}", TType.{{constType}}, {{id}})
{{/fields}}
/**
* Checks that all required fields are non-null.
*/
def validate(_item: {{StructName}}) {
{{#fields}}
{{#required}}
{{#nullable}}
if (_item.{{fieldName}} == null) throw new TProtocolException("Required field {{fieldName}} cannot be null")
{{/nullable}}
{{/required}}
{{/fields}}
}
override def encode(_item: {{StructName}}, _oproto: TProtocol) { _item.write(_oproto) }
override def decode(_iprot: TProtocol) = Immutable.decode(_iprot)
def apply(_iprot: TProtocol): {{StructName}} = decode(_iprot)
def apply(
{{#fields}}
{{fieldName}}: {{>optionalType}}{{#hasDefaultValue}} = {{defaultFieldValue}}{{/hasDefaultValue}}{{#optional}} = None{{/optional}}
{{/fields|,}}
): {{StructName}} = new Immutable(
{{#fields}}
{{fieldName}}
{{/fields|,}}
)
{{#arity0}}
def unapply(_item: {{StructName}}): Boolean = true
{{/arity0}}
{{#arity1}}
def unapply(_item: {{StructName}}): Option[{{>optionalType}}] = Some(_item.{{fieldName}})
{{/arity1}}
{{#arityN}}
def unapply(_item: {{StructName}}): Option[{{product}}] = Some(_item)
{{/arityN}}
object Immutable extends ThriftStructCodec3[{{StructName}}] {
override def encode(_item: {{StructName}}, _oproto: TProtocol) { _item.write(_oproto) }
override def decode(_iprot: TProtocol) = {
{{#fields}}
var {{fieldName}}: {{fieldType}} = {{defaultReadValue}}
var {{gotName}} = false
{{/fields}}
var _done = false
_iprot.readStructBegin()
while (!_done) {
val _field = _iprot.readFieldBegin()
if (_field.`type` == TType.STOP) {
_done = true
} else {
_field.id match {
{{#fields}}
{{#readWriteInfo}}
{{>readField}}
{{/readWriteInfo}}
{{/fields}}
case _ => TProtocolUtil.skip(_iprot, _field.`type`)
}
_iprot.readFieldEnd()
}
}
_iprot.readStructEnd()
{{#fields}}
{{#required}}
if (!{{gotName}}) throw new TProtocolException("Required field '{{StructNameForWire}}' was not found in serialized data for struct {{StructName}}")
{{/required}}
{{/fields}}
new Immutable(
{{#fields}}
{{#optional}}
if ({{gotName}}) Some({{fieldName}}) else None
{{/optional}}
{{^optional}}
{{fieldName}}
{{/optional}}
{{/fields|,}}
)
}
}
/**
* The default read-only implementation of {{StructName}}. You typically should not need to
* directly reference this class; instead, use the {{StructName}}.apply method to construct
* new instances.
*/
class Immutable(
{{#fields}}
val {{fieldName}}: {{>optionalType}}{{#hasDefaultValue}} = {{defaultFieldValue}}{{/hasDefaultValue}}{{#optional}} = None{{/optional}}
{{/fields|,}}
) extends {{StructName}}
{{#withProxy}}
/**
* This Proxy trait allows you to extend the {{StructName}} trait with additional state or
* behavior and implement the read-only methods from {{StructName}} using an underlying
* instance.
*/
trait Proxy extends {{StructName}} {
protected def {{underlyingStructName}}: {{StructName}}
{{#fields}}
def {{fieldName}}: {{>optionalType}} = {{underlyingStructName}}.{{fieldName}}
{{/fields}}
}
{{/withProxy}}
}
trait {{StructName}} extends {{parentType}}
with {{product}}
with java.io.Serializable
{
import {{StructName}}._
{{#fields}}
def {{fieldName}}: {{>optionalType}}
{{/fields}}
{{#fields}}
def _{{indexP1}} = {{fieldName}}
{{/fields}}
override def write(_oprot: TProtocol) {
{{StructName}}.validate(this)
_oprot.writeStructBegin(Struct)
{{#fields}}
{{#readWriteInfo}}
{{>writeField}}
{{/readWriteInfo}}
{{/fields}}
_oprot.writeFieldStop()
_oprot.writeStructEnd()
}
def copy(
{{#fields}}
{{fieldName}}: {{>optionalType}} = this.{{fieldName}}
{{/fields|, }}
): {{StructName}} = new Immutable(
{{#fields}}
{{fieldName}}
{{/fields|, }}
)
override def canEqual(other: Any): Boolean = other.isInstanceOf[{{StructName}}]
override def equals(other: Any): Boolean = runtime.ScalaRunTime._equals(this, other)
override def hashCode: Int = runtime.ScalaRunTime._hashCode(this)
override def toString: String = runtime.ScalaRunTime._toString(this)
{{#hasExceptionMessage}}
override def getMessage: String = String.valueOf({{exceptionMessageField}})
{{/hasExceptionMessage}}
override def productArity: Int = {{arity}}
override def productElement(n: Int): Any = n match {
{{#fields}}
case {{index}} => {{fieldName}}
{{/fields}}
case _ => throw new IndexOutOfBoundsException(n.toString)
}
override def productPrefix: String = "{{StructName}}"
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy