Skip to content

Commit

Permalink
Fix repeated fields with ruby keyword names.
Browse files Browse the repository at this point in the history
The field related runtime code generation was producing invalid
code if you had a repeated field named e.g. "or". I added a
"self." to disambiguate (e.g. "or.clear" became "self.or.clear").
  • Loading branch information
Muir Manders committed May 20, 2015
1 parent b545eb5 commit b7d8546
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/protocol_buffers/runtime/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ def add_writer_to(klass)
klass.class_eval <<-EOF, __FILE__, __LINE__+1
def #{name}=(__value)
if __value.nil?
#{name}.clear
self.#{name}.clear
else
unless __value.equal?(#{name})
#{name}.clear
unless __value.equal?(self.#{name})
self.#{name}.clear
__value.each { |i| @#{name}.push i }
end
if @parent_for_notify
Expand Down Expand Up @@ -610,7 +610,7 @@ def inspect_value(value)

def text_format(io, value, options = nil)
formatted = @value_to_name[value] || value.to_s
io.write formatted
io.write formatted
end
end

Expand Down
8 changes: 8 additions & 0 deletions spec/proto_files/featureful.pb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class C < ::ProtocolBuffers::Message; end
class D < ::ProtocolBuffers::Message; end
class E < ::ProtocolBuffers::Message; end
class F < ::ProtocolBuffers::Message; end
class Keywords < ::ProtocolBuffers::Message; end

# enums
module MainPayloads
Expand Down Expand Up @@ -176,4 +177,11 @@ class F < ::ProtocolBuffers::Message
optional :string, :s, 1
end

class Keywords < ::ProtocolBuffers::Message
set_fully_qualified_name "featureful.Keywords"

repeated :bool, :or, 1
optional :bool, :and, 2
end

end
5 changes: 5 additions & 0 deletions spec/proto_files/featureful.proto
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,8 @@ message E {
message F {
optional string s = 1;
}

message Keywords {
repeated bool or = 1;
optional bool and = 2;
}
8 changes: 8 additions & 0 deletions spec/runtime_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,14 @@ class IntMsg < ProtocolBuffers::Message
Services::NoNameFooBarService.fully_qualified_name.should == nil
end

it "correctly handles fields with keyword names" do
f = Featureful::Keywords.new(
:or => [true],
:and => false,
)
f.or.should =~ [true]
end

def get_rpcs
Services::FooBarService.rpcs.size.should == 2
first_rpc = Services::FooBarService.rpcs[0]
Expand Down

0 comments on commit b7d8546

Please sign in to comment.