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

[Flow]: Add JS objects to Map.mergeIn & Map.mergeDeepIn #987

Merged

Conversation

alex35mil
Copy link
Contributor

Adds vanilla JS objects to flow typings of Immutable.Map's mergeIn & mergeDeepIn.

@facebook-github-bot
Copy link

Thank you for your pull request. As you may know, we require contributors to sign our Contributor License Agreement, and we don't seem to have you on file and listed as active anymore. In order for us to review and merge your code, please email cla@fb.com with your details so we can update your status.

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@wokalski
Copy link
Contributor

@alexfedoseev This repo is pretty much dead. I created a PR in flow-typed to add flow definitions for immutable there.

My fork is here. Feel free to submit a PR with your changes there.

@lacker
Copy link

lacker commented Dec 2, 2016

Dead? Pshhh. It's not like we let this pull request sit for 15 months without responding.

Anyway my guess is that this has been abandoned. I was gonna ask for some sort of test if this was still alive, but I'm just gonna drop it. Sorry for letting this be quiet for so long :P and if you want to keep working on this please feel free to reopen!

@lacker lacker closed this Dec 2, 2016
@wokalski
Copy link
Contributor

wokalski commented Dec 2, 2016

hey, thanks for bringing it back. I didn't mean to insult you. I just didn't see too much activity on any pull request at that time. Anyway, if you want a flow definition here (vs having one in flow-typed) I am more than happy to maintain it.

@lacker
Copy link

lacker commented Dec 2, 2016

What's the "standard thing to do" around having a flow definition here vs in flow-typed? We should have a correct flow-typing as opposed to an incorrect one but I don't particularly have a stance on where it should go. Probably here? So I think it is awesome if you could help maintain this - so here's a question - do you think it makes sense when people submit a change to the flowtypes, to ask for a test that exercises the new type? I just want to make sure that we are protecting against stuff breaking as we improve things.

@lacker lacker reopened this Dec 2, 2016
@wokalski
Copy link
Contributor

wokalski commented Dec 2, 2016

In my opinion it's better when definitions are in the repo. For obvious reasons - it becomes immediately available to everyone who uses it. I'll ask in this PR for their point of view on it.

We might put it here for now and we will see how it evolves.

When it comes to testing, yes, I do think that tests are necessary. And tests are implemented here. I just need to make flow run on travis so that it's automated.

@wokalski
Copy link
Contributor

wokalski commented Dec 2, 2016

Sorry that I spammed across few PRs to move people's work to my PR in flow-typed but I didn't know when and if our work here will ever be merged.

@lacker
Copy link

lacker commented Dec 2, 2016

Yeah I understand, it's cool that you were spamming your alternative, I feel your pain man. But now that I am paying attention I would like the immutable.js repo to be all nice and things. I could really use your help reviewing Flow stuff though since I'm sort of a Flow noob myself. So what do you think we should do for this pull request - just accept it, or ask for some extra test type stuff, or what?

@wokalski
Copy link
Contributor

wokalski commented Dec 2, 2016

I think that the order should be the following:

  1. Decide on the blessed way to import things
  2. Merge my PR I mentioned above which actually contains tests. It also needs some work but I can do it over the weekend.
  3. Further improvements like this one

flow-typed makes a lot of sense because of versioning (separate versioning for flow versions and module versions). Also testing if the definitions actually make sense is a thing to look forward to. However for now, I'd like the "correct" definition to be anywhere and we can proceed with further improvements in the future.

@wokalski
Copy link
Contributor

wokalski commented Dec 5, 2016

I made the tests run on travis in #878

@alex35mil
Copy link
Contributor Author

@lacker @wokalski Hey guys, sorry for being quiet, I stopped visiting this PR after some time and missed the activity here as my gh became too noisy these days.

According to this comment serving shadow files w/ typings is still the way to go, so I guess this PR still makes sense.

AFAIU this PR is currently blocked by #878, let me know if I misread something and let me know what & when needs to be done here to get this merged. Thanks!

@lacker lacker added the flow label Dec 6, 2016
@lacker
Copy link

lacker commented Dec 6, 2016

OK so #878 is in now so we have some Flow type refactorings and also you can now test flow types. Would you mind updating to merge those changes in, and also adding a test that catches these changes? Thanks!

@alex35mil
Copy link
Contributor Author

@lacker Ok! I'll look into this on weekend 👍

@alex35mil alex35mil force-pushed the add-js-objects-to-map-mergein-flowtypes branch from 71d7640 to bfe796e Compare December 12, 2016 02:38
@alex35mil alex35mil force-pushed the add-js-objects-to-map-mergein-flowtypes branch from bfe796e to 2d26e0c Compare December 12, 2016 02:45
@alex35mil
Copy link
Contributor Author

@lacker @wokalski All done.

One note: it's not related to this PR, but when I run jest on my local branch, it throws: https://cl.ly/iPtb
Possibly similar issue: facebook/react-native#7802 (comment)

@wokalski
Copy link
Contributor

I'm a bit confused. The diff shows that my changes are reapplied. Could you rebase the PR against current master? Possibly your fork's master is probably behind

@alex35mil
Copy link
Contributor Author

I'm a bit confused. The diff shows that my changes are reapplied. Could you rebase the PR against current master? Possibly your fork's master is probably behind

@wokalski I think I rebased everything against master (flow tests weren't in my initial fork), let me check it one more time.

@wokalski
Copy link
Contributor

If you look at this file and the contents of the PR, you can see that something is off. Maybe conflict resolution gone wrong?

mergeIn(
keyPath: ESIterable<any>,
...iterables: (ESIterable<any> | { [key: string]: any })[]
): Map<K,V>;
Copy link
Contributor

Choose a reason for hiding this comment

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

This makes me a bit confused. The mergeIn does change the type of the map, doesn't it? If so we (sadly) might need to turn V into any.

Copy link
Contributor Author

@alex35mil alex35mil Dec 12, 2016

Choose a reason for hiding this comment

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

I used any b/c of this: http://facebook.github.io/immutable-js/docs/#/Map/merge

If any of the values provided to merge are not Iterable (would return false for Immutable.Iterable.isIterable) then they are deeply converted via Immutable.fromJS before being merged. However, if the value is an Iterable but includes non-iterable JS objects or arrays, those nested values will be preserved.

So if V is also Map or List, I can pass Object or Array as V and it still should be valid as it will be converted to Map or List by fromJS.

Copy link
Contributor

Choose a reason for hiding this comment

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

This part is correct. However in flow Map and List are parametrised (generic). So if you merge ESIterable<any> with Map<string, string> it becomes Map<string, any>, Map<string, Map<string | number>> would become Map<string, any>`.

AFAIK there's no way to overload the declaration for different V types so that we would have one for V: Map and one for V: List case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, so your suggestion is to turn those to this, right?

mergeIn(
  keyPath: ESIterable<any>,
  ...iterables: (ESIterable<any> | { [key: string]: any })[]
): Map<K,any>;
mergeDeepIn(
  keyPath: ESIterable<any>,
  ...iterables: (ESIterable<any> | { [key: string]: any })[]
): Map<K,any>;

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, iff my reasoning is correct.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also I had two further thoughts:

  • maybe the key path could be ESIterable<K> to make it type safe?
  • It should also be possible to merge it with a Map, not only an object, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

maybe the key path could be ESIterable to make it type safe?

Agreed, done.

It should also be possible to merge it with a Map, not only an object, right?

Right, also w/ List etc., but I think it's handled by ESIterable<K>. Tests added.

@@ -228,6 +228,9 @@ stringToNumber = Map({'a': 1}).removeIn([], 0)
stringToNumber = Map({'a': 1}).mergeIn([], [])
stringToNumber = Map({'a': 1}).mergeDeepIn([], [])

anyMap = Map({'a': {}}).mergeIn(['a'], { b: 2 })
anyMap = Map({'a': {}}).mergeDeepIn(['a'], { b: 2 })
Copy link
Contributor

Choose a reason for hiding this comment

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

Needs some tests for false positives. Put // $ExpectError on a line preceding a code which should output an error for these.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@alex35mil
Copy link
Contributor Author

If you look at this file and the contents of the PR, you can see that something is off. Maybe conflict resolution gone wrong?

@wokalski That's weird, there was conflict only in dist/immutable.js.flow, which is generated file and it's regenerated anyway. Should I take contents of this file and just reapply my changes?

@wokalski
Copy link
Contributor

It would be awesome.

@alex35mil
Copy link
Contributor Author

@wokalski Just pasted contents and I see only my changes in diff: https://cl.ly/iPzf

@wokalski
Copy link
Contributor

omg, sorry. This is what you call a 5 am review. I didn't notice that referred to dist/

@alex35mil
Copy link
Contributor Author

@wokalski Np, I replied to comments 👍

@alex35mil
Copy link
Contributor Author

@wokalski Comments addressed. Also I updated $ExpectError regex: now it's possible to add comments to it (example).

/cc: @lacker

@wokalski
Copy link
Contributor

This is awesome. Great work 👍

Now it's up to somebody with commit access to merge it.

@lacker lacker merged commit 0bd996b into immutable-js:master Dec 14, 2016
@lacker
Copy link

lacker commented Dec 14, 2016

Thanks @alexfedoseev for submitting this and thanks @wokalski for reviewing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants