ottypes / json0

Version 0 of the JSON OT type
447 stars 64 forks source link

oi replaces even if exists #16

Open ronag opened 8 years ago

ronag commented 8 years ago

For {p:PATH, oi:NEWVALUE}

The documentation says:

When inserting, the key must not already be used.

However, if they key exists it currently will overwrite it...

Not sure what the correct behaviour is?

ronag commented 8 years ago

The problem I have here it that I would like to be able to insert an element into an array without knowing if the array exists or not, and if it doesn't exists it should be created:

Which I thought would be possible with the following:

[{
  p: ['tags'],
  oi: [],
}, {
  p: ['tags', 0],
  li: 'archived',
}],

However, this will always result in tags: ['archived']regardless if previous elements exists or not...

alecgibson commented 6 years ago

As a side-note, this is also a huge problem with inversion. It's currently possible to have:

{
  "tags": ["archived"]
}

and then perform this op on it:

[
  {
    "p": ["tags"],
    "oi": []
  }
]

...resulting in this document:

{
  "tags": []
}

Then when you try to invert the previous operation, the op doesn't know what was originally there when it replaced it, so it just deletes the field, resulting in:

{}

...which is not what I had before.

In my opinion, in the case where a path is being overwritten, json0 should throw, because it violates the integrity of the operation, as demonstrated above.

alecgibson commented 6 years ago

Aha, I've just seen that my issue is probably addressed in this PR: https://github.com/ottypes/json0/pull/19