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

Add parent path to changes map #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

derrell
Copy link

@derrell derrell commented Dec 31, 2019

With an arbitrary set of object changes, including keys that contain dots or
slashes, it can be difficult to retrieve, via dot notation, an object whose
property is changing. Consider this set of operations:

let             test;
let             p;
const           ObservableSlim = require("./observable-slim.js");

test = {};
p = ObservableSlim.create(
  test,
  false,
  (changes) =>
    {
      console.log(JSON.stringify(changes, null, "  "));
    });

p.hello = "world";
p["a.b"] = { c : {} };
p["a.b"].c["e.f"] = 42;

The output of the final change is:

[
  {
    "type": "add",
    "target": {
      "e.f": 42
    },
    "property": "e.f",
    "newValue": 42,
    "currentPath": "a.b.c.e.f",
    "jsonPointer": "/a/b/c/e/f",
    "proxy": {
      "e.f": 42
    }
  }
]

Note that it is not exactly trivial to figure out how to update a remote
object based on this information. There is nothing here that would tell you
that of currentPath "a.b.c.e.f", the "a.b" part is a single key.

To help resolve this problem, I have added the parent path to the change
map. With the parent path, an array, it is easy to iterate through the array
beginning at the top-level object, to then add/update/whatever the given
property. With the change, the above test yields, as the final change, the
following, which includes the parent array member:

[
  {
    "type": "add",
    "target": {
      "e.f": 42
    },
    "parent": [
      "a.b",
      "c"
    ],
    "property": "e.f",
    "newValue": 42,
    "currentPath": "a.b.c.e.f",
    "jsonPointer": "/a/b/c/e/f",
    "proxy": {
      "e.f": 42
    }
  }
]

With an arbitrary set of object changes, including keys that contain dots or
slashes, it can be difficult to retrieve, via dot notation, an object whose
property is changing. Consider this set of operations:

```
let             test;
let             p;
const           ObservableSlim = require("./observable-slim.js");

test = {};
p = ObservableSlim.create(
  test,
  false,
  (changes) =>
    {
      console.log(JSON.stringify(changes, null, "  "));
    });

p.hello = "world";
p["a.b"] = { c : {} };
p["a.b"].c["e.f"] = 42;
```

The output of the final change is:

```
[
  {
    "type": "add",
    "target": {
      "e.f": 42
    },
    "property": "e.f",
    "newValue": 42,
    "currentPath": "a.b.c.e.f",
    "jsonPointer": "/a/b/c/e/f",
    "proxy": {
      "e.f": 42
    }
  }
]
```

Note that it is not exactly trivial to figure out how to update a remote
object based on this information. There is nothing here that would tell you
that of currentPath "a.b.c.e.f", the "a.b" part is a single key.

To help resolve this problem, I have added the parent path to the change
map. With the parent path, an array, it is easy to iterate through the array
beginning at the top-level object, to then add/update/whatever the given
property. With the change, the above test yields, as the final change, the
following, which includes the `parent` array member:

```
[
  {
    "type": "add",
    "target": {
      "e.f": 42
    },
    "parent": [
      "a.b",
      "c"
    ],
    "property": "e.f",
    "newValue": 42,
    "currentPath": "a.b.c.e.f",
    "jsonPointer": "/a/b/c/e/f",
    "proxy": {
      "e.f": 42
    }
  }
]
```
@coveralls
Copy link

coveralls commented Dec 31, 2019

Pull Request Test Coverage Report for Build 101

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.02%) to 95.262%

Totals Coverage Status
Change from base Build 89: 0.02%
Covered Lines: 237
Relevant Lines: 237

💛 - Coveralls

@ElliotNB
Copy link
Owner

@derrell My sincere apologies for not seeing this until now! I'll take a look at this contribution this week or by early next week. One thing that I noticed right off the bat is the use of spaces instead of tabs for indentation. Would you be able to switch it to tabs on your fork?

@derrell
Copy link
Author

derrell commented Feb 25, 2021

@ElliotNB Sorry about the formatting issue. I try to be careful to match the format of code I'm changing, but obviously missed it here. Fixed now.

Copy link
Contributor

@BackdoorTech BackdoorTech left a comment

Choose a reason for hiding this comment

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

Looking cool!

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.

4 participants