From 41baeaba96302e480265542340532842bc5ab0c9 Mon Sep 17 00:00:00 2001 From: Kevin Clark Date: Mon, 5 Jan 2009 23:10:17 +0000 Subject: [PATCH] THRIFT-157. rb: Quote strings and qualify class names properly Author: Dave Engberg git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@731763 13f79535-47bb-0310-9956-ffa450edef68 --- compiler/cpp/src/generate/t_rb_generator.cc | 17 ++++++++++++++--- lib/rb/spec/gen-rb/NonblockingService.rb | 2 +- lib/rb/spec/gen-rb/ThriftSpec_types.rb | 10 +++++----- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/compiler/cpp/src/generate/t_rb_generator.cc b/compiler/cpp/src/generate/t_rb_generator.cc index 4d90b108..b2fc5bc7 100644 --- a/compiler/cpp/src/generate/t_rb_generator.cc +++ b/compiler/cpp/src/generate/t_rb_generator.cc @@ -147,6 +147,7 @@ class t_rb_generator : public t_oop_generator { std::string render_includes(); std::string declare_field(t_field* tfield); std::string type_name(t_type* ttype); + std::string full_type_name(t_type* ttype); std::string function_signature(t_function* tfunction, std::string prefix=""); std::string argument_list(t_struct* tstruct); std::string type_to_enum(t_type* ttype); @@ -328,7 +329,7 @@ string t_rb_generator::render_const_value(t_type* type, t_const_value* value) { t_base_type::t_base tbase = ((t_base_type*)type)->get_base(); switch (tbase) { case t_base_type::TYPE_STRING: - out << "'" << value->get_string() << "'"; + out << "%q\"" << value->get_string() << '"'; break; case t_base_type::TYPE_BOOL: out << (value->get_integer() > 0 ? "true" : "false"); @@ -559,7 +560,7 @@ void t_rb_generator::generate_field_data(std::ofstream& out, t_type* field_type, if (!field_type->is_base_type()) { if (field_type->is_struct() || field_type->is_xception()) { - out << ", :class => " << type_name(((t_struct*)field_type)); + out << ", :class => " << full_type_name((t_struct*)field_type); } else if (field_type->is_list()) { out << ", :element => "; generate_field_data(out, ((t_list*)field_type)->get_elem_type()); @@ -905,7 +906,7 @@ void t_rb_generator::generate_process_function(t_service* tservice, indent_down(); for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) { f_service_ << - indent() << "rescue " << (*x_iter)->get_type()->get_name() << " => " << (*x_iter)->get_name() << endl; + indent() << "rescue " << full_type_name((*x_iter)->get_type()) << " => " << (*x_iter)->get_name() << endl; if (!tfunction->is_async()) { indent_up(); f_service_ << @@ -978,6 +979,16 @@ string t_rb_generator::type_name(t_type* ttype) { return prefix + name; } +string t_rb_generator::full_type_name(t_type* ttype) { + string prefix = ""; + vector modules = ruby_modules(ttype->get_program()); + for (vector::iterator m_iter = modules.begin(); + m_iter != modules.end(); ++m_iter) { + prefix += *m_iter + "::"; + } + return prefix + type_name(ttype); +} + /** * Converts the parse type to a Ruby tyoe */ diff --git a/lib/rb/spec/gen-rb/NonblockingService.rb b/lib/rb/spec/gen-rb/NonblockingService.rb index a8868058..8044d8e5 100644 --- a/lib/rb/spec/gen-rb/NonblockingService.rb +++ b/lib/rb/spec/gen-rb/NonblockingService.rb @@ -132,7 +132,7 @@ require File.dirname(__FILE__) + '/ThriftSpec_types' Thrift::Struct.field_accessor self, :success FIELDS = { - SUCCESS => {:type => Thrift::Types::STRUCT, :name => 'success', :class => Hello} + SUCCESS => {:type => Thrift::Types::STRUCT, :name => 'success', :class => SpecNamespace::Hello} } def validate end diff --git a/lib/rb/spec/gen-rb/ThriftSpec_types.rb b/lib/rb/spec/gen-rb/ThriftSpec_types.rb index 613c8de8..a28c9214 100644 --- a/lib/rb/spec/gen-rb/ThriftSpec_types.rb +++ b/lib/rb/spec/gen-rb/ThriftSpec_types.rb @@ -13,7 +13,7 @@ module SpecNamespace Thrift::Struct.field_accessor self, :greeting FIELDS = { - GREETING => {:type => Thrift::Types::STRING, :name => 'greeting', :default => 'hello world'} + GREETING => {:type => Thrift::Types::STRING, :name => 'greeting', :default => %q"hello world"} } def validate end @@ -33,10 +33,10 @@ module SpecNamespace Thrift::Struct.field_accessor self, :simple, :words, :hello, :ints, :complex, :shorts, :opt_string FIELDS = { SIMPLE => {:type => Thrift::Types::I32, :name => 'simple', :default => 53}, - WORDS => {:type => Thrift::Types::STRING, :name => 'words', :default => 'words'}, + WORDS => {:type => Thrift::Types::STRING, :name => 'words', :default => %q"words"}, HELLO => {:type => Thrift::Types::STRUCT, :name => 'hello', :default => Hello.new({ - 'greeting' => 'hello, world!', - }), :class => Hello}, + %q"greeting" => %q"hello, world!", + }), :class => SpecNamespace::Hello}, INTS => {:type => Thrift::Types::LIST, :name => 'ints', :default => [ 1, 2, @@ -94,7 +94,7 @@ module SpecNamespace MAPS => {:type => Thrift::Types::LIST, :name => 'maps', :element => {:type => Thrift::Types::MAP, :key => {:type => Thrift::Types::I16}, :value => {:type => Thrift::Types::I16}}}, LISTS => {:type => Thrift::Types::LIST, :name => 'lists', :element => {:type => Thrift::Types::LIST, :element => {:type => Thrift::Types::I16}}}, SETS => {:type => Thrift::Types::LIST, :name => 'sets', :element => {:type => Thrift::Types::SET, :element => {:type => Thrift::Types::I16}}}, - HELLOS => {:type => Thrift::Types::LIST, :name => 'hellos', :element => {:type => Thrift::Types::STRUCT, :class => Hello}} + HELLOS => {:type => Thrift::Types::LIST, :name => 'hellos', :element => {:type => Thrift::Types::STRUCT, :class => SpecNamespace::Hello}} } def validate end -- 2.17.1