Repository: thrift
Updated Branches:
  refs/heads/master 46f9c4508 -> 932cad8dc


THRIFT-2420 Go argument parser for methods without arguments does not skip 
fields

Patch: Frank Schroeder & Jens Geyer


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

Branch: refs/heads/master
Commit: 932cad8dcc9c7bad95d2100e2f527620a5dca9fd
Parents: 46f9c45
Author: Jens Geyer <[email protected]>
Authored: Fri Mar 21 21:37:54 2014 +0200
Committer: Jens Geyer <[email protected]>
Committed: Fri Mar 21 21:37:54 2014 +0200

----------------------------------------------------------------------
 compiler/cpp/src/generate/t_go_generator.cc | 40 +++++++++++++++---------
 1 file changed, 26 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/thrift/blob/932cad8d/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 a216c2b..43def5d 100644
--- a/compiler/cpp/src/generate/t_go_generator.cc
+++ b/compiler/cpp/src/generate/t_go_generator.cc
@@ -1130,20 +1130,21 @@ void 
t_go_generator::generate_go_struct_reader(ofstream& out,
     // Check for field STOP marker and break
     out <<
         indent() << "if fieldTypeId == thrift.STOP { break; }" << endl;
-    // Switch statement on the field we are reading
-    bool first = true;
+
     string thriftFieldTypeId;
     // Generate deserialization code for known cases
     int32_t field_id = -1;
 
+    // Switch statement on the field we are reading, false if no fields present
+    bool have_switch = !fields.empty();
+    if( have_switch) {
+        indent(out) << "switch fieldId {" << endl;
+    }
+    
+    // All the fields we know
     for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
         field_id = (*f_iter)->get_key();
 
-        if (first) {
-            first = false;
-            indent(out) << "switch fieldId {" << endl;
-        }
-
         // if negative id, ensure we generate a valid method name
         string field_method_prefix("ReadField");
 
@@ -1152,8 +1153,9 @@ void t_go_generator::generate_go_struct_reader(ofstream& 
out,
             field_id *= -1;
         }
 
+        out << 
+            indent() <<"case " << field_id << ":" << endl;
         indent_up();
-        out << "case " << field_id << ":" << endl;
         thriftFieldTypeId = type_to_enum((*f_iter)->get_type());
 
         if (thriftFieldTypeId == "thrift.BINARY") {
@@ -1167,13 +1169,23 @@ void 
t_go_generator::generate_go_struct_reader(ofstream& out,
         indent_down();
     }
 
-    // In the default case we skip the field
-    if (!first) {
+    // Begin switch default case 
+    if( have_switch) {
+        out <<
+            indent() << "default:" << endl;
+            indent_up();
+    }
+            
+    // Skip unknown fields in either case
+    out <<
+        indent() << "if err := iprot.Skip(fieldTypeId); err != nil {" << endl 
<<
+        indent() << "  return err" << endl <<
+        indent() << "}" << endl;
+
+    // End switch default case
+    if( have_switch) {
+        indent_down();
         out <<
-            indent() << "default:" << endl <<
-            indent() << "  if err := iprot.Skip(fieldTypeId); err != nil {" << 
endl <<
-            indent() << "    return err" << endl <<
-            indent() << "  }" << endl <<
             indent() << "}" << endl;
     }
 

Reply via email to