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 2013/11/05 01:32:20 UTC

git commit: THRIFT-2246 Unset enum value is printed by ToString() - fix for some edge cases

Updated Branches:
  refs/heads/master 088c26b40 -> 0ec155e16


THRIFT-2246 Unset enum value is printed by ToString() - fix for some edge cases

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/0ec155e1
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/0ec155e1
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/0ec155e1

Branch: refs/heads/master
Commit: 0ec155e1608c2909183b7c5e0b08a4a80579b4bd
Parents: 088c26b
Author: Jens Geyer <je...@apache.org>
Authored: Tue Nov 5 00:49:10 2013 +0100
Committer: Jens Geyer <je...@apache.org>
Committed: Tue Nov 5 00:49:10 2013 +0100

----------------------------------------------------------------------
 compiler/cpp/src/generate/t_csharp_generator.cc | 18 ++++++++---
 compiler/cpp/src/generate/t_delphi_generator.cc | 33 ++++++++++++++------
 2 files changed, 36 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/thrift/blob/0ec155e1/compiler/cpp/src/generate/t_csharp_generator.cc
----------------------------------------------------------------------
diff --git a/compiler/cpp/src/generate/t_csharp_generator.cc b/compiler/cpp/src/generate/t_csharp_generator.cc
index be20496..baa229e 100644
--- a/compiler/cpp/src/generate/t_csharp_generator.cc
+++ b/compiler/cpp/src/generate/t_csharp_generator.cc
@@ -922,7 +922,15 @@ void t_csharp_generator::generate_csharp_struct_tostring(ofstream& out, t_struct
   const vector<t_field*>& fields = tstruct->get_members();
   vector<t_field*>::const_iterator f_iter;
 
-  indent(out) << "bool __first = true;" << endl;
+  bool useFirstFlag = false;
+  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+    if( ! field_is_required((*f_iter))) {
+      indent(out) << "bool __first = true;" << endl;
+	  useFirstFlag = true;
+    }
+    break;
+  }
+
   bool had_required = false;  // set to true after first required field has been processed
 
   for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
@@ -944,17 +952,17 @@ void t_csharp_generator::generate_csharp_struct_tostring(ofstream& out, t_struct
       }
     }
 
-    if( ! had_required)	{
+    if( useFirstFlag && (! had_required)) {
       indent(out) << "if(!__first) { __sb.Append(\", \"); }" << endl;
-	  if( ! is_required) {
+      if( ! is_required) {
         indent(out) << "__first = false;" << endl;
-	  }
+      }
       indent(out) <<
         "__sb.Append(\"" << prop_name((*f_iter)) << ": \");" << endl;
     } else {
       indent(out) <<
         "__sb.Append(\", " << prop_name((*f_iter)) << ": \");" << endl;
-	}
+    }
         
           
     t_type* ttype = (*f_iter)->get_type();

http://git-wip-us.apache.org/repos/asf/thrift/blob/0ec155e1/compiler/cpp/src/generate/t_delphi_generator.cc
----------------------------------------------------------------------
diff --git a/compiler/cpp/src/generate/t_delphi_generator.cc b/compiler/cpp/src/generate/t_delphi_generator.cc
index 9a86566..954735a 100644
--- a/compiler/cpp/src/generate/t_delphi_generator.cc
+++ b/compiler/cpp/src/generate/t_delphi_generator.cc
@@ -3167,12 +3167,20 @@ void t_delphi_generator::generate_delphi_struct_tostring_impl(ostream& out, stri
 
   string tmp_sb = "__sb";
   string tmp_first = "__first";
+  bool useFirstFlag = false;
 
   indent_impl(out) << "function " << cls_prefix << cls_nm << ".ToString: string;" << endl;
   indent_impl(out) << "var" << endl;
   indent_up_impl();
   indent_impl(out) << tmp_sb << " : TThriftStringBuilder;" << endl;
-  indent_impl(out) << tmp_first << " : Boolean;" << endl;
+  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+    bool is_optional  = ((*f_iter)->get_req() != t_field::T_REQUIRED);
+    if( is_optional) {
+      indent_impl(out) << tmp_first << " : Boolean;" << endl;
+      useFirstFlag = true;
+    }
+    break;
+  }
   indent_down_impl();
   indent_impl(out) << "begin" << endl;
   indent_up_impl();
@@ -3181,7 +3189,10 @@ void t_delphi_generator::generate_delphi_struct_tostring_impl(ostream& out, stri
   indent_impl(out) << "try" << endl;
   indent_up_impl();
 
-  indent_impl(out) << tmp_first << " := TRUE;" << endl;
+  if( useFirstFlag) {
+    indent_impl(out) << tmp_first << " := TRUE;" << endl;
+  }
+  
   bool had_required = false;  // set to true after first required field has been processed
   
   for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
@@ -3201,9 +3212,9 @@ void t_delphi_generator::generate_delphi_struct_tostring_impl(ostream& out, stri
       }
     }
 
-    if( ! had_required)	{
-	  indent_impl(out) << "if not " << tmp_first << " then " << tmp_sb << ".Append(',');" << endl;
-	  if (is_optional) {
+    if( useFirstFlag && (! had_required)) {
+      indent_impl(out) << "if not " << tmp_first << " then " << tmp_sb << ".Append(',');" << endl;
+      if (is_optional) {
         indent_impl(out) << tmp_first << " := FALSE;" << endl;
       }
       indent_impl(out) <<
@@ -3212,7 +3223,7 @@ void t_delphi_generator::generate_delphi_struct_tostring_impl(ostream& out, stri
       indent_impl(out) <<
         tmp_sb << ".Append(', " << prop_name((*f_iter), is_exception) << ": ');" << endl;
     }
-	
+
 
     t_type* ttype = (*f_iter)->get_type();
     if (ttype->is_xception() || ttype->is_struct()) {
@@ -3225,12 +3236,12 @@ void t_delphi_generator::generate_delphi_struct_tostring_impl(ostream& out, stri
       indent_impl(out) <<
         tmp_sb << ".Append(" << prop_name((*f_iter), is_exception)  << ");" << endl;
     }
-	
+    
     if (null_allowed || is_optional) {
       indent_down_impl();
       indent_impl(out) << "end;" << endl;
     } 
-	
+    
     if (!is_optional) {
       had_required = true;  // now __first must be false, so we don't need to check it anymore
     }
@@ -3240,8 +3251,10 @@ void t_delphi_generator::generate_delphi_struct_tostring_impl(ostream& out, stri
     tmp_sb << ".Append(')');" << endl;
   indent_impl(out) <<
     "Result := " << tmp_sb <<  ".ToString;" << endl;
-  indent_impl(out) <<
-    "if " << tmp_first <<  " then {prevent warning};" << endl;
+  if( useFirstFlag) {
+    indent_impl(out) <<
+      "if " << tmp_first <<  " then {prevent warning};" << endl;
+  }
 
   indent_down_impl();
   indent_impl(out) << "finally" << endl;