Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Thrift 0.19.0 compatibility #370

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,4 @@ trait LazyTProtocol extends TProtocol {
* Returns: The offset at which the string can be read.
*/
def offsetSkipBinary(): Int

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.apache.thrift.protocol._
object TLazyBinaryProtocol {
private val AnonymousStruct: TStruct = new TStruct()
private val utf8Charset = Charset.forName("UTF-8")
private val NEW_ENUM_TYPE_ID: Byte = -1
}

class TLazyBinaryProtocol(transport: TArrayByteTransport)
Expand All @@ -30,9 +31,10 @@ class TLazyBinaryProtocol(transport: TArrayByteTransport)
}

override def writeFieldBegin(field: TField): Unit = {
val typeToWrite = if (field.`type` == TType.ENUM) NEW_ENUM_TYPE_ID else field.`type`
val buf = transport.getBuffer(3)
val offset = transport.writerOffset
buf(offset) = field.`type`
buf(offset) = typeToWrite
innerWriteI16(buf, offset + 1, field.id)
}

Expand Down Expand Up @@ -176,7 +178,8 @@ class TLazyBinaryProtocol(transport: TArrayByteTransport)
override def readFieldBegin(): TField = {
val tpe: Byte = readByte()
val id: Short = if (tpe == TType.STOP) 0 else readI16()
new TField("", tpe, id)
val finalType = if (tpe == NEW_ENUM_TYPE_ID) TType.ENUM else tpe
new TField("", finalType, id)
}

override def readFieldEnd(): Unit = ()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.twitter.scrooge.internal

import com.twitter.scrooge.TArrayByteTransport
import com.twitter.scrooge.TFieldBlob
import com.twitter.scrooge.ThriftUnion
import com.twitter.scrooge.{TArrayByteTransport, TFieldBlob, TLazyBinaryProtocol, ThriftUnion}
import com.twitter.util.mock.Mockito
import org.apache.thrift.protocol.TBinaryProtocol
import org.apache.thrift.protocol.TCompactProtocol
import org.apache.thrift.protocol.TField
import org.apache.thrift.protocol.TProtocolException
import org.apache.thrift.protocol.TType
import org.apache.thrift.transport.TMemoryBuffer

import scala.collection.immutable
import org.scalatest.funsuite.AnyFunSuite

Expand Down Expand Up @@ -173,4 +172,44 @@ class TProtocolsTest extends AnyFunSuite with Mockito {
succeed
}

test("readFieldBegin handles new ENUM type identifier") {
val writeBuffer = new TMemoryBuffer(128)
val writeProto = new TBinaryProtocol(writeBuffer)

val newEnum = -1

writeProto.writeByte(newEnum.toByte) // New ENUM type identifier
writeProto.writeI16(1) // Field ID
writeProto.writeI32(2) // Enum value

val readBuffer = TArrayByteTransport(writeBuffer.getArray)
val readProto = new TLazyBinaryProtocol(readBuffer)

val field = readProto.readFieldBegin()
assert(field.`type` == TType.ENUM)
assert(field.id == 1)

val enumValue = readProto.readI32()
assert(enumValue == 2)
}

test("readFieldBegin handles old ENUM type identifier") {
val writeBuffer = new TMemoryBuffer(128)
val writeProto = new TBinaryProtocol(writeBuffer)

writeProto.writeByte(TType.ENUM)
writeProto.writeI16(1)
writeProto.writeI32(2)

val readBuffer = TArrayByteTransport(writeBuffer.getArray)
val readProto = new TLazyBinaryProtocol(readBuffer)

val field = readProto.readFieldBegin()
assert(field.`type` == TType.ENUM)
assert(field.id == 1)

val enumValue = readProto.readI32()
assert(enumValue == 2)
}

}