You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by je...@apache.org on 2014/02/04 23:57:15 UTC

git commit: THRIFT-2343 Fix tutotial code and codegen for methods without exceptions

Updated Branches:
  refs/heads/master 4904ab81f -> 38b1a04b6


THRIFT-2343 Fix tutotial code and codegen for methods without exceptions

Patch: Jens Geyer


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

Branch: refs/heads/master
Commit: 38b1a04b60f1122d94b8e83243f67a9c93182456
Parents: 4904ab8
Author: Jens Geyer <je...@apache.org>
Authored: Tue Feb 4 23:56:39 2014 +0100
Committer: Jens Geyer <je...@apache.org>
Committed: Tue Feb 4 23:56:39 2014 +0100

----------------------------------------------------------------------
 compiler/cpp/src/generate/t_go_generator.cc | 33 +++++++++++++++---------
 tutorial/go/src/client.go                   | 14 +++++-----
 tutorial/go/src/handler.go                  | 10 ++++---
 tutorial/go/src/server.go                   |  2 +-
 4 files changed, 35 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/thrift/blob/38b1a04b/compiler/cpp/src/generate/t_go_generator.cc
----------------------------------------------------------------------
diff --git a/compiler/cpp/src/generate/t_go_generator.cc b/compiler/cpp/src/generate/t_go_generator.cc
index 29b1411..6b90cab 100644
--- a/compiler/cpp/src/generate/t_go_generator.cc
+++ b/compiler/cpp/src/generate/t_go_generator.cc
@@ -1717,7 +1717,7 @@ void t_go_generator::generate_service_client(t_service* tservice)
                        indent() << "  return" << endl <<
                        indent() << "}" << endl <<
                        indent() << "if p.SeqId != seqId {" << endl <<
-                       indent() << "  err = thrift.NewTApplicationException(thrift.BAD_SEQUENCE_ID, \"ping failed: out of sequence response\")" << endl <<
+                       indent() << "  err = thrift.NewTApplicationException(thrift.BAD_SEQUENCE_ID, \"" << (*f_iter)->get_name() << " failed: out of sequence response\")" << endl <<
                        indent() << "  return" << endl <<
                        indent() << "}" << endl <<
                        indent() << result << " := New" << publicize(resultname) << "()" << endl <<
@@ -2351,28 +2351,37 @@ void t_go_generator::generate_process_function(t_service* tservice,
         f_service_ << "args." << publicize(variable_name_to_go_name((*f_iter)->get_name()));
     }
 
-    f_service_ << "); err2 != nil {" << endl <<
-               indent() << "switch v := err2.(type) {" << endl;
-
+    f_service_ << "); err2 != nil {" << endl;
+    
     t_struct* exceptions = tfunction->get_xceptions();
     const vector<t_field*>& x_fields = exceptions->get_members();
-    vector<t_field*>::const_iterator xf_iter;
+	if( ! x_fields.empty()) {
+        f_service_ << indent() << "switch v := err2.(type) {" << endl;
 
-    for (xf_iter = x_fields.begin(); xf_iter != x_fields.end(); ++xf_iter) {
-        f_service_ <<
-                    indent() << "  case *" << type_name((*xf_iter)->get_type()) << ":" << endl <<
-                    indent() << "result." << publicize(variable_name_to_go_name((*xf_iter)->get_name())) << " = v" << endl;
+        vector<t_field*>::const_iterator xf_iter;
+
+        for (xf_iter = x_fields.begin(); xf_iter != x_fields.end(); ++xf_iter) {
+            f_service_ <<
+                        indent() << "  case *" << type_name((*xf_iter)->get_type()) << ":" << endl <<
+                        indent() << "result." << publicize(variable_name_to_go_name((*xf_iter)->get_name())) << " = v" << endl;
+        }
+        
+		f_service_ <<
+                   indent() << "  default:" << endl;
     }
 
     f_service_ <<
-               indent() << "  default:" << endl <<
                indent() << "  x := thrift.NewTApplicationException(thrift.INTERNAL_ERROR, \"Internal error processing " << escape_string(tfunction->get_name()) << ": \" + err.Error())" << endl <<
                indent() << "  oprot.WriteMessageBegin(\"" << escape_string(tfunction->get_name()) << "\", thrift.EXCEPTION, seqId)" << endl <<
                indent() << "  x.Write(oprot)" << endl <<
                indent() << "  oprot.WriteMessageEnd()" << endl <<
                indent() << "  oprot.Flush()" << endl <<
-               indent() << "  return false, err2" << endl <<
-               indent() << "}" << endl;
+               indent() << "  return false, err2" << endl ;
+			   
+    if( ! x_fields.empty()) {
+	    f_service_ <<
+                   indent() << "}" << endl;
+    }			   
 
     f_service_ <<
                indent() << "}" << endl <<

http://git-wip-us.apache.org/repos/asf/thrift/blob/38b1a04b/tutorial/go/src/client.go
----------------------------------------------------------------------
diff --git a/tutorial/go/src/client.go b/tutorial/go/src/client.go
index 543d7fb..31376f8 100644
--- a/tutorial/go/src/client.go
+++ b/tutorial/go/src/client.go
@@ -20,10 +20,10 @@ package main
  */
 
 import (
+	"crypto/tls"
 	"fmt"
 	"git.apache.org/thrift.git/lib/go/thrift"
 	"tutorial"
-	"crypto/tls"
 )
 
 func handleClient(client *tutorial.CalculatorClient) (err error) {
@@ -37,12 +37,12 @@ func handleClient(client *tutorial.CalculatorClient) (err error) {
 	work.Op = tutorial.Operation_DIVIDE
 	work.Num1 = 1
 	work.Num2 = 0
-	quotient, ouch, err := client.Calculate(1, work)
+	quotient, err := client.Calculate(1, work)
 	if err != nil {
 		fmt.Println("Error during operation:", err)
 		return err
-	} else if ouch != nil {
-		fmt.Println("Invalid operation:", ouch)
+	//} else if ouch != nil {
+	//	fmt.Println("Invalid operation:", ouch)
 	} else {
 		fmt.Println("Whoa we can divide by 0 with new value:", quotient)
 	}
@@ -50,12 +50,12 @@ func handleClient(client *tutorial.CalculatorClient) (err error) {
 	work.Op = tutorial.Operation_SUBTRACT
 	work.Num1 = 15
 	work.Num2 = 10
-	diff, ouch, err := client.Calculate(1, work)
+	diff, err := client.Calculate(1, work)
 	if err != nil {
 		fmt.Println("Error during operation:", err)
 		return err
-	} else if ouch != nil {
-		fmt.Println("Invalid operation:", ouch)
+	//} else if ouch != nil {
+	//	fmt.Println("Invalid operation:", ouch)
 	} else {
 		fmt.Print("15-10=", diff, "\n")
 	}

http://git-wip-us.apache.org/repos/asf/thrift/blob/38b1a04b/tutorial/go/src/handler.go
----------------------------------------------------------------------
diff --git a/tutorial/go/src/handler.go b/tutorial/go/src/handler.go
index 3d4c18c..fb0daef 100644
--- a/tutorial/go/src/handler.go
+++ b/tutorial/go/src/handler.go
@@ -44,7 +44,7 @@ func (p *CalculatorHandler) Add(num1 int32, num2 int32) (retval17 int32, err err
 	return num1 + num2, nil
 }
 
-func (p *CalculatorHandler) Calculate(logid int32, w *tutorial.Work) (val int32, ouch *tutorial.InvalidOperation, err error) {
+func (p *CalculatorHandler) Calculate(logid int32, w *tutorial.Work) (val int32, err error) {
 	fmt.Print("calculate(", logid, ", {", w.Op, ",", w.Num1, ",", w.Num2, "})\n")
 	switch w.Op {
 	case tutorial.Operation_ADD:
@@ -58,17 +58,19 @@ func (p *CalculatorHandler) Calculate(logid int32, w *tutorial.Work) (val int32,
 		break
 	case tutorial.Operation_DIVIDE:
 		if w.Num2 == 0 {
-			ouch = tutorial.NewInvalidOperation()
+			ouch := tutorial.NewInvalidOperation()
 			ouch.What = int32(w.Op)
 			ouch.Why = "Cannot divide by 0"
+			err = ouch
 			return
 		}
 		val = w.Num1 / w.Num2
 		break
 	default:
-		ouch = tutorial.NewInvalidOperation()
+		ouch := tutorial.NewInvalidOperation()
 		ouch.What = int32(w.Op)
 		ouch.Why = "Unknown operation"
+		err = ouch
 		return
 	}
 	entry := shared.NewSharedStruct()
@@ -84,7 +86,7 @@ func (p *CalculatorHandler) Calculate(logid int32, w *tutorial.Work) (val int32,
 	   }
 	*/
 	p.log[k] = entry
-	return val, ouch, err
+	return val, err
 }
 
 func (p *CalculatorHandler) GetStruct(key int32) (*shared.SharedStruct, error) {

http://git-wip-us.apache.org/repos/asf/thrift/blob/38b1a04b/tutorial/go/src/server.go
----------------------------------------------------------------------
diff --git a/tutorial/go/src/server.go b/tutorial/go/src/server.go
index ebcfe5b..e4c4b97 100644
--- a/tutorial/go/src/server.go
+++ b/tutorial/go/src/server.go
@@ -20,10 +20,10 @@ package main
  */
 
 import (
+	"crypto/tls"
 	"fmt"
 	"git.apache.org/thrift.git/lib/go/thrift"
 	"tutorial"
-	"crypto/tls"
 )
 
 func runServer(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory, addr string, secure bool) error {