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

Support displaying only areas of diff #6

Open
leosunmo opened this issue Jul 6, 2020 · 9 comments
Open

Support displaying only areas of diff #6

leosunmo opened this issue Jul 6, 2020 · 9 comments

Comments

@leosunmo
Copy link

leosunmo commented Jul 6, 2020

First of all, great library! This is a tricky problem to solve and it's done very nicely in jsondiff!

Now, I am diffing very large JSON files, and I would like to only output the actual differences, not the entire doc if possible.

I couldn't see an existing way to do this, but if there is, please let me know!

Either a config setting that would allow you to display X lines/objects above and below each diff, or potentially a way to roll up previous fields?

The roll-up would ideally look something like this:
From online demo:

{
    "content": [
        {
            "align": "center",
            "content": [
                {
                    "content": "And please, feel free to send us your feedback and comments to ",
                    "style": {
                        "bold": 2,
                        "color": "FFFFFF",
                      - "fontFamily": "Arial",
                        "italic": 2,
                      + "name": "Arial",
                        "size": 20,
                      ~ "underline": 1 => 2
                    }
                },
...

Rolled up:

{
    "content": [
        {
            "content": [
                {
                    "style": {
                        "bold": 2,
                        "color": "FFFFFF",
                      - "fontFamily": "Arial",
                        "italic": 2,
                      + "name": "Arial",
                        "size": 20,
                      ~ "underline": 1 => 2
                    }
                },
...

Or potentially by separating parent objects using a dot notation:

content.content.style {
                        "bold": 2,
                        "color": "FFFFFF",
                      - "fontFamily": "Arial",
                        "italic": 2,
                      + "name": "Arial",
                        "size": 20,
                      ~ "underline": 1 => 2
                    }

It's a tricky one, but to start with, just having an option to say, only display X lines above and below the change, might be enough!

@leosunmo leosunmo changed the title Support displaying one areas of diff Support displaying only areas of diff Jul 6, 2020
@nsf
Copy link
Owner

nsf commented Jul 7, 2020

Good idea, I'll think about implementing it.

@jerbob92
Copy link

I implemented this in #14

@nsf
Copy link
Owner

nsf commented Sep 18, 2021

Yeah, sorry that it took a long time. See discussion in #14. I've just added a version of this feature.

@leosunmo
Copy link
Author

Awesome!! Nice work both @jerbob92 and @nsf !

I did run in to a problem when using SkipMatches: true. It looks like it panics at 0e9c064#diff-ab5d2ac49314d80a4296040d50fa94c987b46467f4b69661ee9459f8e103f754R274.

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x693955]

goroutine 1 [running]:
github.com/nsf/jsondiff.(*context).printSkipped(0xc00017da08, 0x7ca3c0, 0xc00017c470, 0x0, 0x1)
	/home/leo/go/pkg/mod/github.com/nsf/[email protected]/jsondiff.go:274 +0x55
github.com/nsf/jsondiff.(*context).printDiff(0xc00017da08, {0x7e3280, 0xc0002cacc0}, {0x7e3280, 0xc0002ed470})
	/home/leo/go/pkg/mod/github.com/nsf/[email protected]/jsondiff.go:487 +0x1408
github.com/nsf/jsondiff.(*context).printDiff(0xc00017da08, {0x7e3280, 0xc0002cac90}, {0x7e3280, 0xc0002ed440})
	/home/leo/go/pkg/mod/github.com/nsf/[email protected]/jsondiff.go:449 +0x119f
github.com/nsf/jsondiff.(*context).printDiff(0xc00017da08, {0x7e3280, 0xc0002cac60}, {0x7e3280, 0xc0002ed410})
	/home/leo/go/pkg/mod/github.com/nsf/[email protected]/jsondiff.go:449 +0x119f
github.com/nsf/jsondiff.(*context).printDiff(0xc00017da08, {0x7c4180, 0xc0002d8330}, {0x7c4180, 0xc0002d8360})
	/home/leo/go/pkg/mod/github.com/nsf/[email protected]/jsondiff.go:372 +0x978
github.com/nsf/jsondiff.(*context).printDiff(0xc00017da08, {0x7e3280, 0xc0002cac30}, {0x7e3280, 0xc0002ed3e0})
	/home/leo/go/pkg/mod/github.com/nsf/[email protected]/jsondiff.go:449 +0x119f
github.com/nsf/jsondiff.(*context).printDiff(0xc00017da08, {0x7e3280, 0xc0002cac00}, {0x7e3280, 0xc0002ed3b0})
	/home/leo/go/pkg/mod/github.com/nsf/[email protected]/jsondiff.go:449 +0x119f
github.com/nsf/jsondiff.(*context).printDiff(0xc00017da08, {0x7e3280, 0xc0002cabd0}, {0x7e3280, 0xc0002ed380})
	/home/leo/go/pkg/mod/github.com/nsf/[email protected]/jsondiff.go:449 +0x119f
github.com/nsf/jsondiff.Compare({0xc0002c2000, 0x1715, 0x1800}, {0xc0002b5300, 0x11d4, 0x1300}, 0xc0002a8300)
	/home/leo/go/pkg/mod/github.com/nsf/[email protected]/jsondiff.go:555 +0x2cf
github.com/github/vulcanizer.(*Client).GetMappingDiff(0x6c8683, {0x7ffcdb7a404f, 0x21}, {0x7ffcdb7a4071, 0x22})
	/home/leo/workspace/vulcanizer/es.go:1353 +0x37f
github.com/github/vulcanizer/pkg/cli.glob..func19(0xb79e20, {0xc000064a00, 0x2, 0x2})
	/home/leo/workspace/vulcanizer/pkg/cli/mappings.go:50 +0x7a
github.com/spf13/cobra.(*Command).execute(0xb79e20, {0xc0000649c0, 0x2, 0x2})
	/home/leo/go/pkg/mod/github.com/spf13/[email protected]/command.go:846 +0x5f8
github.com/spf13/cobra.(*Command).ExecuteC(0xb7ab40)
	/home/leo/go/pkg/mod/github.com/spf13/[email protected]/command.go:950 +0x3ad
github.com/spf13/cobra.(*Command).Execute(...)
	/home/leo/go/pkg/mod/github.com/spf13/[email protected]/command.go:887
github.com/github/vulcanizer/pkg/cli.InitializeCLI({0xc0000120b0, 0xc00017df70, 0x4076d9}, {0x8db820, 0xc000010010}, {0x8db840, 0xc000010018}, {0x8db840, 0xc000010020})
	/home/leo/workspace/vulcanizer/pkg/cli/entrypoint.go:198 +0xcf1
main.main()
	/home/leo/workspace/vulcanizer/cmd/vulcanizer/main.go:10 +0x7a

I'll see if I can reproduce it with smaller JSON files.

@leosunmo
Copy link
Author

Ah, it seems you have to provide SkippedSliceString, and SkippedKeysString if you enable SkipMatches. Might be worth adding in default functions (same as you have in DefaultConsoleOptions maybe, 0e9c064#diff-ab5d2ac49314d80a4296040d50fa94c987b46467f4b69661ee9459f8e103f754R83-R98).

@leosunmo
Copy link
Author

leosunmo commented Sep 23, 2021

There also seems to be an issue with the tags.
Previous commit, 6ea32392771e7b92c36671ce0ca29e11a063fa44:
6ea32392771e7b92c36671ce0ca29e11a063fa44

Latest commit, 0e9c064b4f898a321504baefc282b270b8c98669:
0e9c064b4f898a321504baefc282b270b8c98669

This is the case even if you disable SkipMatches.

@nsf
Copy link
Owner

nsf commented Sep 23, 2021

Yes, it does look buggy. Wellll, I guess I'll dedicate some more time on the weekend to address it all. Thanks for trying it out and reporting back!

@nsf
Copy link
Owner

nsf commented Sep 25, 2021

I did a pass on fixing those issues. The tags problem should be gone now. Also SkippedKeysString and SkippedSliceString are optional now. When not present "skipped" text strings will be omitted completely.

I'm not releasing 1.0 yet. More testing is required and some more maintenance in other areas is required as well (such as making JSON formatter actually useful). I'll try to do some of it one day, but no promises.

@nsf
Copy link
Owner

nsf commented Sep 26, 2021

Added few more changes:

  • better defaults for HTML
  • restored how tags were formatted before recent changes
  • slightly renamed options, instead of using Go terms, I'm using JS terms (slice -> array, object keys -> object properties)
  • now object properites are sorted by key when just printing them out (slightly different code path, it was missing the sort part)

Also updated jsondiff demo:
https://nosmileface.dev/jsondiff/ (source code is in its own repo: https://github.com/nsf/jsondiff-demo).

Demo uses all the modern JS stuff now: wasm, preact, webpack, typescript, tailwindcss.

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

No branches or pull requests

3 participants