From 8d79e3f9544d4b8d49b9ef6e596970a7c1f3e459 Mon Sep 17 00:00:00 2001 From: Kevin Clark Date: Wed, 18 Jun 2008 01:09:15 +0000 Subject: [PATCH] Convert fields of type Thrift::Types::SET to use the Set library. Also teach Thrift::Struct how to compare itself with == git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@668959 13f79535-47bb-0310-9956-ffa450edef68 --- compiler/cpp/src/generate/t_rb_generator.cc | 7 ++----- lib/rb/lib/thrift/protocol.rb | 3 +++ lib/rb/lib/thrift/struct.rb | 12 ++++++++++-- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/compiler/cpp/src/generate/t_rb_generator.cc b/compiler/cpp/src/generate/t_rb_generator.cc index de55c39a..16cbe081 100644 --- a/compiler/cpp/src/generate/t_rb_generator.cc +++ b/compiler/cpp/src/generate/t_rb_generator.cc @@ -397,7 +397,7 @@ string t_rb_generator::render_const_value(t_type* type, t_const_value* value) { etype = ((t_set*)type)->get_elem_type(); } if (type->is_set()) { - out << "{"; + out << "Set.new(["; } else { out << "[" << endl; } @@ -407,14 +407,11 @@ string t_rb_generator::render_const_value(t_type* type, t_const_value* value) { for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) { out << indent(); out << render_const_value(etype, *v_iter); - if (type->is_set()) { - out << " => true"; - } out << "," << endl; } indent_down(); if (type->is_set()) { - indent(out) << "}"; + indent(out) << "])"; } else { indent(out) << "]"; } diff --git a/lib/rb/lib/thrift/protocol.rb b/lib/rb/lib/thrift/protocol.rb index c4dd7624..b5143817 100644 --- a/lib/rb/lib/thrift/protocol.rb +++ b/lib/rb/lib/thrift/protocol.rb @@ -8,6 +8,9 @@ # Author: Mark Slee # +# this require is to make generated struct definitions happy +require 'set' + module Thrift class ProtocolException < Exception diff --git a/lib/rb/lib/thrift/struct.rb b/lib/rb/lib/thrift/struct.rb index ce113bf9..be46341f 100644 --- a/lib/rb/lib/thrift/struct.rb +++ b/lib/rb/lib/thrift/struct.rb @@ -44,6 +44,14 @@ module Thrift oprot.write_struct_end() end + def ==(other) + return false unless other.is_a?(self.class) + each_field do |fid, type, name, default| + return false unless self.instance_variable_get("@#{name}") == other.instance_variable_get("@#{name}") + end + true + end + protected def handle_message(iprot, fid, ftype) @@ -77,10 +85,10 @@ module Thrift iprot.read_list_end elsif field[:type] == Types::SET e_type, size = iprot.read_set_begin - value = {} + value = Set.new size.times do element = read_field(iprot, field_info(field[:element])) - value[element] = true + value << element end iprot.read_set_end else -- 2.17.1