Skip to content

Fix snapshot/restore methods to separate keys and values - #167

Open
sapryniukt wants to merge 1 commit into
adopted-ember-addons:mainfrom
sapryniukt:fix-snapshot-restore-to-separate-keys-and-values
Open

sapryniukt wants to merge 1 commit into
adopted-ember-addons:mainfrom
sapryniukt:fix-snapshot-restore-to-separate-keys-and-values

Conversation

@sapryniukt

@sapryniukt sapryniukt commented Mar 16, 2022 •

Copy link
Copy Markdown

this PR is based on #24 issue and #27 pull request.

After updating the key storage template from string to nested keys, especially in the snapshot() and restore() methods there is no clear separation of keys and values of changes if both are objects.

snapshot() returns structure like

snapshot:  {
  changes: {
    "name": "Jim Bob",
    "address": { 
      "country": {
        "id": "US",
        "name": "United States"
      }
    }
  }
}

The idea is not to use getChangeValue() instead,
It will have a structure like

snapshot:  {
  changes: {
    "name": { 
      Symbol("__value__"): "Jim Bob" 
    },
    "address": { 
      "country": { 
        Symbol("__value__"): {
          "id": "US",
          "name": "United States"
        }
      }
    }
  }
}

Update 04 August 2022

A simpler solution was applied to avoid changing the shape of data and changes could keep their own proptotypes. In reduce method of #restore we should set initialValue as:

someArr.reduce((...params) => {
  // some code
}, Object.create(prototype));

@snewcomer

Copy link
Copy Markdown
Collaborator

@sapryniukt Thanks for the PR! Is this structure useful for you? If you try and access address.country.id to see what the value is, now you need to do something like address.country[VALUE].id. Is that ok?

@sapryniukt

Copy link
Copy Markdown
Author

@snewcomer Yep! As I understand it, snapshot() is only used for restore() therefore, changeset after restore will have the expected result. The problem is that keys are nested objects and values can also be objects. Especially for the condition of exit from recursion it is necessary to separate which objects are keys and which values.
the idea was to make something like address.country[VALUE].id.

@snewcomer

Copy link
Copy Markdown
Collaborator

Do you have a test case that shows a bug that was fixed? This would be a breaking change and still unsure of the value. Happy to consider it though!

Comment thread test/index.test.ts Outdated

it('#restore restores a snapshot of the changeset with nested values', () => {
let user = { name: 'Adam', address: { country: 'United States' } };
it('#restore restores a snapshot of the changeset when nested value is object', () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@sapryniukt could you please keep the existing test case as is and only add a new test?

cc @snewcomer this test mimics the scenario that fails as today restore would not honor an instance of the Country class and would only restore the object structure

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Sorry for my lack of familiarity but I’m guessing we can’t fix the bug by keeping the existing data shape?

@sapryniukt
sapryniukt force-pushed the fix-snapshot-restore-to-separate-keys-and-values branch 3 times, most recently from 6361e4c to 1ec241d Compare August 4, 2022 10:09
@sapryniukt
sapryniukt force-pushed the fix-snapshot-restore-to-separate-keys-and-values branch from 1ec241d to 21637d2 Compare August 4, 2022 10:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants