THRIFT-1897 cocoa: Support validation of required fields
Patch: Kevin Li
diff --git a/compiler/cpp/src/generate/t_cocoa_generator.cc b/compiler/cpp/src/generate/t_cocoa_generator.cc
index c4265f7..15311cc 100644
--- a/compiler/cpp/src/generate/t_cocoa_generator.cc
+++ b/compiler/cpp/src/generate/t_cocoa_generator.cc
@@ -56,6 +56,9 @@
iter = parsed_options.find("log_unexpected");
log_unexpected_ = (iter != parsed_options.end());
+ iter = parsed_options.find("validate_required");
+ validate_required_ = (iter != parsed_options.end());
+
out_dir_base_ = "gen-cocoa";
}
@@ -101,6 +104,7 @@
void generate_cocoa_struct_reader(std::ofstream& out, t_struct* tstruct);
void generate_cocoa_struct_result_writer(std::ofstream& out, t_struct* tstruct);
void generate_cocoa_struct_writer(std::ofstream& out, t_struct* tstruct);
+ void generate_cocoa_struct_validator(std::ofstream& out, t_struct* tstruct);
void generate_cocoa_struct_description(std::ofstream& out, t_struct* tstruct);
std::string function_result_helper_struct_type(t_function* tfunction);
@@ -217,6 +221,7 @@
std::ofstream f_impl_;
bool log_unexpected_;
+ bool validate_required_;
};
@@ -279,6 +284,7 @@
string result = string() +
"#import \"TProtocol.h\"\n" +
"#import \"TApplicationException.h\"\n" +
+ "#import \"TProtocolException.h\"\n" +
"#import \"TProtocolUtil.h\"\n" +
"#import \"TProcessor.h\"\n" +
"#import \"TObjective-C.h\"\n" +
@@ -514,6 +520,9 @@
out << "- (void) write: (id <TProtocol>) outProtocol;" << endl;
out << endl;
+ // validator
+ out << "- (void) validate;" << endl << endl;
+
// getters and setters
generate_cocoa_struct_field_accessor_declarations(out, tstruct, is_exception);
@@ -807,6 +816,7 @@
} else {
generate_cocoa_struct_writer(out, tstruct);
}
+ generate_cocoa_struct_validator(out, tstruct);
generate_cocoa_struct_description(out, tstruct);
out << "@end" << endl << endl;
@@ -910,6 +920,12 @@
out <<
indent() << "[inProtocol readStructEnd];" << endl;
+ // performs various checks (e.g. check that all required fields are set)
+ if (validate_required_) {
+ out <<
+ indent() << "[self validate];" << endl;
+ }
+
indent_down();
out <<
indent() << "}" << endl <<
@@ -1047,6 +1063,39 @@
}
/**
+ * Generates a function to perform various checks
+ * (e.g. check that all required fields are set)
+ *
+ * @param tstruct The struct definition
+ */
+void t_cocoa_generator::generate_cocoa_struct_validator(ofstream& out,
+ t_struct* tstruct) {
+ out <<
+ indent() << "- (void) validate {" << endl;
+ indent_up();
+
+ const vector<t_field*>& fields = tstruct->get_members();
+ vector<t_field*>::const_iterator f_iter;
+
+ out << indent() << "// check for required fields" << endl;
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ t_field* field = (*f_iter);
+ if ((*f_iter)->get_req() == t_field::T_REQUIRED) {
+ out <<
+ indent() << "if (!__" << field->get_name() << "_isset) {" << endl <<
+ indent() << " @throw [TProtocolException exceptionWithName: @\"TProtocolException\"" << endl <<
+ indent() << " reason: @\"Required field '" << (*f_iter)->get_name() << "' is not set.\"];" << endl <<
+ indent() << "}" << endl;
+ }
+ }
+
+ indent_down();
+ out <<
+ indent() << "}" << endl <<
+ endl;
+}
+
+/**
* Generate property accessor methods for all fields in the struct.
* getter, setter, isset getter.
*
@@ -2671,5 +2720,7 @@
THRIFT_REGISTER_GENERATOR(cocoa, "Cocoa",
" log_unexpected: Log every time an unexpected field ID or type is encountered.\n"
+" validate_required:\n"
+" Throws exception if any required field is not set.\n"
)