THRIFT-5835 Allow exceptions to be used as regular struct datatype
Initial feature testcase added, compiler checks disabled.
Patch: Jens Geyer
diff --git a/compiler/cpp/src/thrift/parse/t_function.h b/compiler/cpp/src/thrift/parse/t_function.h
index 57cf5ff..d2cb19b 100644
--- a/compiler/cpp/src/thrift/parse/t_function.h
+++ b/compiler/cpp/src/thrift/parse/t_function.h
@@ -85,17 +85,23 @@
void validate() const {
get_returntype()->validate();
+
+#ifndef ALLOW_EXCEPTIONS_AS_TYPE
if (get_returntype()->get_true_type()->is_xception()) {
failure("method %s(): exception type \"%s\" cannot be used as function return", get_name().c_str(), get_returntype()->get_name().c_str());
}
+#endif
std::vector<t_field*>::const_iterator it;
std::vector<t_field*> list = get_arglist()->get_members();
for(it=list.begin(); it != list.end(); ++it) {
(*it)->get_type()->validate();
+
+#ifndef ALLOW_EXCEPTIONS_AS_TYPE
if( (*it)->get_type()->get_true_type()->is_xception()) {
failure("method %s(): exception type \"%s\" cannot be used as function argument %s", get_name().c_str(), (*it)->get_type()->get_name().c_str(), (*it)->get_name().c_str());
}
+#endif
}
}
diff --git a/compiler/cpp/src/thrift/parse/t_list.h b/compiler/cpp/src/thrift/parse/t_list.h
index 5daa412..162281c 100644
--- a/compiler/cpp/src/thrift/parse/t_list.h
+++ b/compiler/cpp/src/thrift/parse/t_list.h
@@ -35,9 +35,11 @@
bool is_list() const override { return true; }
void validate() const {
+#ifndef ALLOW_EXCEPTIONS_AS_TYPE
if( get_elem_type()->get_true_type()->is_xception()) {
failure("exception type \"%s\" cannot be used inside a list", get_elem_type()->get_name().c_str());
}
+#endif
}
private:
diff --git a/compiler/cpp/src/thrift/parse/t_map.h b/compiler/cpp/src/thrift/parse/t_map.h
index 444fca7..30a8b06 100644
--- a/compiler/cpp/src/thrift/parse/t_map.h
+++ b/compiler/cpp/src/thrift/parse/t_map.h
@@ -38,12 +38,14 @@
bool is_map() const override { return true; }
void validate() const {
+#ifndef ALLOW_EXCEPTIONS_AS_TYPE
if( get_key_type()->get_true_type()->is_xception()) {
failure("exception type \"%s\" cannot be used inside a map", get_key_type()->get_name().c_str());
}
if( get_val_type()->get_true_type()->is_xception()) {
failure("exception type \"%s\" cannot be used inside a map", get_val_type()->get_name().c_str());
}
+#endif
}
private:
diff --git a/compiler/cpp/src/thrift/parse/t_set.h b/compiler/cpp/src/thrift/parse/t_set.h
index 4a02dcc..88de93f 100644
--- a/compiler/cpp/src/thrift/parse/t_set.h
+++ b/compiler/cpp/src/thrift/parse/t_set.h
@@ -37,9 +37,11 @@
bool is_set() const override { return true; }
void validate() const {
+#ifndef ALLOW_EXCEPTIONS_AS_TYPE
if( get_elem_type()->get_true_type()->is_xception()) {
failure("exception type \"%s\" cannot be used inside a set", get_elem_type()->get_name().c_str());
}
+#endif
}
private:
diff --git a/compiler/cpp/src/thrift/parse/t_struct.h b/compiler/cpp/src/thrift/parse/t_struct.h
index 941712d..3aa67c0 100644
--- a/compiler/cpp/src/thrift/parse/t_struct.h
+++ b/compiler/cpp/src/thrift/parse/t_struct.h
@@ -112,9 +112,8 @@
const members_type& get_sorted_members() const { return members_in_id_order_; }
bool is_struct() const override { return !is_xception_; }
-
bool is_xception() const override { return is_xception_; }
-
+ bool is_method_xcepts() const override { return is_method_xcepts_; }
bool is_union() const { return is_union_; }
t_field* get_field_by_name(std::string field_name) {
@@ -144,11 +143,14 @@
std::vector<t_field*> list = get_members();
for(it=list.begin(); it != list.end(); ++it) {
(*it)->get_type()->validate();
+
+#ifndef ALLOW_EXCEPTIONS_AS_TYPE
if (!is_method_xcepts_) { // this is in fact the only legal usage for any exception type
if( (*it)->get_type()->get_true_type()->is_xception()) {
failure("%s %s: exception type \"%s\" cannot be used as member field type %s", what.c_str(), get_name().c_str(), (*it)->get_type()->get_name().c_str(), (*it)->get_name().c_str());
}
}
+#endif
}
}
diff --git a/compiler/cpp/src/thrift/parse/t_type.h b/compiler/cpp/src/thrift/parse/t_type.h
index f408242..d087601 100644
--- a/compiler/cpp/src/thrift/parse/t_type.h
+++ b/compiler/cpp/src/thrift/parse/t_type.h
@@ -26,6 +26,8 @@
#include <stdint.h>
#include <string>
+#define ALLOW_EXCEPTIONS_AS_TYPE
+
class t_program;
/**
@@ -54,6 +56,7 @@
virtual bool is_enum() const { return false; }
virtual bool is_struct() const { return false; }
virtual bool is_xception() const { return false; }
+ virtual bool is_method_xcepts() const { return false; }
virtual bool is_container() const { return false; }
virtual bool is_list() const { return false; }
virtual bool is_set() const { return false; }
diff --git a/test/ExceptionStruct.thrift b/test/ExceptionStruct.thrift
new file mode 100644
index 0000000..b962209
--- /dev/null
+++ b/test/ExceptionStruct.thrift
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+namespace * test.ExceptionStruct
+
+enum ErrorCode {
+ GenericError,
+ ServerOverload,
+ InvalidData
+}
+
+struct GetRequest {
+ 1: string id
+ 2: binary data // some arbitrary data
+}
+
+struct GetResponse {
+ 1: i32 job_nr
+ 2: binary data // some arbitrary data
+}
+
+struct BatchGetRequest {
+ 1: list<GetRequest> requests
+}
+
+struct BatchGetResponse {
+ 1: map<string, GetRequest> responses, // key is id
+ 2: map<string, SomeException> errors, // key is id
+}
+
+exception SomeException {
+ 2: ErrorCode error
+}
+
+service Foo {
+ GetResponse get(1: GetRequest request) throws(1: SomeException error);
+ BatchGetResponse batchGet(1: BatchGetRequest request) throws(1: SomeException error); // may or may not be the same exception type, only throw exception when full request failed
+}
+
+# eof