adopted-ember-addons / validated-changeset

Buffering changes to form data
MIT License
36 stars 27 forks source link

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

Open sapryniukt opened 2 years ago

sapryniukt commented 2 years ago

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 commented 2 years ago

@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 commented 2 years ago

@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 commented 2 years ago

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!