Skip to content

Commit c0d384a

Browse files
taozleJens-G
authored andcommitted
THRIFT-4236 Support context in go generated code
Client: Go Patch: taozle <[email protected]> This closes apache#1309
1 parent 0dd8235 commit c0d384a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1520
-683
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ RUN buildDeps=" \
5050
/thrift \
5151
&& cmake --build . --config Release \
5252
&& make install \
53-
&& curl -k -sSL "https://storage.googleapis.com/golang/go1.8.3.linux-amd64.tar.gz" -o /tmp/go.tar.gz \
53+
&& curl -k -sSL "https://storage.googleapis.com/golang/go1.5.2.linux-amd64.tar.gz" -o /tmp/go.tar.gz \
5454
&& tar xzf /tmp/go.tar.gz -C /tmp \
5555
&& cp /tmp/go/bin/gofmt /usr/bin/gofmt \
5656
&& apt-get purge -y --auto-remove $buildDeps \

build/docker/centos/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ RUN curl -sSL http://packages.erlang-solutions.com/rpm/centos/erlang_solutions.r
102102
erlang-tools
103103

104104
# Go Dependencies
105-
RUN curl -sSL https://storage.googleapis.com/golang/go1.8.3.linux-amd64.tar.gz | tar -C /usr/local/ -xz
105+
RUN curl -sSL https://storage.googleapis.com/golang/go1.4.3.linux-amd64.tar.gz | tar -C /usr/local/ -xz
106106
ENV PATH /usr/local/go/bin:$PATH
107107

108108
# Haskell Dependencies

build/docker/debian/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ RUN pip2 install -U ipaddress backports.ssl_match_hostname tornado
155155
RUN pip3 install -U backports.ssl_match_hostname tornado
156156

157157
# Go
158-
RUN curl -sSL https://storage.googleapis.com/golang/go1.8.3.linux-amd64.tar.gz | tar -C /usr/local/ -xz
158+
RUN curl -sSL https://storage.googleapis.com/golang/go1.4.3.linux-amd64.tar.gz | tar -C /usr/local/ -xz
159159
ENV PATH /usr/local/go/bin:$PATH
160160

161161
# Haxe

build/docker/ubuntu/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ RUN pip2 install -U ipaddress backports.ssl_match_hostname tornado
177177
RUN pip3 install -U backports.ssl_match_hostname tornado
178178

179179
# Go
180-
RUN curl -sSL https://storage.googleapis.com/golang/go1.8.3.linux-amd64.tar.gz | tar -C /usr/local/ -xz
180+
RUN curl -sSL https://storage.googleapis.com/golang/go1.4.3.linux-amd64.tar.gz | tar -C /usr/local/ -xz
181181
ENV PATH /usr/local/go/bin:$PATH
182182

183183
# Haxe

compiler/cpp/src/thrift/generate/t_go_generator.cc

Lines changed: 23 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class t_go_generator : public t_generator {
8181
gen_package_prefix_ = "";
8282
package_flag = "";
8383
read_write_private_ = false;
84-
use_context_ = false;
84+
legacy_context_ = false;
8585
ignore_initialisms_ = false;
8686
for( iter = parsed_options.begin(); iter != parsed_options.end(); ++iter) {
8787
if( iter->first.compare("package_prefix") == 0) {
@@ -92,8 +92,8 @@ class t_go_generator : public t_generator {
9292
package_flag = (iter->second);
9393
} else if( iter->first.compare("read_write_private") == 0) {
9494
read_write_private_ = true;
95-
} else if( iter->first.compare("use_context") == 0) {
96-
use_context_ = true;
95+
} else if( iter->first.compare("legacy_context") == 0) {
96+
legacy_context_ = true;
9797
} else if( iter->first.compare("ignore_initialisms") == 0) {
9898
ignore_initialisms_ = true;
9999
} else {
@@ -288,7 +288,7 @@ class t_go_generator : public t_generator {
288288
std::string gen_package_prefix_;
289289
std::string gen_thrift_import_;
290290
bool read_write_private_;
291-
bool use_context_;
291+
bool legacy_context_;
292292
bool ignore_initialisms_;
293293

294294
/**
@@ -884,7 +884,10 @@ string t_go_generator::go_imports_begin(bool consts) {
884884
"\t\"database/sql/driver\"\n"
885885
"\t\"errors\"\n";
886886
}
887-
if (use_context_) {
887+
if (legacy_context_) {
888+
extra +=
889+
"\t\"golang.org/x/net/context\"\n";
890+
} else {
888891
extra +=
889892
"\t\"context\"\n";
890893
}
@@ -904,20 +907,13 @@ string t_go_generator::go_imports_begin(bool consts) {
904907
* This will have to do in lieu of more intelligent import statement construction
905908
*/
906909
string t_go_generator::go_imports_end() {
907-
string extra;
908-
909-
if (use_context_) {
910-
extra +=
911-
"var _ = context.Background\n";
912-
}
913-
914910
return string(
915911
")\n\n"
916912
"// (needed to ensure safety because of naive import list construction.)\n"
917913
"var _ = thrift.ZERO\n"
918914
"var _ = fmt.Printf\n"
915+
"var _ = context.Background\n"
919916
"var _ = reflect.DeepEqual\n"
920-
+ extra +
921917
"var _ = bytes.Equal\n\n");
922918
}
923919

@@ -1842,7 +1838,7 @@ void t_go_generator::generate_service_interface(t_service* tservice) {
18421838

18431839
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
18441840
generate_go_docstring(f_types_, (*f_iter));
1845-
f_types_ << indent() << function_signature_if(*f_iter, "", true, use_context_) << endl;
1841+
f_types_ << indent() << function_signature_if(*f_iter, "", true, true) << endl;
18461842
}
18471843
}
18481844

@@ -2604,36 +2600,31 @@ void t_go_generator::generate_service_server(t_service* tservice) {
26042600
// Generate the header portion
26052601
string self(tmp("self"));
26062602

2607-
string processorFunction("thrift.TProcessorFunction");
2608-
if (use_context_) {
2609-
processorFunction = "thrift.TProcessorFunction2";
2610-
}
2611-
26122603
if (extends_processor.empty()) {
26132604
f_types_ << indent() << "type " << serviceName << "Processor struct {" << endl;
2614-
f_types_ << indent() << " processorMap map[string]" << processorFunction << endl;
2605+
f_types_ << indent() << " processorMap map[string]thrift.TProcessorFunction" << endl;
26152606
f_types_ << indent() << " handler " << serviceName << endl;
26162607
f_types_ << indent() << "}" << endl << endl;
26172608
f_types_ << indent() << "func (p *" << serviceName
2618-
<< "Processor) AddToProcessorMap(key string, processor " << processorFunction << ") {"
2609+
<< "Processor) AddToProcessorMap(key string, processor thrift.TProcessorFunction) {"
26192610
<< endl;
26202611
f_types_ << indent() << " p.processorMap[key] = processor" << endl;
26212612
f_types_ << indent() << "}" << endl << endl;
26222613
f_types_ << indent() << "func (p *" << serviceName
26232614
<< "Processor) GetProcessorFunction(key string) "
2624-
"(processor "<< processorFunction << ", ok bool) {" << endl;
2615+
"(processor thrift.TProcessorFunction, ok bool) {" << endl;
26252616
f_types_ << indent() << " processor, ok = p.processorMap[key]" << endl;
26262617
f_types_ << indent() << " return processor, ok" << endl;
26272618
f_types_ << indent() << "}" << endl << endl;
26282619
f_types_ << indent() << "func (p *" << serviceName
2629-
<< "Processor) ProcessorMap() map[string]" << processorFunction << "{" << endl;
2620+
<< "Processor) ProcessorMap() map[string]thrift.TProcessorFunction {" << endl;
26302621
f_types_ << indent() << " return p.processorMap" << endl;
26312622
f_types_ << indent() << "}" << endl << endl;
26322623
f_types_ << indent() << "func New" << serviceName << "Processor(handler " << serviceName
26332624
<< ") *" << serviceName << "Processor {" << endl << endl;
26342625
f_types_
26352626
<< indent() << " " << self << " := &" << serviceName
2636-
<< "Processor{handler:handler, processorMap:make(map[string]" << processorFunction << ")}"
2627+
<< "Processor{handler:handler, processorMap:make(map[string]thrift.TProcessorFunction)}"
26372628
<< endl;
26382629

26392630
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
@@ -2643,24 +2634,16 @@ void t_go_generator::generate_service_server(t_service* tservice) {
26432634
<< "{handler:handler}" << endl;
26442635
}
26452636

2646-
string ctxParam("");
2647-
string ctxVar("");
2648-
if (use_context_) {
2649-
ctxParam = "ctx context.Context, ";
2650-
ctxVar = "ctx, ";
2651-
}
2652-
26532637
string x(tmp("x"));
26542638
f_types_ << indent() << "return " << self << endl;
26552639
f_types_ << indent() << "}" << endl << endl;
26562640
f_types_ << indent() << "func (p *" << serviceName
2657-
<< "Processor) Process(" << ctxParam
2658-
<< "iprot, oprot thrift.TProtocol) (success bool, err "
2641+
<< "Processor) Process(ctx context.Context, iprot, oprot thrift.TProtocol) (success bool, err "
26592642
"thrift.TException) {" << endl;
26602643
f_types_ << indent() << " name, _, seqId, err := iprot.ReadMessageBegin()" << endl;
26612644
f_types_ << indent() << " if err != nil { return false, err }" << endl;
26622645
f_types_ << indent() << " if processor, ok := p.GetProcessorFunction(name); ok {" << endl;
2663-
f_types_ << indent() << " return processor.Process(" << ctxVar << "seqId, iprot, oprot)" << endl;
2646+
f_types_ << indent() << " return processor.Process(ctx, seqId, iprot, oprot)" << endl;
26642647
f_types_ << indent() << " }" << endl;
26652648
f_types_ << indent() << " iprot.Skip(thrift.STRUCT)" << endl;
26662649
f_types_ << indent() << " iprot.ReadMessageEnd()" << endl;
@@ -2714,20 +2697,14 @@ void t_go_generator::generate_process_function(t_service* tservice, t_function*
27142697
string argsname = publicize(tfunction->get_name() + "_args", true);
27152698
string resultname = publicize(tfunction->get_name() + "_result", true);
27162699

2717-
string ctxParam("");
2718-
string ctxVar("");
2719-
if (use_context_) {
2720-
ctxParam = "ctx context.Context, ";
2721-
ctxVar = "ctx";
2722-
}
27232700
// t_struct* xs = tfunction->get_xceptions();
27242701
// const std::vector<t_field*>& xceptions = xs->get_members();
27252702
vector<t_field*>::const_iterator x_iter;
27262703
f_types_ << indent() << "type " << processorName << " struct {" << endl;
27272704
f_types_ << indent() << " handler " << publicize(tservice->get_name()) << endl;
27282705
f_types_ << indent() << "}" << endl << endl;
27292706
f_types_ << indent() << "func (p *" << processorName
2730-
<< ") Process(" << ctxParam << "seqId int32, iprot, oprot thrift.TProtocol) (success bool, err "
2707+
<< ") Process(ctx context.Context, seqId int32, iprot, oprot thrift.TProtocol) (success bool, err "
27312708
"thrift.TException) {" << endl;
27322709
indent_up();
27332710
f_types_ << indent() << "args := " << argsname << "{}" << endl;
@@ -2771,13 +2748,11 @@ void t_go_generator::generate_process_function(t_service* tservice, t_function*
27712748
f_types_ << "err2 = p.handler." << publicize(tfunction->get_name()) << "(";
27722749
bool first = true;
27732750

2774-
f_types_ << ctxVar;
2751+
f_types_ << "ctx";
27752752
for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
27762753
if (first) {
27772754
first = false;
2778-
if (use_context_) {
2779-
f_types_ << ", ";
2780-
}
2755+
f_types_ << ", ";
27812756
} else {
27822757
f_types_ << ", ";
27832758
}
@@ -3459,7 +3434,7 @@ string t_go_generator::function_signature(t_function* tfunction, string prefix)
34593434
* Renders an interface function signature of the form 'type name(args)'
34603435
*
34613436
* @param tfunction Function definition
3462-
* @param disableContext Client doesn't suppport context for now.
3437+
* @param enableContext Client doesn't suppport context for now.
34633438
* @return String of rendered function definition
34643439
*/
34653440
string t_go_generator::function_signature_if(t_function* tfunction, string prefix, bool addError, bool enableContext) {
@@ -3745,5 +3720,5 @@ THRIFT_REGISTER_GENERATOR(go, "Go",
37453720
" Disable automatic spelling correction of initialisms (e.g. \"URL\")\n" \
37463721
" read_write_private\n"
37473722
" Make read/write methods private, default is public Read/Write\n" \
3748-
" use_context\n"
3749-
" Make service method receive a context as first argument.\n")
3723+
" legacy_context\n"
3724+
" Use legacy x/net/context instead of context in go<1.7.\n")

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ if test "$with_go" = "yes"; then
397397
AC_PATH_PROG([GO], [go])
398398
if [[ -x "$GO" ]] ; then
399399
AS_IF([test -n "$GO"],[
400-
ax_go_version="1.7"
400+
ax_go_version="1.4"
401401
402402
AC_MSG_CHECKING([for Go version])
403403
golang_version=`$GO version 2>&1 | $SED -e 's/\(go \)\(version \)\(go\)\(@<:@0-9@:>@.@<:@0-9@:>@.@<:@0-9@:>@\)\(@<:@\*@:>@*\).*/\4/'`

doc/install/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,5 @@ These are only required if you choose to build the libraries for the given langu
3939
* Bit::Vector
4040
* Class::Accessor
4141
* Haxe 3.1.3
42-
* Go 1.7
42+
* Go 1.4
4343
* Delphi 2010

lib/go/Makefile.am

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ install:
3131
@echo '##############################################################'
3232

3333
check-local:
34-
$(GO) test -race ./thrift
34+
GOPATH=`pwd` $(GO) get golang.org/x/net/context
35+
GOPATH=`pwd` $(GO) test -race ./thrift
3536

3637
all-local:
37-
$(GO) build ./thrift
38+
GOPATH=`pwd` $(GO) get golang.org/x/net/context
39+
GOPATH=`pwd` $(GO) build ./thrift
3840

3941
EXTRA_DIST = \
4042
thrift \

lib/go/test/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#
1919

2020
THRIFT = $(top_builddir)/compiler/cpp/thrift
21-
THRIFTARGS = -out gopath/src/ --gen go:thrift_import=thrift
21+
THRIFTARGS = -out gopath/src/ --gen go:thrift_import=thrift,legacy_context
2222
THRIFTTEST = $(top_srcdir)/test/ThriftTest.thrift
2323

2424
# Thrift for GO has problems with complex map keys: THRIFT-2063
@@ -57,6 +57,7 @@ gopath: $(THRIFT) $(THRIFTTEST) \
5757
$(THRIFT) $(THRIFTARGS),read_write_private DontExportRWTest.thrift
5858
$(THRIFT) $(THRIFTARGS),ignore_initialisms IgnoreInitialismsTest.thrift
5959
GOPATH=`pwd`/gopath $(GO) get github.com/golang/mock/gomock
60+
GOPATH=`pwd`/gopath $(GO) get golang.org/x/net/context
6061
ln -nfs ../../../thrift gopath/src/thrift
6162
ln -nfs ../../tests gopath/src/tests
6263
cp -r ./dontexportrwtest gopath/src

lib/go/test/tests/go17.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// +build go1.7
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
package tests
23+
24+
import (
25+
"context"
26+
"fmt"
27+
)
28+
29+
var defaultCtx = context.Background()
30+
31+
type FirstImpl struct{}
32+
33+
func (f *FirstImpl) ReturnOne(ctx context.Context) (r int64, err error) {
34+
return 1, nil
35+
}
36+
37+
type SecondImpl struct{}
38+
39+
func (s *SecondImpl) ReturnTwo(ctx context.Context) (r int64, err error) {
40+
return 2, nil
41+
}
42+
43+
type impl struct{}
44+
45+
func (i *impl) Hi(ctx context.Context, in int64, s string) (err error) { fmt.Println("Hi!"); return }
46+
func (i *impl) Emptyfunc(ctx context.Context) (err error) { return }
47+
func (i *impl) EchoInt(ctx context.Context, param int64) (r int64, err error) { return param, nil }

0 commit comments

Comments
 (0)