juji-io / editscript

A library to diff and patch Clojure/ClojureScript data structures
Eclipse Public License 1.0
472 stars 23 forks source link

How to restore the editscript? #8

Closed FreekPaans closed 5 years ago

FreekPaans commented 5 years ago

Hi, I'm trying to persist the edit script so I can apply it at a later time. How would I go about doing that?

In essence what I want to do:

(patch a (get-edits (diff a b))

Thanks.

FreekPaans commented 5 years ago

The following solves my issue btw:

(defn editscript-patch
  [init-val edits]
  (reduce editscript.patch/patch*
    init-val
    edits))

But depends on internals so I hope there's a better way for doing this :)

huahaiy commented 5 years ago

(patch a (diff a b)) should do.

FreekPaans commented 5 years ago

The problem is that I don’t have b anymore, only a and (get-edits (diff a b)).

On Wed, 24 Apr 2019 at 05:31, Huaha Yang notifications@github.com wrote:

(patch a (diff a b)) should do.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/juji-io/editscript/issues/8#issuecomment-486053780, or mute the thread https://github.com/notifications/unsubscribe-auth/AA35X2JJVLPPEYL4OUCHJH3PR7ICLANCNFSM4HHEELFA .

huahaiy commented 5 years ago

You keep the result of (diff a b), say (def d (diff a b)), then (patch a d).

Of course, if you want to save the raw diff vector of (get-edits d), then restore from that, I can add a function to initialize an editscript instance from a raw diff vector.

FreekPaans commented 5 years ago

Of course, if you want to save the raw diff vector of (get-edits d), then restore from that, I can add a function to initialize an editscript instance from a raw diff vector.

That's exactly what I need. I need to store the diff offline, so I can't keep the EditScript instance.

huahaiy commented 5 years ago

@FreekPaans a function edits->script is added. Please let me know if it does what you want. Thanks.

FreekPaans commented 5 years ago

Hi, I finally got around testing this. Unfortunately, I'm running into some issues with this release]. When doing a trivial patch it throws.

(def edit-source {})
(def edit-dest {:x :hello-world})

"this throws"
(let [edits (editscript.edit/get-edits
              (editscript/diff edit-source edit-dest))]
  (editscript/patch edit-source (editscript.edit/edits->script edits))
  )

Throws with:

Execution error (AssertionError) at editscript.edit/edits->script (edit.cljc:198).
Assert failed: Not a vector of valid edits
(valid-edits? edits)

Also, I'm seeing some weird behavior with regard to equality:

(assert (= (editscript.edit/get-edits
                        (editscript/diff {} {}))
          (editscript.edit/get-edits
                        (editscript/diff {} {})))
  "this succeeds")

(def some-edits (editscript.edit/get-edits
                  (editscript/diff {} {})))

(assert (= some-edits (editscript.edit/get-edits
                        (editscript/diff {} {})))
  "this fails")

I created a full repro repo here: https://github.com/FreekPaans/editscript-repro/blob/master/src/editscript_test/core.clj. Also tested on Clojure 1.10.0, Java version was 11.