From d6b7182df59ae7cb1849c36b5d9513fce681ce35 Mon Sep 17 00:00:00 2001 From: Kevin Clark Date: Tue, 6 Jan 2009 01:34:28 +0000 Subject: [PATCH] THRIFT-246. rb: Generate #struct_fields rather than reflect. Author: Bryan Duxbury git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@731806 13f79535-47bb-0310-9956-ffa450edef68 --- compiler/cpp/src/generate/t_rb_generator.cc | 5 +++- lib/rb/benchmark/gen-rb/BenchmarkService.rb | 6 +++++ lib/rb/lib/thrift/struct.rb | 2 ++ lib/rb/spec/gen-rb/NonblockingService.rb | 30 +++++++++++++++++++++ lib/rb/spec/gen-rb/ThriftSpec_types.rb | 15 +++++++++++ 5 files changed, 57 insertions(+), 1 deletion(-) diff --git a/compiler/cpp/src/generate/t_rb_generator.cc b/compiler/cpp/src/generate/t_rb_generator.cc index b2fc5bc7..e3c3031f 100644 --- a/compiler/cpp/src/generate/t_rb_generator.cc +++ b/compiler/cpp/src/generate/t_rb_generator.cc @@ -540,7 +540,10 @@ void t_rb_generator::generate_field_defns(std::ofstream& out, t_struct* tstruct) } indent_down(); out << endl; - indent(out) << "}" << endl; + indent(out) << "}" << endl << endl; + + indent(out) << "def struct_fields; FIELDS; end" << endl << endl; + } void t_rb_generator::generate_field_data(std::ofstream& out, t_type* field_type, diff --git a/lib/rb/benchmark/gen-rb/BenchmarkService.rb b/lib/rb/benchmark/gen-rb/BenchmarkService.rb index 2c3ae4f6..832098b5 100644 --- a/lib/rb/benchmark/gen-rb/BenchmarkService.rb +++ b/lib/rb/benchmark/gen-rb/BenchmarkService.rb @@ -52,6 +52,9 @@ require File.dirname(__FILE__) + '/Benchmark_types' FIELDS = { N => {:type => Thrift::Types::BYTE, :name => 'n'} } + + def struct_fields; FIELDS; end + def validate end @@ -65,6 +68,9 @@ require File.dirname(__FILE__) + '/Benchmark_types' FIELDS = { SUCCESS => {:type => Thrift::Types::I32, :name => 'success'} } + + def struct_fields; FIELDS; end + def validate end diff --git a/lib/rb/lib/thrift/struct.rb b/lib/rb/lib/thrift/struct.rb index 25eb9819..d790bd43 100644 --- a/lib/rb/lib/thrift/struct.rb +++ b/lib/rb/lib/thrift/struct.rb @@ -60,6 +60,8 @@ module Thrift names_to_ids[name] end + # Obsoleted by THRIFT-246, which generates this method inline + # TODO: Should be removed at some point. -- Kevin Clark def struct_fields self.class.const_get(:FIELDS) end diff --git a/lib/rb/spec/gen-rb/NonblockingService.rb b/lib/rb/spec/gen-rb/NonblockingService.rb index 8044d8e5..3b49fce1 100644 --- a/lib/rb/spec/gen-rb/NonblockingService.rb +++ b/lib/rb/spec/gen-rb/NonblockingService.rb @@ -121,6 +121,9 @@ require File.dirname(__FILE__) + '/ThriftSpec_types' FIELDS = { ENGLISH => {:type => Thrift::Types::BOOL, :name => 'english'} } + + def struct_fields; FIELDS; end + def validate end @@ -134,6 +137,9 @@ require File.dirname(__FILE__) + '/ThriftSpec_types' FIELDS = { SUCCESS => {:type => Thrift::Types::STRUCT, :name => 'success', :class => SpecNamespace::Hello} } + + def struct_fields; FIELDS; end + def validate end @@ -145,6 +151,9 @@ require File.dirname(__FILE__) + '/ThriftSpec_types' FIELDS = { } + + def struct_fields; FIELDS; end + def validate end @@ -158,6 +167,9 @@ require File.dirname(__FILE__) + '/ThriftSpec_types' FIELDS = { SUCCESS => {:type => Thrift::Types::BOOL, :name => 'success'} } + + def struct_fields; FIELDS; end + def validate end @@ -171,6 +183,9 @@ require File.dirname(__FILE__) + '/ThriftSpec_types' FIELDS = { N => {:type => Thrift::Types::I32, :name => 'n'} } + + def struct_fields; FIELDS; end + def validate end @@ -182,6 +197,9 @@ require File.dirname(__FILE__) + '/ThriftSpec_types' FIELDS = { } + + def struct_fields; FIELDS; end + def validate end @@ -193,6 +211,9 @@ require File.dirname(__FILE__) + '/ThriftSpec_types' FIELDS = { } + + def struct_fields; FIELDS; end + def validate end @@ -204,6 +225,9 @@ require File.dirname(__FILE__) + '/ThriftSpec_types' FIELDS = { } + + def struct_fields; FIELDS; end + def validate end @@ -217,6 +241,9 @@ require File.dirname(__FILE__) + '/ThriftSpec_types' FIELDS = { SECONDS => {:type => Thrift::Types::DOUBLE, :name => 'seconds'} } + + def struct_fields; FIELDS; end + def validate end @@ -228,6 +255,9 @@ require File.dirname(__FILE__) + '/ThriftSpec_types' FIELDS = { } + + def struct_fields; FIELDS; end + def validate end diff --git a/lib/rb/spec/gen-rb/ThriftSpec_types.rb b/lib/rb/spec/gen-rb/ThriftSpec_types.rb index a28c9214..ea73af12 100644 --- a/lib/rb/spec/gen-rb/ThriftSpec_types.rb +++ b/lib/rb/spec/gen-rb/ThriftSpec_types.rb @@ -15,6 +15,9 @@ module SpecNamespace FIELDS = { GREETING => {:type => Thrift::Types::STRING, :name => 'greeting', :default => %q"hello world"} } + + def struct_fields; FIELDS; end + def validate end @@ -50,6 +53,9 @@ module SpecNamespace ]), :element => {:type => Thrift::Types::I16}}, OPT_STRING => {:type => Thrift::Types::STRING, :name => 'opt_string', :optional => true} } + + def struct_fields; FIELDS; end + def validate end @@ -63,6 +69,9 @@ module SpecNamespace FIELDS = { YESNO => {:type => Thrift::Types::BOOL, :name => 'yesno', :default => true} } + + def struct_fields; FIELDS; end + def validate end @@ -96,6 +105,9 @@ module SpecNamespace 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 => SpecNamespace::Hello}} } + + def struct_fields; FIELDS; end + def validate end @@ -111,6 +123,9 @@ module SpecNamespace MESSAGE => {:type => Thrift::Types::STRING, :name => 'message'}, CODE => {:type => Thrift::Types::I32, :name => 'code', :default => 1} } + + def struct_fields; FIELDS; end + def validate end -- 2.17.1