THRIFT-399. hs: Fix set and number issues in generated constant code
authorKevin Clark <kclark@apache.org>
Tue, 24 Mar 2009 15:56:19 +0000 (15:56 +0000)
committerKevin Clark <kclark@apache.org>
Tue, 24 Mar 2009 15:56:19 +0000 (15:56 +0000)
Author: Spiridon Eliopoulos

git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@757864 13f79535-47bb-0310-9956-ffa450edef68

compiler/cpp/src/generate/t_hs_generator.cc

index 91a7204..95f04ff 100644 (file)
@@ -287,6 +287,7 @@ void t_hs_generator::generate_const(t_const* tconst) {
   string name = decapitalize(tconst->get_name());
   t_const_value* value = tconst->get_value();
 
+  indent(f_consts_) << name << " :: " << render_hs_type(type, false) << endl;
   indent(f_consts_) << name << " = " << render_const_value(type, value) << endl << endl;
 }
 
@@ -383,13 +384,24 @@ string t_hs_generator::render_const_value(t_type* type, t_const_value* value) {
       out << "(" << key << ","<< val << ")";
     }
     out << "])";
-  } else if (type->is_list()) {
+  } else if (type->is_list() || type->is_set()) {
     t_type* etype;
-    etype = ((t_list*)type)->get_elem_type();
-    out << "[";
+
+    if (type->is_list()) {
+        etype = ((t_list*) type)->get_elem_type();
+    } else  {
+        etype = ((t_set*) type)->get_elem_type();
+    }
+
     const vector<t_const_value*>& val = value->get_list();
     vector<t_const_value*>::const_iterator v_iter;
-    bool first=true;
+    bool first = true;
+
+    if (type->is_set())
+        out << "(Set.fromList ";
+
+    out << "[";
+
     for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
       if(first)
         first=false;
@@ -397,17 +409,10 @@ string t_hs_generator::render_const_value(t_type* type, t_const_value* value) {
         out << ",";
       out << render_const_value(etype, *v_iter);
     }
+
     out << "]";
-  } else if (type->is_set()) {
-    t_type* etype = ((t_set*)type)->get_elem_type();
-    const vector<t_const_value*>& val = value->get_list();
-    vector<t_const_value*>::const_iterator v_iter;
-    out << "(mkSet [";
-    for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
-      string val = render_const_value(etype, *v_iter);
-      out << val;
-    }
-    out << "])";
+    if (type->is_set())
+        out << ")";
   } else {
     throw "CANNOT GENERATE CONSTANT FOR TYPE: " + type->get_name();
   }