From: Mark Slee Date: Tue, 6 Feb 2007 21:03:18 +0000 (+0000) Subject: Thrift support for the xsd_nillable attribute X-Git-Tag: 0.2.0~1491 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=7df0e2a9aa61e24a5121b24f0cac60d8a858a88b;p=common%2Fthrift.git Thrift support for the xsd_nillable attribute Reviewed By: dave git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664981 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/compiler/cpp/src/generate/t_xsd_generator.cc b/compiler/cpp/src/generate/t_xsd_generator.cc index 49550b15..06803ec9 100644 --- a/compiler/cpp/src/generate/t_xsd_generator.cc +++ b/compiler/cpp/src/generate/t_xsd_generator.cc @@ -48,7 +48,7 @@ void t_xsd_generator::generate_struct(t_struct* tstruct) { indent_up(); for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { - generate_element(s_xsd_types_, (*m_iter)->get_name(), (*m_iter)->get_type(), (*m_iter)->get_xsd_attrs(), (*m_iter)->get_xsd_optional() || xsd_all); + generate_element(s_xsd_types_, (*m_iter)->get_name(), (*m_iter)->get_type(), (*m_iter)->get_xsd_attrs(), (*m_iter)->get_xsd_optional() || xsd_all, (*m_iter)->get_xsd_nillable()); } indent_down(); @@ -68,14 +68,16 @@ void t_xsd_generator::generate_element(ostream& out, t_type* ttype, vector attrs, bool optional, + bool nillable, bool list_element) { string sminOccurs = (optional || list_element) ? " minOccurs=\"0\"" : ""; string smaxOccurs = list_element ? " maxOccurs=\"unbounded\"" : ""; string soptional = sminOccurs + smaxOccurs; + string snillable = nillable ? " nillable=\"true\"" : ""; if (ttype->is_void() || ttype->is_list()) { indent(out) << - "" << endl; + "" << endl; indent_up(); if (attrs.size() == 0 && ttype->is_void()) { indent(out) << @@ -83,15 +85,8 @@ void t_xsd_generator::generate_element(ostream& out, } else { indent(out) << "" << endl; - indent_up(); - - vector::iterator a_iter; - for (a_iter = attrs.begin(); a_iter != attrs.end(); ++a_iter) { - indent(out) << "" << endl; - } - + indent_up(); if (ttype->is_list()) { - indent(out) << "" << endl; indent(out) << "" << endl; indent_up(); string subname; @@ -102,9 +97,14 @@ void t_xsd_generator::generate_element(ostream& out, subname = type_name(subtype); } f_php_ << "$GLOBALS['" << program_->get_name() << "_xsd_elt_" << name << "'] = '" << subname << "';" << endl; - generate_element(out, subname, subtype, vector(), false, true); + generate_element(out, subname, subtype, vector(), false, false, true); indent_down(); indent(out) << "" << endl; + indent(out) << "" << endl; + } + vector::iterator a_iter; + for (a_iter = attrs.begin(); a_iter != attrs.end(); ++a_iter) { + indent(out) << "" << endl; } indent_down(); indent(out) << @@ -116,10 +116,10 @@ void t_xsd_generator::generate_element(ostream& out, } else { if (attrs.size() == 0) { indent(out) << - "" << endl; + "" << endl; } else { // Wow, all this work for a SIMPLE TYPE with attributes?!?!?! - indent(out) << "" << endl; + indent(out) << "" << endl; indent_up(); indent(out) << "" << endl; indent_up(); diff --git a/compiler/cpp/src/generate/t_xsd_generator.h b/compiler/cpp/src/generate/t_xsd_generator.h index 7a602835..21e74fb1 100644 --- a/compiler/cpp/src/generate/t_xsd_generator.h +++ b/compiler/cpp/src/generate/t_xsd_generator.h @@ -40,7 +40,7 @@ class t_xsd_generator : public t_generator { private: - void generate_element(std::ostream& out, std::string name, t_type* ttype, std::vector attrs=std::vector(), bool optional=false, bool list_element=false); + void generate_element(std::ostream& out, std::string name, t_type* ttype, std::vector attrs=std::vector(), bool optional=false, bool nillable=false, bool list_element=false); std::string ns(std::string in, std::string ns) { return ns + ":" + in; diff --git a/compiler/cpp/src/parse/t_field.h b/compiler/cpp/src/parse/t_field.h index 090426a7..dfa5cacf 100644 --- a/compiler/cpp/src/parse/t_field.h +++ b/compiler/cpp/src/parse/t_field.h @@ -16,14 +16,16 @@ class t_field { name_(name), key_(0), value_(NULL), - xsd_optional_(false) {} + xsd_optional_(false), + xsd_nillable_(false) {} t_field(t_type* type, std::string name, int32_t key) : type_(type), name_(name), key_(key), value_(NULL), - xsd_optional_(false) {} + xsd_optional_(false), + xsd_nillable_(false) {} ~t_field() {} @@ -55,6 +57,14 @@ class t_field { return xsd_optional_; } + void set_xsd_nillable(bool xsd_nillable) { + xsd_nillable_ = xsd_nillable; + } + + bool get_xsd_nillable() const { + return xsd_nillable_; + } + void add_xsd_attr(std::string attr) { xsd_attrs_.push_back(attr); } @@ -83,6 +93,7 @@ class t_field { t_const_value* value_; bool xsd_optional_; + bool xsd_nillable_; std::vector xsd_attrs_; std::string doc_; diff --git a/compiler/cpp/src/thriftl.ll b/compiler/cpp/src/thriftl.ll index 5486adac..593df107 100644 --- a/compiler/cpp/src/thriftl.ll +++ b/compiler/cpp/src/thriftl.ll @@ -63,6 +63,7 @@ sliteral ("'"[^']*"'") "php_namespace" { return tok_php_namespace; } "xsd_all" { return tok_xsd_all; } "xsd_optional" { return tok_xsd_optional; } +"xsd_nillable" { return tok_xsd_nillable; } "xsd_namespace" { return tok_xsd_namespace; } "xsd_attrs" { return tok_xsd_attrs; } "include" { return tok_include; } diff --git a/compiler/cpp/src/thrifty.yy b/compiler/cpp/src/thrifty.yy index a53d0023..c1d383b6 100644 --- a/compiler/cpp/src/thrifty.yy +++ b/compiler/cpp/src/thrifty.yy @@ -70,6 +70,7 @@ int y_field_val = -1; %token tok_java_package %token tok_xsd_all %token tok_xsd_optional +%token tok_xsd_nillable %token tok_xsd_namespace %token tok_xsd_attrs @@ -155,6 +156,7 @@ int y_field_val = -1; %type Async %type XsdAll %type XsdOptional +%type XsdNillable %type XsdAttributes %type CppType @@ -517,6 +519,16 @@ XsdOptional: $$ = false; } +XsdNillable: + tok_xsd_nillable + { + $$ = true; + } +| + { + $$ = false; + } + XsdAttributes: tok_xsd_attrs tok_identifier { @@ -631,7 +643,7 @@ FieldList: } Field: - DocTextOptional FieldIdentifier FieldType tok_identifier FieldValue XsdOptional XsdAttributes CommaOrSemicolonOptional + DocTextOptional FieldIdentifier FieldType tok_identifier FieldValue XsdOptional XsdNillable XsdAttributes CommaOrSemicolonOptional { pdebug("tok_int_constant : Field -> FieldType tok_identifier"); if ($2 < 0) { @@ -643,11 +655,12 @@ Field: $$->set_value($5); } $$->set_xsd_optional($6); + $$->set_xsd_nillable($7); if ($1 != NULL) { $$->set_doc($1); } - if ($7 != NULL) { - $$->add_xsd_attr($7); + if ($8 != NULL) { + $$->add_xsd_attr($8); } }