From e52c046cb62d1f910302148f7bc2091c014acd27 Mon Sep 17 00:00:00 2001 From: Jens Geyer Date: Fri, 2 May 2014 23:37:39 +0200 Subject: [PATCH] THRIFT-2491 unable to import generated ThriftTest service Client: Go Patch: Aleksey Pesternikov This closes #105 commit f2e7186ca8d63f407dba0c56ee51afd6405926ba Author: Aleksey Pesternikov Date: 2014-04-22T12:48:14Z add _ to generated filename if it ends with _test.go --- compiler/cpp/src/generate/t_generator.h | 2 +- compiler/cpp/src/generate/t_go_generator.cc | 17 ++++++++---- lib/go/test/Makefile.am | 10 +++---- lib/go/test/NamespacedTest.thrift | 2 +- lib/go/test/tests/binary_key_test.go | 4 +-- .../test/tests/multiplexed_protocol_test.go | 20 +++++++------- lib/go/test/tests/one_way_test.go | 8 +++--- lib/go/test/tests/optional_fields_test.go | 26 +++++++++---------- test/ThriftTest.thrift | 2 +- 9 files changed, 49 insertions(+), 42 deletions(-) diff --git a/compiler/cpp/src/generate/t_generator.h b/compiler/cpp/src/generate/t_generator.h index d5cf8352..d1317774 100644 --- a/compiler/cpp/src/generate/t_generator.h +++ b/compiler/cpp/src/generate/t_generator.h @@ -193,7 +193,7 @@ class t_generator { in[0] = tolower(in[0]); return in; } - std::string lowercase(std::string in) { + static std::string lowercase(std::string in) { for (size_t i = 0; i < in.size(); ++i) { in[i] = tolower(in[i]); } diff --git a/compiler/cpp/src/generate/t_go_generator.cc b/compiler/cpp/src/generate/t_go_generator.cc index 4893c71e..6f273f10 100644 --- a/compiler/cpp/src/generate/t_go_generator.cc +++ b/compiler/cpp/src/generate/t_go_generator.cc @@ -254,12 +254,11 @@ public: return package_flag; } std::string real_module = program->get_namespace("go"); - - if (real_module.empty()) { - return program->get_name(); + if (!real_module.empty()) { + return real_module; } - return real_module; + return lowercase(program->get_name()); } private: @@ -1510,7 +1509,15 @@ void t_go_generator::generate_go_struct_writer(ofstream& out, */ void t_go_generator::generate_service(t_service* tservice) { - string f_service_name = package_dir_ + "/" + underscore(service_name_) + ".go"; + string test_suffix("_test"); + string filename = lowercase(service_name_); + string f_service_name; + if (filename.compare(filename.length() - test_suffix.length(), + test_suffix.length(), test_suffix) == 0) { + f_service_name = package_dir_ + "/" + filename + "_.go"; + } else { + f_service_name = package_dir_ + "/" + filename + ".go"; + } f_service_.open(f_service_name.c_str()); f_service_ << go_autogen_comment() << diff --git a/lib/go/test/Makefile.am b/lib/go/test/Makefile.am index 5499fb7a..0be6cf7f 100644 --- a/lib/go/test/Makefile.am +++ b/lib/go/test/Makefile.am @@ -47,11 +47,11 @@ gopath: $(top_srcdir)/compiler/cpp/thrift $(THRIFTTEST) \ check: gopath GOPATH=`pwd`/gopath $(GO) build \ - IncludesTest \ - BinaryKeyTest \ - ServicesTest \ - TypedefFieldTest \ - RefAnnotationFieldsTest + includestest \ + binarykeytest \ + servicestest \ + typedeffieldtest \ + refannotationfieldstest GOPATH=`pwd`/gopath $(GO) test thrift tests clean-local: diff --git a/lib/go/test/NamespacedTest.thrift b/lib/go/test/NamespacedTest.thrift index 1bb2fc41..a9103506 100644 --- a/lib/go/test/NamespacedTest.thrift +++ b/lib/go/test/NamespacedTest.thrift @@ -19,7 +19,7 @@ include "ThriftTest.thrift" -namespace go lib.go.test.NamespacedTest +namespace go lib.go.test.namespacedtest enum Stuff { ONE = 1, diff --git a/lib/go/test/tests/binary_key_test.go b/lib/go/test/tests/binary_key_test.go index 4cd3eb44..aa961939 100644 --- a/lib/go/test/tests/binary_key_test.go +++ b/lib/go/test/tests/binary_key_test.go @@ -20,12 +20,12 @@ package tests import ( - "BinaryKeyTest" + "binarykeytest" "testing" ) func TestBinaryMapKeyGeneratesString(t *testing.T) { - s := BinaryKeyTest.NewTestStruct() + s := binarykeytest.NewTestStruct() //This will only compile if BinToString has type of map[string]string s.BinToString = make(map[string]string) } diff --git a/lib/go/test/tests/multiplexed_protocol_test.go b/lib/go/test/tests/multiplexed_protocol_test.go index 5b8a3287..00669ef7 100644 --- a/lib/go/test/tests/multiplexed_protocol_test.go +++ b/lib/go/test/tests/multiplexed_protocol_test.go @@ -20,7 +20,7 @@ package tests import ( - "MultiplexedProtocolTest" + "multiplexedprotocoltest" "net" "testing" "thrift" @@ -61,37 +61,37 @@ func TestInitTwoServers(t *testing.T) { } server = thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory) - firstProcessor := MultiplexedProtocolTest.NewFirstProcessor(&FirstImpl{}) + firstProcessor := multiplexedprotocoltest.NewFirstProcessor(&FirstImpl{}) processor.RegisterProcessor("FirstService", firstProcessor) - secondProcessor := MultiplexedProtocolTest.NewSecondProcessor(&SecondImpl{}) + secondProcessor := multiplexedprotocoltest.NewSecondProcessor(&SecondImpl{}) processor.RegisterProcessor("SecondService", secondProcessor) go server.Serve() } -var firstClient *MultiplexedProtocolTest.FirstClient +var firstClient *multiplexedprotocoltest.FirstClient func TestInitClient1(t *testing.T) { socket := thrift.NewTSocketFromAddrTimeout(addr, TIMEOUT) transport := thrift.NewTFramedTransport(socket) var protocol thrift.TProtocol = thrift.NewTBinaryProtocolTransport(transport) protocol = thrift.NewTMultiplexedProtocol(protocol, "FirstService") - firstClient = MultiplexedProtocolTest.NewFirstClientProtocol(transport, protocol, protocol) + firstClient = multiplexedprotocoltest.NewFirstClientProtocol(transport, protocol, protocol) err := transport.Open() if err != nil { t.Fatal("Unable to open client socket", err) } } -var secondClient *MultiplexedProtocolTest.SecondClient +var secondClient *multiplexedprotocoltest.SecondClient func TestInitClient2(t *testing.T) { socket := thrift.NewTSocketFromAddrTimeout(addr, TIMEOUT) transport := thrift.NewTFramedTransport(socket) var protocol thrift.TProtocol = thrift.NewTBinaryProtocolTransport(transport) protocol = thrift.NewTMultiplexedProtocol(protocol, "SecondService") - secondClient = MultiplexedProtocolTest.NewSecondClientProtocol(transport, protocol, protocol) + secondClient = multiplexedprotocoltest.NewSecondClientProtocol(transport, protocol, protocol) err := transport.Open() if err != nil { t.Fatal("Unable to open client socket", err) @@ -99,11 +99,11 @@ func TestInitClient2(t *testing.T) { } //create client without service prefix -func createLegacyClient(t *testing.T) *MultiplexedProtocolTest.SecondClient { +func createLegacyClient(t *testing.T) *multiplexedprotocoltest.SecondClient { socket := thrift.NewTSocketFromAddrTimeout(addr, TIMEOUT) transport := thrift.NewTFramedTransport(socket) var protocol thrift.TProtocol = thrift.NewTBinaryProtocolTransport(transport) - legacyClient := MultiplexedProtocolTest.NewSecondClientProtocol(transport, protocol, protocol) + legacyClient := multiplexedprotocoltest.NewSecondClientProtocol(transport, protocol, protocol) err := transport.Open() if err != nil { t.Fatal("Unable to open client socket", err) @@ -139,7 +139,7 @@ func TestCallLegacy(t *testing.T) { t.Fatal("Expecting error") } //register default processor and call again - processor.RegisterDefault(MultiplexedProtocolTest.NewSecondProcessor(&SecondImpl{})) + processor.RegisterDefault(multiplexedprotocoltest.NewSecondProcessor(&SecondImpl{})) legacyClient = createLegacyClient(t) ret, err = legacyClient.ReturnTwo() if err != nil { diff --git a/lib/go/test/tests/one_way_test.go b/lib/go/test/tests/one_way_test.go index 5ffbbfe4..3ff025f5 100644 --- a/lib/go/test/tests/one_way_test.go +++ b/lib/go/test/tests/one_way_test.go @@ -20,9 +20,9 @@ package tests import ( - "OnewayTest" "fmt" "net" + "onewaytest" "testing" "thrift" "time" @@ -47,7 +47,7 @@ const TIMEOUT = time.Second var addr net.Addr var server *thrift.TSimpleServer -var client *OnewayTest.OneWayClient +var client *onewaytest.OneWayClient func TestInitOneway(t *testing.T) { var err error @@ -56,7 +56,7 @@ func TestInitOneway(t *testing.T) { if err != nil { t.Fatal("Unable to create server socket", err) } - processor := OnewayTest.NewOneWayProcessor(&impl{}) + processor := onewaytest.NewOneWayProcessor(&impl{}) server = thrift.NewTSimpleServer2(processor, serverTransport) go server.Serve() @@ -65,7 +65,7 @@ func TestInitOneway(t *testing.T) { func TestInitOnewayClient(t *testing.T) { transport := thrift.NewTSocketFromAddrTimeout(addr, TIMEOUT) protocol := thrift.NewTBinaryProtocolTransport(transport) - client = OnewayTest.NewOneWayClientProtocol(transport, protocol, protocol) + client = onewaytest.NewOneWayClientProtocol(transport, protocol, protocol) err := transport.Open() if err != nil { t.Fatal("Unable to open client socket", err) diff --git a/lib/go/test/tests/optional_fields_test.go b/lib/go/test/tests/optional_fields_test.go index 4b0797c4..324bf009 100644 --- a/lib/go/test/tests/optional_fields_test.go +++ b/lib/go/test/tests/optional_fields_test.go @@ -20,15 +20,15 @@ package tests import ( - "OptionalFieldsTest" "bytes" gomock "code.google.com/p/gomock/gomock" + "optionalfieldstest" "testing" "thrift" ) func TestIsSetReturnFalseOnCreation(t *testing.T) { - ao := OptionalFieldsTest.NewAllOptional() + ao := optionalfieldstest.NewAllOptional() if ao.IsSetS() { t.Errorf("Optional field S is set on initialization") } @@ -71,7 +71,7 @@ func TestIsSetReturnFalseOnCreation(t *testing.T) { } func TestDefaultValuesOnCreation(t *testing.T) { - ao := OptionalFieldsTest.NewAllOptional() + ao := optionalfieldstest.NewAllOptional() if ao.GetS() != "DEFAULT" { t.Errorf("Unexpected default value %#v for field S", ao.GetS()) } @@ -112,7 +112,7 @@ func TestDefaultValuesOnCreation(t *testing.T) { } func TestInitialValuesOnCreation(t *testing.T) { - ao := OptionalFieldsTest.NewAllOptional() + ao := optionalfieldstest.NewAllOptional() if ao.S != "DEFAULT" { t.Errorf("Unexpected initial value %#v for field S", ao.S) } @@ -152,11 +152,11 @@ func TestInitialValuesOnCreation(t *testing.T) { } func TestIsSetReturnTrueAfterUpdate(t *testing.T) { - ao := OptionalFieldsTest.NewAllOptional() + ao := optionalfieldstest.NewAllOptional() ao.S = "somevalue" ao.I = 123 ao.B = true - ao.Aa = OptionalFieldsTest.NewStructA() + ao.Aa = optionalfieldstest.NewStructA() if !ao.IsSetS() { t.Errorf("Field S should be set") } @@ -172,7 +172,7 @@ func TestIsSetReturnTrueAfterUpdate(t *testing.T) { } func TestListNotEmpty(t *testing.T) { - ao := OptionalFieldsTest.NewAllOptional() + ao := optionalfieldstest.NewAllOptional() ao.L = []int64{1, 2, 3} if !ao.IsSetL() { t.Errorf("Field L should be set") @@ -189,7 +189,7 @@ func TestNoOptionalUnsetFieldsOnWire(t *testing.T) { proto.EXPECT().WriteFieldStop().Return(nil), proto.EXPECT().WriteStructEnd().Return(nil), ) - ao := OptionalFieldsTest.NewAllOptional() + ao := optionalfieldstest.NewAllOptional() ao.Write(proto) } @@ -202,7 +202,7 @@ func TestNoSetToDefaultFieldsOnWire(t *testing.T) { proto.EXPECT().WriteFieldStop().Return(nil), proto.EXPECT().WriteStructEnd().Return(nil), ) - ao := OptionalFieldsTest.NewAllOptional() + ao := optionalfieldstest.NewAllOptional() ao.I = 42 ao.Write(proto) } @@ -220,7 +220,7 @@ func TestOneISetFieldOnWire(t *testing.T) { proto.EXPECT().WriteFieldStop().Return(nil), proto.EXPECT().WriteStructEnd().Return(nil), ) - ao := OptionalFieldsTest.NewAllOptional() + ao := optionalfieldstest.NewAllOptional() ao.I = 123 ao.Write(proto) } @@ -240,7 +240,7 @@ func TestOneLSetFieldOnWire(t *testing.T) { proto.EXPECT().WriteFieldStop().Return(nil), proto.EXPECT().WriteStructEnd().Return(nil), ) - ao := OptionalFieldsTest.NewAllOptional() + ao := optionalfieldstest.NewAllOptional() ao.L = []int64{1, 2} ao.Write(proto) } @@ -257,7 +257,7 @@ func TestOneBinSetFieldOnWire(t *testing.T) { proto.EXPECT().WriteFieldStop().Return(nil), proto.EXPECT().WriteStructEnd().Return(nil), ) - ao := OptionalFieldsTest.NewAllOptional() + ao := optionalfieldstest.NewAllOptional() ao.Bin = []byte("somebytestring") ao.Write(proto) } @@ -274,7 +274,7 @@ func TestOneEmptyBinSetFieldOnWire(t *testing.T) { proto.EXPECT().WriteFieldStop().Return(nil), proto.EXPECT().WriteStructEnd().Return(nil), ) - ao := OptionalFieldsTest.NewAllOptional() + ao := optionalfieldstest.NewAllOptional() ao.Bin = []byte{} ao.Write(proto) } diff --git a/test/ThriftTest.thrift b/test/ThriftTest.thrift index 7ca194ef..4a689fee 100644 --- a/test/ThriftTest.thrift +++ b/test/ThriftTest.thrift @@ -31,7 +31,7 @@ namespace js ThriftTest namespace st ThriftTest namespace py ThriftTest namespace py.twisted ThriftTest -namespace go ThriftTest +namespace go thrifttest namespace php ThriftTest namespace delphi Thrift.Test namespace cocoa ThriftTest -- 2.17.1