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

diff.Diff() with an empty value produces one blank line #22

Open
thockin opened this issue Jun 18, 2019 · 2 comments
Open

diff.Diff() with an empty value produces one blank line #22

thockin opened this issue Jun 18, 2019 · 2 comments
Labels
breaking_change Breaking change; potential feature for a v2 branch

Comments

@thockin
Copy link

thockin commented Jun 18, 2019

I have a series of blobs to diff, each against the previous. The seed is an empty string. Diffing the first blob against that produces a diff with one "removed" line.

-
+{
+  "apiVersion": "v1",
+  "kind": "Service",
+  "metadata": {
@kylelemons
Copy link
Owner

kylelemons commented Aug 24, 2019

Hmm, yeah that is unfortunate. I think this would be too big of a behavior change for a minor version update, however.

The reason for this is that Diff is comparing lines from A to lines in B by splitting on \n, so there is no way to get Diff to compare against zero lines on one of its sides.

To help improve this situation, I've exported Render which can be used directly on the output of DiffChunks, so that you can control this behavior yourself. For example, you could make a helper like this:

func diffWithEmpty(a, b string) string {
  var aLines, bLines []string
  if a != "" {
    aLines = strings.Split(a, "\n")
  }
  if b != "" {
    bLines = strings.Split(b, "\n")
  }
  return diff.Render(diff.DiffChunks(aLines, bLines))
}

to get the behavior you're looking for.

For the moment, this is only exposed by github.com/kylelemons/godebug@master if you are using go modules; let me know if this works for you, and I can cut a new module release.

I also highly recommend the cmp package if you are comparing structures, as it actually does proper recursive descent instead of stringifying everything before running a string-based diff algorithm.

@thockin
Copy link
Author

thockin commented Aug 30, 2019

This was not a critical issue for me, so frankly I probably won't update or change what we're doing. I thought it was worth filing a bug, but it sounds like it's not something you want to "break" (though IMO that's debatable, but I admire the dedication to not breaking people :).

Thanks for a great and simple lib that (mostly) just works.

@kylelemons kylelemons added the breaking_change Breaking change; potential feature for a v2 branch label Aug 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking_change Breaking change; potential feature for a v2 branch
Projects
None yet
Development

No branches or pull requests

2 participants