Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace repeated messages instead of appending more #55

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ All notable changes to this project will be documented in this file.

This format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## UNRELEASED
### Fixed
- Fixed CI by locking 3 version of google-protobuf dependency
## 0.17.0
### Changed
- Instead of appending to repeated enum message, we're replacing it to avoid issues in case output will be rendered twice
- If one field was defined twice, only last definition will end up in output

## Fixed
- Fixed CI by locking 3 version or lower of google-protobuf dependency.

## 0.16.2
### Added
Expand Down
8 changes: 5 additions & 3 deletions lib/pbbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ 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"]
# Using concat so it behaves the same as _append_repeated

@message[name].concat arg.to_ary
@message[name].replace arg.to_ary
skatkov marked this conversation as resolved.
Show resolved Hide resolved
elsif arg.respond_to?(:to_ary) && descriptor.type.eql?(:message)
# example syntax that should end up here:
# pb.friends [Person.new(name: "Johnny Test"), Person.new(name: "Max Verstappen")]
Expand All @@ -89,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 [arg]
end
else
# example syntax that should end up here:
# pb.field "value"

@message[name] = arg
end
else
Expand Down
2 changes: 1 addition & 1 deletion pbbuilder.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Gem::Specification.new do |spec|
spec.name = "pbbuilder"
spec.version = "0.16.2"
spec.version = "0.17.0"
spec.authors = ["Bouke van der Bijl"]
spec.email = ["[email protected]"]
spec.homepage = "https://github.com/cheddar-me/pbbuilder"
Expand Down
29 changes: 28 additions & 1 deletion test/pbbuilder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,38 @@ 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

test "replaces the repeated field's value with the last one set" do
person = Pbbuilder.new(API::Person.new) do |pb|
pb.field_mask do
pb.paths ["ok", "that's"]
end
end.target!

p = Pbbuilder.new(person) do |pb|
pb.field_mask do
pb.paths ["ok", "that's"]
end
end.target!

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

test "sets the last value of the repeated field to be the only value" 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)
friends = [klass.new("Friend 1"), klass.new("Friend 2")]
Expand Down
Loading