You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by kc...@apache.org on 2008/08/26 22:02:07 UTC

svn commit: r689193 - in /incubator/thrift/trunk: compiler/cpp/src/generate/t_rb_generator.cc lib/rb/lib/thrift/struct.rb lib/rb/spec/ThriftSpec.thrift lib/rb/spec/gen-rb/ThriftSpec_types.rb lib/rb/spec/struct_spec.rb

Author: kclark
Date: Tue Aug 26 13:02:07 2008
New Revision: 689193

URL: http://svn.apache.org/viewvc?rev=689193&view=rev
Log:
rb: Add pretty inspect, optional field hint for Thrift::Struct

Author: Bryan Duxbury

Modified:
    incubator/thrift/trunk/compiler/cpp/src/generate/t_rb_generator.cc
    incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb
    incubator/thrift/trunk/lib/rb/spec/ThriftSpec.thrift
    incubator/thrift/trunk/lib/rb/spec/gen-rb/ThriftSpec_types.rb
    incubator/thrift/trunk/lib/rb/spec/struct_spec.rb

Modified: incubator/thrift/trunk/compiler/cpp/src/generate/t_rb_generator.cc
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/compiler/cpp/src/generate/t_rb_generator.cc?rev=689193&r1=689192&r2=689193&view=diff
==============================================================================
--- incubator/thrift/trunk/compiler/cpp/src/generate/t_rb_generator.cc (original)
+++ incubator/thrift/trunk/compiler/cpp/src/generate/t_rb_generator.cc Tue Aug 26 13:02:07 2008
@@ -69,7 +69,7 @@
   void generate_rb_simple_exception_constructor(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);
+  void generate_field_data  (std::ofstream& out, t_type* field_type, const std::string& field_name, t_const_value* field_value, bool optional);
 
   /**
    * Service-level generation functions
@@ -512,7 +512,8 @@
     indent(out) <<
       (*f_iter)->get_key() << " => ";
 
-    generate_field_data(out, (*f_iter)->get_type(), (*f_iter)->get_name(), (*f_iter)->get_value());
+    generate_field_data(out, (*f_iter)->get_type(), (*f_iter)->get_name(), (*f_iter)->get_value(), 
+      (*f_iter)->get_req() == t_field::T_OPTIONAL);
   }
   indent_down();
   out << endl;
@@ -520,7 +521,7 @@
 }
 
 void t_rb_generator::generate_field_data(std::ofstream& out, t_type* field_type,
-    const std::string& field_name = "", t_const_value* field_value = NULL) {
+    const std::string& field_name = "", t_const_value* field_value = NULL, bool optional = false) {
   field_type = get_true_type(field_type);
 
   // Begin this field's defn
@@ -550,6 +551,10 @@
       generate_field_data(out, ((t_set*)field_type)->get_elem_type());
     }
   }
+  
+  if(optional) {
+    out << ", :optional => true";
+  }
 
   // End of this field's defn
   out << "}";

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb?rev=689193&r1=689192&r2=689193&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb Tue Aug 26 13:02:07 2008
@@ -57,10 +57,21 @@
 
     def each_field
       struct_fields.each do |fid, data|
-        yield fid, data[:type], data[:name], data[:default]
+        yield fid, data[:type], data[:name], data[:default], data[:optional]
       end
     end
 
+    def inspect(skip_optional_nulls = true)
+      fields = []
+      each_field do |fid, type, name, default, optional|
+        value = instance_variable_get("@#{name}")
+        unless skip_optional_nulls && optional && value.nil?
+          fields << "#{name}:#{value.inspect}"
+        end
+      end
+      "<#{self.class} #{fields.join(", ")}>"
+    end
+
     def read(iprot)
       # TODO(kevinclark): Make sure transport is C readable
       if iprot.respond_to?(:decode_binary)

Modified: incubator/thrift/trunk/lib/rb/spec/ThriftSpec.thrift
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/spec/ThriftSpec.thrift?rev=689193&r1=689192&r2=689193&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/spec/ThriftSpec.thrift (original)
+++ incubator/thrift/trunk/lib/rb/spec/ThriftSpec.thrift Tue Aug 26 13:02:07 2008
@@ -10,7 +10,8 @@
   3: Hello hello = {'greeting' : "hello, world!"},
   4: list<i32> ints = [1, 2, 2, 3],
   5: map<i32, map<string, double>> complex,
-  6: set<i16> shorts = [5, 17, 239]
+  6: set<i16> shorts = [5, 17, 239],
+  7: optional string opt_string
 }
 
 struct BoolStruct {

Modified: incubator/thrift/trunk/lib/rb/spec/gen-rb/ThriftSpec_types.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/spec/gen-rb/ThriftSpec_types.rb?rev=689193&r1=689192&r2=689193&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/spec/gen-rb/ThriftSpec_types.rb (original)
+++ incubator/thrift/trunk/lib/rb/spec/gen-rb/ThriftSpec_types.rb Tue Aug 26 13:02:07 2008
@@ -17,7 +17,7 @@
 
     class Foo
       include Thrift::Struct
-      Thrift::Struct.field_accessor self, :simple, :words, :hello, :ints, :complex, :shorts
+      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'},
@@ -34,7 +34,8 @@
         6 => {:type => Thrift::Types::SET, :name => 'shorts', :default => Set.new([          5,
           17,
           239,
-        ]), :element => {:type => Thrift::Types::I16}}
+        ]), :element => {:type => Thrift::Types::I16}},
+        7 => {:type => Thrift::Types::STRING, :name => 'opt_string', :optional => true}
       }
     end
 

Modified: incubator/thrift/trunk/lib/rb/spec/struct_spec.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/spec/struct_spec.rb?rev=689193&r1=689192&r2=689193&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/spec/struct_spec.rb (original)
+++ incubator/thrift/trunk/lib/rb/spec/struct_spec.rb Tue Aug 26 13:02:07 2008
@@ -17,14 +17,15 @@
   describe Struct do
     it "should iterate over all fields properly" do
       fields = {}
-      Foo.new.each_field { |fid,type,name,default| fields[fid] = [type,name,default] }
+      Foo.new.each_field { |fid,type,name,default,optional| fields[fid] = [type,name,default,optional] }
       fields.should == {
-        1 => [Types::I32, 'simple', 53],
-        2 => [Types::STRING, 'words', "words"],
-        3 => [Types::STRUCT, 'hello', Hello.new(:greeting => 'hello, world!')],
-        4 => [Types::LIST, 'ints', [1, 2, 2, 3]],
-        5 => [Types::MAP, 'complex', nil],
-        6 => [Types::SET, 'shorts', Set.new([5, 17, 239])]
+        1 => [Types::I32, 'simple', 53, nil],
+        2 => [Types::STRING, 'words', "words", nil],
+        3 => [Types::STRUCT, 'hello', Hello.new(:greeting => 'hello, world!'), nil],
+        4 => [Types::LIST, 'ints', [1, 2, 2, 3], nil],
+        5 => [Types::MAP, 'complex', nil, nil],
+        6 => [Types::SET, 'shorts', Set.new([5, 17, 239]), nil],
+        7 => [Types::STRING, 'opt_string', nil, true]
       }
     end