Permalink
Browse files

scrooge: make scrooge-runtime 3 backwards compatible with scrooge-run…

…time 2

Via some method redirection and the introduction of ThriftStructCodec3 which extends ThriftStructCodec, scrooge-runtime 3 should now be drop-in compatible with scrooge-runtime 2 (I actually tested this in the birdcage with the tweetypie project).  That means with this change:
  - scrooge-runtime in the birdcage can be replaced with scrooge-runtime 3
  - all projects in the birdcage that currently use scrooge2 can use scrooge-runtime 3 without any changes (except pom)
  - scrooge 2 and scrooge 3 generated code can coexist in the same project without dependency conflicts
  - conversion from scrooge 2 to 3 can be done more safely on a project by project basis, rather than having to upgrade the entire birdcage

There are still some difficult cases, such as with value-add libraries like gizmoduck-twiggy, but the problem space is now smaller.

RB_ID=139582
  • Loading branch information...
1 parent e1e883c commit b43e7d12973d594779f144e2cf8d4951e0846d26 @jjmmcc jjmmcc committed Apr 16, 2013
View
1 scrooge-generator/src/main/resources/javagen/service.java
@@ -3,6 +3,7 @@
import com.twitter.scrooge.Option;
import com.twitter.scrooge.ThriftStruct;
import com.twitter.scrooge.ThriftStructCodec;
+import com.twitter.scrooge.ThriftStructCodec3;
import com.twitter.scrooge.Utilities;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
View
5 scrooge-generator/src/main/resources/javagen/struct.java
@@ -6,6 +6,7 @@
import com.twitter.scrooge.Utilities;
import com.twitter.scrooge.ThriftStruct;
import com.twitter.scrooge.ThriftStructCodec;
+import com.twitter.scrooge.ThriftStructCodec3;
import org.apache.thrift.protocol.*;
import java.nio.ByteBuffer;
import java.net.InetSocketAddress;
@@ -74,7 +75,8 @@ public Builder copy() {
return builder;
}
- public static ThriftStructCodec<{{StructName}}> CODEC = new ThriftStructCodec<{{StructName}}>() {
+ public static ThriftStructCodec<{{StructName}}> CODEC = new ThriftStructCodec3<{{StructName}}>() {
+ @Override
public {{StructName}} decode(TProtocol _iprot) throws org.apache.thrift.TException {
Builder builder = new Builder();
{{#fields}}
@@ -109,6 +111,7 @@ public Builder copy() {
}
}
+ @Override
public void encode({{StructName}} struct, TProtocol oprot) throws org.apache.thrift.TException {
struct.write(oprot);
}
View
3 scrooge-generator/src/main/resources/javagen/union.java
@@ -6,6 +6,7 @@
import com.twitter.scrooge.Utilities;
import com.twitter.scrooge.ThriftStruct;
import com.twitter.scrooge.ThriftStructCodec;
+import com.twitter.scrooge.ThriftStructCodec3;
import org.apache.thrift.protocol.*;
import java.nio.ByteBuffer;
import java.net.InetSocketAddress;
@@ -40,7 +41,7 @@
*/
public final Field setField;
- public static ThriftStructCodec<{{StructName}}> CODEC = new ThriftStructCodec<{{StructName}}>() {
+ public static ThriftStructCodec<{{StructName}}> CODEC = new ThriftStructCodec3<{{StructName}}>() {
public {{StructName}} decode(TProtocol _iprot) throws org.apache.thrift.TException {
{{StructName}} result = null;
_iprot.readStructBegin();
View
2 scrooge-generator/src/main/resources/scalagen/service.scala
@@ -1,6 +1,6 @@
package {{package}}
-import com.twitter.scrooge.{ThriftStruct, ThriftStructCodec}
+import com.twitter.scrooge.{ThriftStruct, ThriftStructCodec, ThriftStructCodec3}
import java.net.InetSocketAddress
import java.nio.ByteBuffer
import org.apache.thrift.protocol._
View
14 scrooge-generator/src/main/resources/scalagen/struct.scala
@@ -1,7 +1,7 @@
{{#public}}
package {{package}}
-import com.twitter.scrooge.{ThriftException, ThriftStruct, ThriftStructCodec}
+import com.twitter.scrooge.{ThriftException, ThriftStruct, ThriftStructCodec3}
import org.apache.thrift.protocol._
import java.nio.ByteBuffer
{{#withFinagleClient}}
@@ -12,7 +12,7 @@ import scala.collection.{Map, Set}
{{/public}}
{{docstring}}
-object {{StructName}} extends ThriftStructCodec[{{StructName}}] {
+object {{StructName}} extends ThriftStructCodec3[{{StructName}}] {
val Struct = new TStruct("{{StructNameForWire}}")
{{#fields}}
val {{fieldConst}} = new TField("{{fieldNameForWire}}", TType.{{constType}}, {{id}})
@@ -31,8 +31,8 @@ object {{StructName}} extends ThriftStructCodec[{{StructName}}] {
{{/fields}}
}
- def encode(_item: {{StructName}}, _oproto: TProtocol) { _item.write(_oproto) }
- def decode(_iprot: TProtocol) = Immutable.decode(_iprot)
+ 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)
@@ -56,9 +56,9 @@ object {{StructName}} extends ThriftStructCodec[{{StructName}}] {
def unapply(_item: {{StructName}}): Option[{{product}}] = Some(_item)
{{/arityN}}
- object Immutable extends ThriftStructCodec[{{StructName}}] {
- def encode(_item: {{StructName}}, _oproto: TProtocol) { _item.write(_oproto) }
- def decode(_iprot: TProtocol) = {
+ 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
View
8 scrooge-generator/src/main/resources/scalagen/union.scala
@@ -1,7 +1,7 @@
{{#public}}
package {{package}}
-import com.twitter.scrooge.{ThriftStruct, ThriftStructCodec}
+import com.twitter.scrooge.{ThriftStruct, ThriftStructCodec3}
import org.apache.thrift.protocol._
import java.nio.ByteBuffer
{{#withFinagleClient}}
@@ -16,14 +16,14 @@ sealed trait {{StructName}} extends {{parentType}}
{{docstring}}
@javax.annotation.Generated(value = Array("com.twitter.scrooge.Compiler"), date = "{{date}}")
-object {{StructName}} extends ThriftStructCodec[{{StructName}}] {
+object {{StructName}} extends ThriftStructCodec3[{{StructName}}] {
val Union = new TStruct("{{StructNameForWire}}")
{{#fields}}
val {{fieldConst}} = new TField("{{fieldNameForWire}}", TType.{{constType}}, {{id}})
{{/fields}}
- def encode(_item: {{StructName}}, _oprot: TProtocol) { _item.write(_oprot) }
- def decode(_iprot: TProtocol): {{StructName}} = {
+ override def encode(_item: {{StructName}}, _oprot: TProtocol) { _item.write(_oprot) }
+ override def decode(_iprot: TProtocol): {{StructName}} = {
var _result: {{StructName}} = null
_iprot.readStructBegin()
val _field = _iprot.readFieldBegin()
View
25 scrooge-runtime/src/main/scala/com/twitter/scrooge/ThriftStruct.scala
@@ -7,18 +7,33 @@ trait ThriftStruct {
def write(oprot: TProtocol)
}
-abstract class ThriftStructCodec[T <: ThriftStruct] {
+/**
+ * A trait encapsulating the logic for encoding and decoding a specific thrift struct
+ * type. The methods encoder and decoder are used by scrooge2 generated code, but are
+ * now deprecated. scrooge3 generated code uses encode and decode.
+ */
+trait ThriftStructCodec[T <: ThriftStruct] {
@throws(classOf[org.apache.thrift.TException])
- def encode(t: T, oprot: TProtocol)
+ def encode(t: T, oprot: TProtocol) = encoder(t, oprot)
@throws(classOf[org.apache.thrift.TException])
- def decode(iprot: TProtocol): T
+ def decode(iprot: TProtocol): T = decoder(iprot)
lazy val metaData = new ThriftStructMetaData(this)
@deprecated("your code is generated by an outdated version of scrooge-generator", "2.7.0")
- def encoder: (T, TProtocol) => Unit = encode _
+ def encoder: (T, TProtocol) => Unit
@deprecated("your code is generated by an outdated version of scrooge-generator", "2.7.0")
- def decoder: TProtocol => T = decode _
+ def decoder: TProtocol => T
}
+
+/**
+ * structs generated by scrooge3 will extends this base class, which provides implementations
+ * for the deprecated encoder/decoder methods that depend on encode and decode, which are
+ * generated.
+ */
+abstract class ThriftStructCodec3[T <: ThriftStruct] extends ThriftStructCodec[T] {
+ override val encoder: (T, TProtocol) => Unit = encode _
+ override val decoder: TProtocol => T = decode _
+}

0 comments on commit b43e7d1

Please sign in to comment.