smpallen99 / whatwasit

Track changes to your Ecto models
MIT License
63 stars 8 forks source link

Diff support for String #11

Closed Eiji7 closed 8 years ago

Eiji7 commented 8 years ago

I want to easily generate diff for %MyProject.User (admin and/or editor role) to preview multi line String e.g %MyProject.Post body field. This should be method that accept type argument (Atom or String) and optionally lines (Integer).

Lines argument is to limit diff response for X lines around change (not need when type is json).

Type argument may be:

  1. json (for API)
  2. normal (default)
  3. patch-file (for download - file patching and reverting by Linux patch command)
  4. side-by-side

Format for normal: We have Enum of lines. Each line have String(s) and/or Tuple(s). String(s) are not affected text. Tuple is info about affected text, where:

When side-by-side we have tuple: {left_column, right_column} where each column is array of lines (see format for nomal). For example: [ ["same text 1", {"insert", "text inserted in first line"}, "same text 2"], ["same text for second line ..."] ]

JSON API: Returns json with two fields: source - original text changes - array of changes where each element has type, line, column, change_length. For example: {"source": "First line\nSecondd line\nThird line", "changes":[{"change_length": 1, "column": 7, "line": 2, "type": "delete"}]} API says: "remove 7th character of 2nd column for given source".

Generating diff should be available for one and all versions (Version) for same model and model id.

Diff support can be optional when installing.

smpallen99 commented 8 years ago

Two ways to do this. 1. calculate and store on each change. 2. Generate from database on each request. The latter would be easier, but has performance implications.

I won't have time to work on this for a while. I've some ex_admin features I need to complete first.

A PR is welcome.

Eiji7 commented 8 years ago

Yes, these two ways can be configurable too. First when installing, second as a separated module.

defmodule MyProject.Post do
    use Whatwasit
    use Whatwasit.Diff # normal diffs when calling with two strings
    use Whatwasit.Version.Diff # diffs for versions
    # ...
end