You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
keys with type "Set" returning an Array rather than set in 0.13.1 #614
I’ve been on a version of 0.13.0 Beta 2 (:ref => ‘9b87dee507802a8cb96f2d316802c9fd241448c8’) for a couple of years. Just tried the current live gem, 0.13.1 and ran my specs. A few failed that have Sets for keys. I opened Rails Consoles with 0.13.0 Beta 2 and 0.13.1 running. Here’s what I see:
0.13.0 beta 2:
p.descendant_ids.class
=> Set
0.13.1:
p.descendant_ids.class
=> Array
I did a cursory search through the issues and didn’t see anything related.
describe"a key with a Set type"doDocWithSetKey=Docdoset_collection_name"docs_with_set_keys"key:foo,SetendbeforedoDocWithSetKey.createfoo: [1,2,3]endit"should persist as a Set"doexpect(DocWithSetKey.first.foo).tobe_a(Set)endend
This spec passes for me. Can you help me figure out how to break it?
If you want to see a spec that fails, add this into functional/keys_spec.rb:
context "when persisting a typecast Set" do
TypecastedKeyModel = Doc do
key :people, Set, :typecast => "Person"
end
Person = Struct.new(:name) do
def self.to_mongo(value)
value.name
end
def self.from_mongo(value)
Person.new value
end
end
it "should not mutate the model's state" do
person = Person.new "Bob"
doc = TypecastedKeyModel.new(:people => Set.new([person]))
doc.save
doc.people.should == Set.new([person])
end
end
Question for you, Chris: is there any reason not to use map! to modify what is returned by from_mongo? I'd rather not make several lists to get to the final one unless necessary.
I can't think of a reason off the top of my head, but generally I make those kinds of changes when there may be potential side-effects of mutating a provided list rather than returning a new copy.
Where's the offending line? I might be able to provide better opinions if I can look at the blame history.
Activity
cheald commentedon Dec 4, 2015
Ah, interesting. That looks like a regression! I'll see what I can find!
cheald commentedon Dec 4, 2015
This spec passes for me. Can you help me figure out how to break it?
jamieorc commentedon Dec 4, 2015
Thanks, I'll have a look this afternoon.
jamieorc commentedon Dec 7, 2015
I've verified the problem again, so digging into it.
jamieorc commentedon Dec 7, 2015
I've found what causes the problem, but not the regression in the code:
When
:typecastis set,Arrayis returned instead ofSet. Here,:foo_idsistypecasttoObjectId:Here
:foo_idsis set toString:With no
:typecast, aSetis returned, as expected:jamieorc commentedon Dec 7, 2015
I've found the regression and will work up a pull request. It's around using
mapinstead ofmap!when typecasting:map(currently used) always returns anArraymap!(formerly used) operates on the existingArrayorSet, so preserves the original list typejamieorc commentedon Dec 7, 2015
If you want to see a spec that fails, add this into functional/keys_spec.rb:
jamieorc commentedon Dec 7, 2015
Question for you, Chris: is there any reason not to use
map!to modify what is returned byfrom_mongo? I'd rather not make several lists to get to the final one unless necessary.cheald commentedon Dec 7, 2015
I can't think of a reason off the top of my head, but generally I make those kinds of changes when there may be potential side-effects of mutating a provided list rather than returning a new copy.
Where's the offending line? I might be able to provide better opinions if I can look at the blame history.
jamieorc commentedon Dec 7, 2015
Line 65 in key.rb:
https://github.com/mongomapper/mongomapper/blob/master/lib/mongo_mapper/plugins/keys/key.rb#L65
I've got a spec and the change done on a fork. I'll make a pull request if you don't see any reason not to.