Skip to content

Commit d3ff0ab

Browse files
committed
Update the compare/diff function signature step by step.
1 parent 3d74acf commit d3ff0ab

File tree

7 files changed

+42
-7
lines changed

7 files changed

+42
-7
lines changed

builtin/compare.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package builtin
22

3-
func Compare(x, y any, ignorePaths ...string) (bool, error) {
4-
d, err := Diff(x, y, ignorePaths...)
3+
func Compare(x, y any, ignores ...any) (bool, error) {
4+
d, err := Diff(x, y, ignores...)
55
if err != nil {
66
return false, err
77
}

builtin/compare_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func TestCompareWithIgnorePathOrKeys(t *testing.T) {
9090
}
9191
for i, tt := range tests {
9292
t.Run(fmt.Sprintf("Case %d", i), func(t *testing.T) {
93-
got, err := Compare(tt.x, tt.y, tt.ignorePathOrKeys...)
93+
got, err := Compare(tt.x, tt.y, tt.ignorePathOrKeys)
9494
if err != nil {
9595
t.Error(err)
9696
}

builtin/diff.go

+20-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,25 @@ import (
1212
"github.com/itchyny/gojq"
1313
)
1414

15-
func Diff(x, y any, ignorePaths ...string) (string, error) {
15+
func Diff(x, y any, ignores ...any) (string, error) {
16+
var ignoreSpecifiers []string
17+
for _, i := range ignores {
18+
switch v := i.(type) {
19+
case string:
20+
ignoreSpecifiers = append(ignoreSpecifiers, v)
21+
case []any:
22+
for _, vv := range v {
23+
s, ok := vv.(string)
24+
if !ok {
25+
return "", fmt.Errorf("invalid ignore specifiers: %v", vv)
26+
}
27+
ignoreSpecifiers = append(ignoreSpecifiers, s)
28+
}
29+
default:
30+
return "", fmt.Errorf("invalid ignore specifiers: %v", i)
31+
}
32+
}
33+
1634
impl := diffImpl{}
1735

1836
// normalize values
@@ -25,7 +43,7 @@ func Diff(x, y any, ignorePaths ...string) (string, error) {
2543
return "", err
2644
}
2745

28-
jqIgnorePaths, cmpIgnoreKeys := impl.splitIgnoreSpecifiers(ignorePaths)
46+
jqIgnorePaths, cmpIgnoreKeys := impl.splitIgnoreSpecifiers(ignoreSpecifiers)
2947

3048
var diffOpts []cmp.Option
3149
if len(cmpIgnoreKeys) >= 0 {

builtin/diff_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func TestDiffWithIgnorePathOrKeys(t *testing.T) {
164164
}
165165
for i, tt := range tests {
166166
t.Run(fmt.Sprintf("Case %d", i), func(t *testing.T) {
167-
got, err := Diff(tt.x, tt.y, tt.ignorePathOrKeys...)
167+
got, err := Diff(tt.x, tt.y, tt.ignorePathOrKeys)
168168
if err != nil {
169169
t.Error(err)
170170
}

eval.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func onPrintTraceCallNode(tp exprtrace.TreePrinter, tree treeprint.Tree, callNod
262262
}
263263
}
264264

265-
diff, _ := builtin.Diff(a, b, ignoreKeys...) //nostyle:handlerror
265+
diff, _ := builtin.Diff(a, b, ignoreKeys) //nostyle:handlerror
266266

267267
// Normalize NBSP to SPACE
268268
diff = strings.Map(func(r rune) rune {

option_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,7 @@ func TestBuiltinFunctionBooks(t *testing.T) {
10441044
{"testdata/book/builtin_pick.yml", false},
10451045
{"testdata/book/builtin_omit.yml", false},
10461046
{"testdata/book/builtin_merge.yml", false},
1047+
{"testdata/book/builtin_compare.yml", false},
10471048
}
10481049
ctx := context.Background()
10491050
for _, tt := range tests {

testdata/book/builtin_compare.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
desc: For compare() built-in function
2+
steps:
3+
merge:
4+
test: |
5+
compare(
6+
{"a": 1, "b": 3, "c": 5},
7+
{"a": 1, "b": 2, "c": 4},
8+
"b", "c"
9+
)
10+
merge2:
11+
test: |
12+
compare(
13+
{"a": 1, "b": 3, "c": 5},
14+
{"a": 1, "b": 2, "c": 4},
15+
["b", "c"]
16+
)

0 commit comments

Comments
 (0)