Author: bryanduxbury
Date: Fri Jan 16 14:34:40 2009
New Revision: 735167

URL: http://svn.apache.org/viewvc?rev=735167&view=rev
Log:
THRIFT-224 Validate method should check that enum types are assigned valid 
values
Each generated enumeration type will now have a VALID_VALUES Set as a static 
member that contains all the values of the enumeration.

Modified:
    incubator/thrift/trunk/compiler/cpp/src/generate/t_rb_generator.cc

Modified: incubator/thrift/trunk/compiler/cpp/src/generate/t_rb_generator.cc
URL: 
http://svn.apache.org/viewvc/incubator/thrift/trunk/compiler/cpp/src/generate/t_rb_generator.cc?rev=735167&r1=735166&r2=735167&view=diff
==============================================================================
--- incubator/thrift/trunk/compiler/cpp/src/generate/t_rb_generator.cc 
(original)
+++ incubator/thrift/trunk/compiler/cpp/src/generate/t_rb_generator.cc Fri Jan 
16 14:34:40 2009
@@ -297,6 +297,18 @@
     f_types_ <<
       indent() << name << " = " << value << endl;
   }
+  
+  // Create a set with valid values for this enum
+  indent(f_types_) << "VALID_VALUES = Set.new([";
+  bool first = true;
+  for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
+    // Populate the set
+    if ((*c_iter)->has_value()){
+      first ? first = false: f_types_ << ", ";
+      f_types_ << capitalize((*c_iter)->get_name());
+    }          
+  }
+  f_types_ << "]).freeze" << endl;
 
   indent_down();
   indent(f_types_) <<
@@ -1060,6 +1072,19 @@
       }
       out << endl;
     }
+  }
+  
+  // if field is an enum, check that its value is valid
+  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+    t_field* field = (*f_iter);
+        
+    if (field->get_type()->is_enum()){      
+      indent(out) << "unless @" << field->get_name() << ".nil? || " << 
field->get_type()->get_name() << "::VALID_VALUES.include?(@" << 
field->get_name() << ")" << endl;      
+      indent_up();
+      indent(out) << "raise 
Thrift::ProtocolException.new(Thrift::ProtocolException::UNKNOWN, 'Invalid 
value of field " << field->get_name() << "!')" << endl;  
+      indent_down();
+      indent(out) << "end" << endl;
+    }
   }  
   
   indent_down();


Reply via email to