Skip to content

Commit

Permalink
fixing dupplicated fields
Browse files Browse the repository at this point in the history
  • Loading branch information
skatkov committed Mar 21, 2024
1 parent 60eefdd commit b11d896
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/pbbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def set!(field, *args, &block)
if arg.respond_to?(:to_hash)
# example syntax that should end up here:
# pb.fields {"one" => "two"}

arg.to_hash.each { |k, v| @message[name][k] = v }
elsif arg.respond_to?(:to_ary) && !descriptor.type.eql?(:message)
# pb.fields ["one", "two"]
Expand All @@ -88,11 +89,13 @@ def set!(field, *args, &block)
else
# example syntax that should end up here:
# pb.fields "one"
@message[name].push arg

@message[name].replace Array.wrap(arg)
end
else
# example syntax that should end up here:
# pb.field "value"

@message[name] = arg
end
else
Expand Down
13 changes: 12 additions & 1 deletion test/pbbuilder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class PbbuilderTest < ActiveSupport::TestCase

assert_equal "Hello world", person.name
assert_equal "Friend #1", person.friends.first.name
assert_equal ["ok", "that's", "cool"], person.field_mask.paths
assert_equal ["cool"], person.field_mask.paths
assert_equal "Manuelo", person.best_friend.name
assert_equal "Eggs", person.favourite_foods["Breakfast"]
end
Expand All @@ -45,6 +45,17 @@ class PbbuilderTest < ActiveSupport::TestCase

assert_equal(["ok", "that's"], p.field_mask.paths)
end

test "Would not stack duplicated values, uses last one defined" do
person = Pbbuilder.new(API::Person.new) do |pb|
pb.field_mask do
pb.paths ["ok", "that's"]
pb.paths ["cool"]
end
end.target!

assert_equal ["cool"], person.field_mask.paths
end

test "it can extract fields in a nice way" do
klass = Struct.new(:name)
Expand Down

0 comments on commit b11d896

Please sign in to comment.