Skip to content

Commit

Permalink
Merge pull request #36 from aereal/use-del-2
Browse files Browse the repository at this point in the history
feat: use del() function to ignore the member instead of assigning null
  • Loading branch information
aereal authored Jan 15, 2025
2 parents 72b6e2a + 536d86a commit 3ff0e31
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 17 deletions.
37 changes: 24 additions & 13 deletions cmd/jsondiff/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@ package main

import (
"bytes"
"os"
"testing"

"github.com/google/go-cmp/cmp"
)

func Test(t *testing.T) {
type testCase struct {
name string
argv []string
wantStatus int
wantOut string
wantErr string
name string
argv []string
wantStatus int
wantOutPath string
wantErr string
}
testCases := []testCase{
{"ok; no queries", []string{"jsondiff", "../../testdata/from.json", "../../testdata/to.json"}, codeOK, "--- from.json\n+++ to.json\n@@ -1,7 +1,7 @@\n {\n \"a\": 1,\n- \"b\": 2,\n- \"c\": 3,\n- \"d\": 4\n+ \"b\": 1,\n+ \"c\": 2,\n+ \"d\": 3\n }\n \n", ""},
{"ok; with -exit-code", []string{"jsondiff", "-exit-code", "../../testdata/from.json", "../../testdata/to.json"}, codeHaveDifferences, "--- from.json\n+++ to.json\n@@ -1,7 +1,7 @@\n {\n \"a\": 1,\n- \"b\": 2,\n- \"c\": 3,\n- \"d\": 4\n+ \"b\": 1,\n+ \"c\": 2,\n+ \"d\": 3\n }\n \n", ""},
{"ok; with -only option", []string{"jsondiff", "-only", ".d", "../../testdata/from.json", "../../testdata/to.json"}, codeOK, "--- from.json\n+++ to.json\n@@ -1,2 +1,2 @@\n-4\n+3\n \n", ""},
{"ok; with -ignore option", []string{"jsondiff", "-ignore", ".d", "../../testdata/from.json", "../../testdata/to.json"}, codeOK, "--- from.json\n+++ to.json\n@@ -1,7 +1,7 @@\n {\n \"a\": 1,\n- \"b\": 2,\n- \"c\": 3,\n+ \"b\": 1,\n+ \"c\": 2,\n \"d\": null\n }\n \n", ""},
{"ok; no queries", []string{"jsondiff", "../../testdata/from.json", "../../testdata/to.json"}, codeOK, "./testdata/ok_no_queries.stdout.txt", ""},
{"ok; with -exit-code", []string{"jsondiff", "-exit-code", "../../testdata/from.json", "../../testdata/to.json"}, codeHaveDifferences, "./testdata/ok_with_exit_code.stdout.txt", ""},
{"ok; with -only option", []string{"jsondiff", "-only", ".d", "../../testdata/from.json", "../../testdata/to.json"}, codeOK, "./testdata/ok_with_only_option.stdout.txt", ""},
{"ok; with -ignore option", []string{"jsondiff", "-ignore", ".d", "../../testdata/from.json", "../../testdata/to.json"}, codeOK, "./testdata/ok_with_ignore_option.stdout.txt", ""},
{"no paths given", []string{"jsondiff"}, codeError, "", "2 file paths must be passed\n"},
}
for _, tc := range testCases {
Expand All @@ -33,11 +34,21 @@ func Test(t *testing.T) {
if gotStatus != tc.wantStatus {
t.Errorf("status code: got=%d want=%d", tc.wantStatus, gotStatus)
}
if diff := cmp.Diff(tc.wantOut, outStream.String()); diff != "" {
t.Errorf("output:\n%s", diff)
var wantOut string
if tc.wantOutPath != "" {
out, err := os.ReadFile(tc.wantOutPath)
if err != nil {
t.Fatal(err)
}
wantOut = string(out)
}
if diff := cmp.Diff(tc.wantErr, errStream.String()); diff != "" {
t.Errorf("error:\n%s", diff)
gotOut := outStream.String()
if diff := cmp.Diff(wantOut, gotOut); diff != "" {
t.Errorf("output (-want, +got):\n%s", diff)
}
gotErr := errStream.String()
if diff := cmp.Diff(tc.wantErr, gotErr); diff != "" {
t.Errorf("error (-want, +got):\n%s", diff)
}
})
}
Expand Down
13 changes: 13 additions & 0 deletions cmd/jsondiff/testdata/ok_no_queries.stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--- from.json
+++ to.json
@@ -1,7 +1,7 @@
{
"a": 1,
- "b": 2,
- "c": 3,
- "d": 4
+ "b": 1,
+ "c": 2,
+ "d": 3
}

13 changes: 13 additions & 0 deletions cmd/jsondiff/testdata/ok_with_exit_code.stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--- from.json
+++ to.json
@@ -1,7 +1,7 @@
{
"a": 1,
- "b": 2,
- "c": 3,
- "d": 4
+ "b": 1,
+ "c": 2,
+ "d": 3
}

11 changes: 11 additions & 0 deletions cmd/jsondiff/testdata/ok_with_ignore_option.stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--- from.json
+++ to.json
@@ -1,6 +1,6 @@
{
"a": 1,
- "b": 2,
- "c": 3
+ "b": 1,
+ "c": 2
}

6 changes: 6 additions & 0 deletions cmd/jsondiff/testdata/ok_with_only_option.stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
--- from.json
+++ to.json
@@ -1,2 +1,2 @@
-4
+3

17 changes: 16 additions & 1 deletion diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func Diff(from, to *Input, opts ...Option) (string, error) {
switch {
case o.ignore != nil:
var err error
q := WithUpdate(o.ignore)
q := deleting(o.ignore)
fromObj, err = ModifyValue(q, fromObj)
if err != nil {
return "", fmt.Errorf("modify(lhs): %v", err)
Expand Down Expand Up @@ -163,6 +163,9 @@ func toJSON(x interface{}) (string, error) {
return b.String(), nil
}

// WithUpdate is a internal helper function.
//
// Deprecated: WithUpdate will be deleted in the future.
func WithUpdate(query *gojq.Query) *gojq.Query {
var ret *gojq.Query
qs := splitIntoTerms(query)
Expand All @@ -186,6 +189,18 @@ func WithUpdate(query *gojq.Query) *gojq.Query {
return ret
}

func deleting(query *gojq.Query) *gojq.Query {
return &gojq.Query{
Term: &gojq.Term{
Type: gojq.TermTypeFunc,
Func: &gojq.Func{
Name: "del",
Args: []*gojq.Query{query},
},
},
}
}

var nullRhs *gojq.Query

func init() {
Expand Down
22 changes: 22 additions & 0 deletions diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,28 @@ func Test_withUpdate(t *testing.T) {
}
}

func Test_deleting(t *testing.T) {
queries := []struct {
query string
want string
}{
{".age", "del(.age)"},
{".age, .name", "del(.age, .name)"},
{".age, .name, .meta", "del(.age, .name, .meta)"},
{".meta[]", "del(.meta[])"},
{".meta[0:-1]", "del(.meta[0:-1])"},
}
for _, c := range queries {
t.Run(c.query, func(t *testing.T) {
want := parseQuery(t, c.want)
got := deleting(parseQuery(t, c.query))
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("-want, +got:\n%s", diff)
}
})
}
}

func parseQuery(t *testing.T, q string) *gojq.Query {
t.Helper()
parsed, err := gojq.Parse(q)
Expand Down
5 changes: 2 additions & 3 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ func ExampleDiffFromObjects_ignore() {
// Output:
// --- from
// +++ to
// @@ -2,6 +2,6 @@
// @@ -1,5 +1,5 @@
// {
// "a": 1,
// "b": null,
// "c": null,
// - "d": 4
// + "d": 3
// }
Expand Down

0 comments on commit 3ff0e31

Please sign in to comment.