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

fromJS reviver should have access to the full key path #1036

Closed
jeffbski opened this issue Jan 9, 2017 · 1 comment
Closed

fromJS reviver should have access to the full key path #1036

jeffbski opened this issue Jan 9, 2017 · 1 comment

Comments

@jeffbski
Copy link
Contributor

jeffbski commented Jan 9, 2017

I've come to the conclusion that the fromJS reviver should have access to the full key path.

If that was provided then we can easily setup a map to do custom things at certain paths when converting nested objects and lists into Immutable.js structures.

For instance, given a plain JS structure

const struct1 = {
  profile: {
    firstName: 'John',
    lasName: 'Smith',
    emails: [
      { type: 'home', name: 'J Smith', email: 'john@foo.com' },
      { type: 'work', name: 'John Smith', email: 'johnsmith@bar.com' }
    ]
  }
};

If we want to convert structures like this into Records and Lists, we might create a reviver that maps the root, profile, emails, and emails entries to records.

const recordMap = {
  '': RootRecord,
  'profile': ProfileRecord,
  'profile.emails[x]': EmailRecord
}

Then in our reviver, we can apply the appropriate record based on the full path of the key.

Currently as of today, we only get the local key, so there is no good way to setup a proper mapping. That local key might be used in many places so we really need a full path key. Also with the list items, the local key will just be a number, so this doesn't tell us which Record to use assuming there could be many lists in the structure.

I see that your v4 subRecords feature might help but even then I'm not sure they go deep enough and can reach into lists.

But besides the use of Records if you wanted to do any specific tailoring to structures when converting with fromJS, it really makes sense to have the full key path available so you can know what to do. You can't always just assume things based on the data, it would be better to also have the full key path so you know if you are in the right part of the structure.

I thought I might be able to figure out the full key path using the this object which provides the parent, but as far as I can tell that doesn't help any.

If there is another way to get the full path in the reviver, please let me know.

Otherwise, I'm thinking it would be good to add another parameter which is the full key path to the reviver. For consistency with the rest of the API it would probably be an array like ['profile', 'emails', 1].

So the new signature for the fromJS reviver would be

fromJS(json: any, reviver?: (k: any, v: Iterable<any, any>, path: Array<string>) => any): any

Thoughts or alternative ideas?

@jeffbski
Copy link
Contributor Author

jeffbski commented Mar 8, 2017

Awesome! Thanks @leebyron

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

No branches or pull requests

2 participants