@@ -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 */
906909string 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 */
34653440string 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 " )
0 commit comments