Skip to content

Commit

Permalink
Replace enum messages, don't concut them
Browse files Browse the repository at this point in the history
adjust comments

adding a test

fixing dupplicated fields

replace Array.wrap with simpler form

update changelog
  • Loading branch information
skatkov committed Mar 22, 2024
1 parent ec2607d commit 2210e6f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ 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).

## 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
- Add support for partial as a first argument , e.g.`pb.friends "racers/racer", as: :racer, collection: @racers`
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
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 "duplicated enum fields don't happen" 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 "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)
friends = [klass.new("Friend 1"), klass.new("Friend 2")]
Expand Down

0 comments on commit 2210e6f

Please sign in to comment.