You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by ro...@apache.org on 2013/03/22 22:13:15 UTC
[1/2] git commit: THRIFT-1897 cocoa: Support validation of required
fields Patch: Kevin Li
Updated Branches:
refs/heads/master 565517a16 -> ff951bd56
THRIFT-1897 cocoa: Support validation of required fields
Patch: Kevin Li
Project: http://git-wip-us.apache.org/repos/asf/thrift/repo
Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/5505465f
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/5505465f
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/5505465f
Branch: refs/heads/master
Commit: 5505465ffa6476a4e64f01d0d27c540d86fe48fe
Parents: 565517a
Author: Roger Meier <ro...@apache.org>
Authored: Fri Mar 22 22:11:01 2013 +0100
Committer: Roger Meier <ro...@apache.org>
Committed: Fri Mar 22 22:11:01 2013 +0100
----------------------------------------------------------------------
compiler/cpp/src/generate/t_cocoa_generator.cc | 51 +++++++++++++++++++
1 files changed, 51 insertions(+), 0 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/thrift/blob/5505465f/compiler/cpp/src/generate/t_cocoa_generator.cc
----------------------------------------------------------------------
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 @@ class t_cocoa_generator : public t_oop_generator {
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 @@ class t_cocoa_generator : public t_oop_generator {
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 @@ class t_cocoa_generator : public t_oop_generator {
std::ofstream f_impl_;
bool log_unexpected_;
+ bool validate_required_;
};
@@ -279,6 +284,7 @@ string t_cocoa_generator::cocoa_thrift_imports() {
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 @@ void t_cocoa_generator::generate_cocoa_struct_interface(ofstream &out,
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 @@ void t_cocoa_generator::generate_cocoa_struct_implementation(ofstream &out,
} 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 @@ void t_cocoa_generator::generate_cocoa_struct_reader(ofstream& out,
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 @@ void t_cocoa_generator::generate_cocoa_struct_result_writer(ofstream& out,
}
/**
+ * 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 @@ string t_cocoa_generator::call_field_setter(t_field* tfield, string fieldName) {
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"
)
[2/2] git commit: THRIFT-1863 cocoa: [TBinaryProtocol
readMessageBeginReturnName:type:sequenceID] returns incorrect message type
Patch: Kevin Li
Posted by ro...@apache.org.
THRIFT-1863 cocoa: [TBinaryProtocol readMessageBeginReturnName:type:sequenceID] returns incorrect message type
Patch: Kevin Li
Project: http://git-wip-us.apache.org/repos/asf/thrift/repo
Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/ff951bd5
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/ff951bd5
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/ff951bd5
Branch: refs/heads/master
Commit: ff951bd5675a1ba72b39abeff8aafb770d3c43cf
Parents: 5505465
Author: Roger Meier <ro...@apache.org>
Authored: Fri Mar 22 22:12:19 2013 +0100
Committer: Roger Meier <ro...@apache.org>
Committed: Fri Mar 22 22:12:19 2013 +0100
----------------------------------------------------------------------
lib/cocoa/src/protocol/TBinaryProtocol.m | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/thrift/blob/ff951bd5/lib/cocoa/src/protocol/TBinaryProtocol.m
----------------------------------------------------------------------
diff --git a/lib/cocoa/src/protocol/TBinaryProtocol.m b/lib/cocoa/src/protocol/TBinaryProtocol.m
index f269aaa..bee616e 100644
--- a/lib/cocoa/src/protocol/TBinaryProtocol.m
+++ b/lib/cocoa/src/protocol/TBinaryProtocol.m
@@ -118,7 +118,7 @@ static TBinaryProtocolFactory * gSharedFactory = nil;
reason: @"Bad version in readMessageBegin"];
}
if (type != NULL) {
- *type = version & 0x00FF;
+ *type = size & 0x00FF;
}
NSString * messageName = [self readString];
if (name != NULL) {