Skip to content
This repository has been archived by the owner on Jul 5, 2023. It is now read-only.

Zip plucked values with tstruct keys in the same order for pluck_to_tstruct #563

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion lib/sorbet-rails/rails_mixins/pluck_to_tstruct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ def pluck_to_tstruct(ta_struct, associations: {})
end

pluck_keys = (tstruct_keys - associations_keys) + associations.values
tstruct_keys_in_pluck_order = tstruct_keys - associations_keys + associations_keys

# loosely based on pluck_to_hash gem
# https://github.com/girishso/pluck_to_hash/blob/master/lib/pluck_to_hash.rb
keys_one = pluck_keys.size == 1
pluck(*pluck_keys).map do |row|
row = [row] if keys_one
value = Hash[map_nil_values_to_default(tstruct_props, tstruct_keys.zip(row))]
value = Hash[map_nil_values_to_default(tstruct_props, tstruct_keys_in_pluck_order.zip(row))]
Copy link
Author

@gnaratil2017 gnaratil2017 Jun 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we zip the tstruct keys with the plucked values, we want to make sure the corresponding key and value match up correctly.

This requires that our tstruct keys are in the same order as the pluck_keys, which is why we want to subtract the associations_keys and then append them to the end of tstruct_keys on line 34. This matches the operations and order changes we performed for the pluck_keys on line 33.

tstruct.new(value)
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/pluck_to_tstruct_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class WizardWithWandT < T::Struct
include TStructComparable

const :name, String
const :house, String
const :wand_wood_type, String
const :house, String
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this change in the order of the tstruct props, the spec on line 138 fails without the new changes to pluck_to_tstruct.rb.

context 'pluck with associations' do
associations = { wand_wood_type: "wands.wood_type" }
expected = [
WizardWithWandT.new(name: "Harry Potter", house: "Gryffindor", wand_wood_type: "Holly"),
WizardWithWandT.new(name: "Hermione Granger", house: "Gryffindor", wand_wood_type: "Vine"),
]
it_should_behave_like 'pluck_to_tstruct with associations', WizardWithWandT, associations, expected
end

Rather than the expected array, the actual array would look like:

[
     WizardWithWandT.new(name: "Harry Potter", house: "Holly", wand_wood_type: "Gryffindor"),
     WizardWithWandT.new(name: "Hermione Granger", house: "Vine", wand_wood_type: "Gryffindor"),
]

end

class WizardWithDefaultParentEmailT < T::Struct
Expand Down