You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by jk...@apache.org on 2017/09/27 19:46:37 UTC

thrift git commit: THRIFT-4346: Allow go ZlibTransportFactory to wrap other factories Client: go

Repository: thrift
Updated Branches:
  refs/heads/master e8fbd8c3d -> 39310dad7


THRIFT-4346: Allow go ZlibTransportFactory to wrap other factories
Client: go

This closes #1375


Project: http://git-wip-us.apache.org/repos/asf/thrift/repo
Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/39310dad
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/39310dad
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/39310dad

Branch: refs/heads/master
Commit: 39310dad793ca69b4b7217a3b54430e682e5e2a4
Parents: e8fbd8c
Author: Yuri Khrustalev <yu...@gmail.com>
Authored: Mon Sep 25 23:22:33 2017 +0300
Committer: James E. King, III <jk...@apache.org>
Committed: Wed Sep 27 12:46:19 2017 -0700

----------------------------------------------------------------------
 lib/go/thrift/zlib_transport.go      | 19 +++++++++++++++++--
 lib/go/thrift/zlib_transport_test.go | 29 +++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/thrift/blob/39310dad/lib/go/thrift/zlib_transport.go
----------------------------------------------------------------------
diff --git a/lib/go/thrift/zlib_transport.go b/lib/go/thrift/zlib_transport.go
index 6f477ca..f2f0732 100644
--- a/lib/go/thrift/zlib_transport.go
+++ b/lib/go/thrift/zlib_transport.go
@@ -27,7 +27,8 @@ import (
 
 // TZlibTransportFactory is a factory for TZlibTransport instances
 type TZlibTransportFactory struct {
-	level int
+	level   int
+	factory TTransportFactory
 }
 
 // TZlibTransport is a TTransport implementation that makes use of zlib compression.
@@ -39,12 +40,26 @@ type TZlibTransport struct {
 
 // GetTransport constructs a new instance of NewTZlibTransport
 func (p *TZlibTransportFactory) GetTransport(trans TTransport) (TTransport, error) {
+	if p.factory != nil {
+		// wrap other factory
+		var err error
+		trans, err = p.factory.GetTransport(trans)
+		if err != nil {
+			return nil, err
+		}
+	}
 	return NewTZlibTransport(trans, p.level)
 }
 
 // NewTZlibTransportFactory constructs a new instance of NewTZlibTransportFactory
 func NewTZlibTransportFactory(level int) *TZlibTransportFactory {
-	return &TZlibTransportFactory{level: level}
+	return &TZlibTransportFactory{level: level, factory: nil}
+}
+
+// NewTZlibTransportFactory constructs a new instance of TZlibTransportFactory
+// as a wrapper over existing transport factory
+func NewTZlibTransportFactoryWithFactory(level int, factory TTransportFactory) *TZlibTransportFactory {
+	return &TZlibTransportFactory{level: level, factory: factory}
 }
 
 // NewTZlibTransport constructs a new instance of TZlibTransport

http://git-wip-us.apache.org/repos/asf/thrift/blob/39310dad/lib/go/thrift/zlib_transport_test.go
----------------------------------------------------------------------
diff --git a/lib/go/thrift/zlib_transport_test.go b/lib/go/thrift/zlib_transport_test.go
index f57610c..3c6f11e 100644
--- a/lib/go/thrift/zlib_transport_test.go
+++ b/lib/go/thrift/zlib_transport_test.go
@@ -31,3 +31,32 @@ func TestZlibTransport(t *testing.T) {
 	}
 	TransportTest(t, trans, trans)
 }
+
+type DummyTransportFactory struct{}
+
+func (p *DummyTransportFactory) GetTransport(trans TTransport) (TTransport, error) {
+	return NewTMemoryBuffer(), nil
+}
+
+func TestZlibFactoryTransportWithFactory(t *testing.T) {
+	factory := NewTZlibTransportFactoryWithFactory(
+		zlib.BestCompression,
+		&DummyTransportFactory{},
+	)
+	buffer := NewTMemoryBuffer()
+	trans, err := factory.GetTransport(buffer)
+	if err != nil {
+		t.Fatal(err)
+	}
+	TransportTest(t, trans, trans)
+}
+
+func TestZlibFactoryTransportWithoutFactory(t *testing.T) {
+	factory := NewTZlibTransportFactoryWithFactory(zlib.BestCompression, nil)
+	buffer := NewTMemoryBuffer()
+	trans, err := factory.GetTransport(buffer)
+	if err != nil {
+		t.Fatal(err)
+	}
+	TransportTest(t, trans, trans)
+}