rb: Create constants for field ids in generated structs [THRIFT-165]
Author: Bryan Duxbury
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@705330 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/compiler/cpp/src/generate/t_java_generator.cc b/compiler/cpp/src/generate/t_java_generator.cc
index e8d9a95..60ad2fb 100644
--- a/compiler/cpp/src/generate/t_java_generator.cc
+++ b/compiler/cpp/src/generate/t_java_generator.cc
@@ -44,12 +44,6 @@
out_dir_base_ = (bean_style_ ? "gen-javabean" : "gen-java");
}
- string upcase_string(string original) {
- std::transform(original.begin(), original.end(), original.begin(), (int(*)(int)) toupper);
- return original;
- }
-
-
/**
* Init and close methods
*/
diff --git a/compiler/cpp/src/generate/t_oop_generator.h b/compiler/cpp/src/generate/t_oop_generator.h
index 8cba67f..3150954 100644
--- a/compiler/cpp/src/generate/t_oop_generator.h
+++ b/compiler/cpp/src/generate/t_oop_generator.h
@@ -35,6 +35,11 @@
indent(out) << "}" << std::endl;
}
+ std::string upcase_string(std::string original) {
+ std::transform(original.begin(), original.end(), original.begin(), (int(*)(int)) toupper);
+ return original;
+ }
+
/**
* Generates a comment about this code being autogenerated, using C++ style
* comments, which are also fair game in Java / PHP, yay!
diff --git a/compiler/cpp/src/generate/t_rb_generator.cc b/compiler/cpp/src/generate/t_rb_generator.cc
index acc87e6..570b4a7 100644
--- a/compiler/cpp/src/generate/t_rb_generator.cc
+++ b/compiler/cpp/src/generate/t_rb_generator.cc
@@ -67,6 +67,7 @@
void generate_rb_function_helpers(t_function* tfunction);
void generate_rb_simple_constructor(std::ofstream& out, t_struct* tstruct);
void generate_rb_simple_exception_constructor(std::ofstream& out, t_struct* tstruct);
+ void generate_field_constants (std::ofstream& out, t_struct* tstruct);
void generate_accessors (std::ofstream& out, t_struct* tstruct);
void generate_field_defns (std::ofstream& out, t_struct* tstruct);
void generate_field_data (std::ofstream& out, t_type* field_type, const std::string& field_name, t_const_value* field_value, bool optional);
@@ -455,6 +456,7 @@
generate_rb_simple_exception_constructor(out, tstruct);
}
+ generate_field_constants(out, tstruct);
generate_accessors(out, tstruct);
generate_field_defns(out, tstruct);
@@ -485,6 +487,19 @@
}
}
+void t_rb_generator::generate_field_constants(std::ofstream& out, t_struct* tstruct) {
+ const vector<t_field*>& fields = tstruct->get_members();
+ vector<t_field*>::const_iterator f_iter;
+
+ for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+ std::string field_name = (*f_iter)->get_name();
+ std::string cap_field_name = upcase_string(field_name);
+
+ indent(out) << cap_field_name << " = " << (*f_iter)->get_key() << endl;
+ }
+ out << endl;
+}
+
void t_rb_generator::generate_accessors(std::ofstream& out, t_struct* tstruct) {
const vector<t_field*>& members = tstruct->get_members();
vector<t_field*>::const_iterator m_iter;
@@ -510,7 +525,7 @@
}
indent(out) <<
- (*f_iter)->get_key() << " => ";
+ upcase_string((*f_iter)->get_name()) << " => ";
generate_field_data(out, (*f_iter)->get_type(), (*f_iter)->get_name(), (*f_iter)->get_value(),
(*f_iter)->get_req() == t_field::T_OPTIONAL);
diff --git a/lib/rb/benchmark/gen-rb/BenchmarkService.rb b/lib/rb/benchmark/gen-rb/BenchmarkService.rb
index 42c8aec..7d39882 100644
--- a/lib/rb/benchmark/gen-rb/BenchmarkService.rb
+++ b/lib/rb/benchmark/gen-rb/BenchmarkService.rb
@@ -46,17 +46,21 @@
class Fibonacci_args
include Thrift::Struct
+ N = 1
+
Thrift::Struct.field_accessor self, :n
FIELDS = {
- 1 => {:type => Thrift::Types::BYTE, :name => 'n'}
+ N => {:type => Thrift::Types::BYTE, :name => 'n'}
}
end
class Fibonacci_result
include Thrift::Struct
+ SUCCESS = 0
+
Thrift::Struct.field_accessor self, :success
FIELDS = {
- 0 => {:type => Thrift::Types::I32, :name => 'success'}
+ SUCCESS => {:type => Thrift::Types::I32, :name => 'success'}
}
end
diff --git a/lib/rb/spec/gen-rb/NonblockingService.rb b/lib/rb/spec/gen-rb/NonblockingService.rb
index 5893f74..ee5fae1 100644
--- a/lib/rb/spec/gen-rb/NonblockingService.rb
+++ b/lib/rb/spec/gen-rb/NonblockingService.rb
@@ -115,22 +115,27 @@
class Greeting_args
include Thrift::Struct
+ ENGLISH = 1
+
Thrift::Struct.field_accessor self, :english
FIELDS = {
- 1 => {:type => Thrift::Types::BOOL, :name => 'english'}
+ ENGLISH => {:type => Thrift::Types::BOOL, :name => 'english'}
}
end
class Greeting_result
include Thrift::Struct
+ SUCCESS = 0
+
Thrift::Struct.field_accessor self, :success
FIELDS = {
- 0 => {:type => Thrift::Types::STRUCT, :name => 'success', :class => Hello}
+ SUCCESS => {:type => Thrift::Types::STRUCT, :name => 'success', :class => Hello}
}
end
class Block_args
include Thrift::Struct
+
FIELDS = {
}
@@ -138,22 +143,27 @@
class Block_result
include Thrift::Struct
+ SUCCESS = 0
+
Thrift::Struct.field_accessor self, :success
FIELDS = {
- 0 => {:type => Thrift::Types::BOOL, :name => 'success'}
+ SUCCESS => {:type => Thrift::Types::BOOL, :name => 'success'}
}
end
class Unblock_args
include Thrift::Struct
+ N = 1
+
Thrift::Struct.field_accessor self, :n
FIELDS = {
- 1 => {:type => Thrift::Types::I32, :name => 'n'}
+ N => {:type => Thrift::Types::I32, :name => 'n'}
}
end
class Unblock_result
include Thrift::Struct
+
FIELDS = {
}
@@ -161,6 +171,7 @@
class Shutdown_args
include Thrift::Struct
+
FIELDS = {
}
@@ -168,6 +179,7 @@
class Shutdown_result
include Thrift::Struct
+
FIELDS = {
}
@@ -175,14 +187,17 @@
class Sleep_args
include Thrift::Struct
+ SECONDS = 1
+
Thrift::Struct.field_accessor self, :seconds
FIELDS = {
- 1 => {:type => Thrift::Types::DOUBLE, :name => 'seconds'}
+ SECONDS => {:type => Thrift::Types::DOUBLE, :name => 'seconds'}
}
end
class Sleep_result
include Thrift::Struct
+
FIELDS = {
}
diff --git a/lib/rb/spec/gen-rb/ThriftSpec_types.rb b/lib/rb/spec/gen-rb/ThriftSpec_types.rb
index d7ad600..0251524 100644
--- a/lib/rb/spec/gen-rb/ThriftSpec_types.rb
+++ b/lib/rb/spec/gen-rb/ThriftSpec_types.rb
@@ -9,59 +9,83 @@
module SpecNamespace
class Hello
include Thrift::Struct
+ GREETING = 1
+
Thrift::Struct.field_accessor self, :greeting
FIELDS = {
- 1 => {:type => Thrift::Types::STRING, :name => 'greeting', :default => 'hello world'}
+ GREETING => {:type => Thrift::Types::STRING, :name => 'greeting', :default => 'hello world'}
}
end
class Foo
include Thrift::Struct
+ SIMPLE = 1
+ WORDS = 2
+ HELLO = 3
+ INTS = 4
+ COMPLEX = 5
+ SHORTS = 6
+ OPT_STRING = 7
+
Thrift::Struct.field_accessor self, :simple, :words, :hello, :ints, :complex, :shorts, :opt_string
FIELDS = {
- 1 => {:type => Thrift::Types::I32, :name => 'simple', :default => 53},
- 2 => {:type => Thrift::Types::STRING, :name => 'words', :default => 'words'},
- 3 => {:type => Thrift::Types::STRUCT, :name => 'hello', :default => Hello.new({
+ SIMPLE => {:type => Thrift::Types::I32, :name => 'simple', :default => 53},
+ WORDS => {:type => Thrift::Types::STRING, :name => 'words', :default => 'words'},
+ HELLO => {:type => Thrift::Types::STRUCT, :name => 'hello', :default => Hello.new({
'greeting' => 'hello, world!',
}), :class => Hello},
- 4 => {:type => Thrift::Types::LIST, :name => 'ints', :default => [
+ INTS => {:type => Thrift::Types::LIST, :name => 'ints', :default => [
1,
2,
2,
3,
], :element => {:type => Thrift::Types::I32}},
- 5 => {:type => Thrift::Types::MAP, :name => 'complex', :key => {:type => Thrift::Types::I32}, :value => {:type => Thrift::Types::MAP, :key => {:type => Thrift::Types::STRING}, :value => {:type => Thrift::Types::DOUBLE}}},
- 6 => {:type => Thrift::Types::SET, :name => 'shorts', :default => Set.new([ 5,
+ COMPLEX => {:type => Thrift::Types::MAP, :name => 'complex', :key => {:type => Thrift::Types::I32}, :value => {:type => Thrift::Types::MAP, :key => {:type => Thrift::Types::STRING}, :value => {:type => Thrift::Types::DOUBLE}}},
+ SHORTS => {:type => Thrift::Types::SET, :name => 'shorts', :default => Set.new([ 5,
17,
239,
]), :element => {:type => Thrift::Types::I16}},
- 7 => {:type => Thrift::Types::STRING, :name => 'opt_string', :optional => true}
+ OPT_STRING => {:type => Thrift::Types::STRING, :name => 'opt_string', :optional => true}
}
end
class BoolStruct
include Thrift::Struct
+ YESNO = 1
+
Thrift::Struct.field_accessor self, :yesno
FIELDS = {
- 1 => {:type => Thrift::Types::BOOL, :name => 'yesno', :default => true}
+ YESNO => {:type => Thrift::Types::BOOL, :name => 'yesno', :default => true}
}
end
class SimpleList
include Thrift::Struct
+ BOOLS = 1
+ BYTES = 2
+ I16S = 3
+ I32S = 4
+ I64S = 5
+ DOUBLES = 6
+ STRINGS = 7
+ MAPS = 8
+ LISTS = 9
+ SETS = 10
+ HELLOS = 11
+
Thrift::Struct.field_accessor self, :bools, :bytes, :i16s, :i32s, :i64s, :doubles, :strings, :maps, :lists, :sets, :hellos
FIELDS = {
- 1 => {:type => Thrift::Types::LIST, :name => 'bools', :element => {:type => Thrift::Types::BOOL}},
- 2 => {:type => Thrift::Types::LIST, :name => 'bytes', :element => {:type => Thrift::Types::BYTE}},
- 3 => {:type => Thrift::Types::LIST, :name => 'i16s', :element => {:type => Thrift::Types::I16}},
- 4 => {:type => Thrift::Types::LIST, :name => 'i32s', :element => {:type => Thrift::Types::I32}},
- 5 => {:type => Thrift::Types::LIST, :name => 'i64s', :element => {:type => Thrift::Types::I64}},
- 6 => {:type => Thrift::Types::LIST, :name => 'doubles', :element => {:type => Thrift::Types::DOUBLE}},
- 7 => {:type => Thrift::Types::LIST, :name => 'strings', :element => {:type => Thrift::Types::STRING}},
- 8 => {:type => Thrift::Types::LIST, :name => 'maps', :element => {:type => Thrift::Types::MAP, :key => {:type => Thrift::Types::I16}, :value => {:type => Thrift::Types::I16}}},
- 9 => {:type => Thrift::Types::LIST, :name => 'lists', :element => {:type => Thrift::Types::LIST, :element => {:type => Thrift::Types::I16}}},
- 10 => {:type => Thrift::Types::LIST, :name => 'sets', :element => {:type => Thrift::Types::SET, :element => {:type => Thrift::Types::I16}}},
- 11 => {:type => Thrift::Types::LIST, :name => 'hellos', :element => {:type => Thrift::Types::STRUCT, :class => Hello}}
+ BOOLS => {:type => Thrift::Types::LIST, :name => 'bools', :element => {:type => Thrift::Types::BOOL}},
+ BYTES => {:type => Thrift::Types::LIST, :name => 'bytes', :element => {:type => Thrift::Types::BYTE}},
+ I16S => {:type => Thrift::Types::LIST, :name => 'i16s', :element => {:type => Thrift::Types::I16}},
+ I32S => {:type => Thrift::Types::LIST, :name => 'i32s', :element => {:type => Thrift::Types::I32}},
+ I64S => {:type => Thrift::Types::LIST, :name => 'i64s', :element => {:type => Thrift::Types::I64}},
+ DOUBLES => {:type => Thrift::Types::LIST, :name => 'doubles', :element => {:type => Thrift::Types::DOUBLE}},
+ STRINGS => {:type => Thrift::Types::LIST, :name => 'strings', :element => {:type => Thrift::Types::STRING}},
+ 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}}
}
end