From baad03555d6efa8e3ce091fc7b85eb47eaa8e9bc Mon Sep 17 00:00:00 2001 From: Yash Khare Date: Tue, 25 Mar 2025 20:08:15 +0530 Subject: [PATCH 1/5] fix: nil checks for expected type Signed-off-by: Yash Khare --- jsondiff.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/jsondiff.go b/jsondiff.go index d527246..49dd721 100644 --- a/jsondiff.go +++ b/jsondiff.go @@ -815,11 +815,18 @@ func compareAndColorizeMaps(a, b map[string]interface{}, indent string, red, gre // Iterate over each key-value pair in the first map. for key, aValue := range a { bValue, bHasKey := b[key] // Get the corresponding value from the second map and check if the key exists. - if !bHasKey { // If the key does not exist in the second map. + if aValue == nil && !bHasKey { + expectedOutput.WriteString(fmt.Sprintf("%s\"%s\": \"Unsupported Type\",\n", indent+" ", red(key))) + continue + } + if aValue == nil && bValue == nil { + continue + } + if !bHasKey { // If the key does not exist in the second map. + fmt.Println("idhar check for ", key) writeKeyValuePair(&expectedOutput, red(key), aValue, indent+" ", red) // Write the key-value pair with red color. continue // Move to the next key-value pair. } - // Compare the values for the current key in both maps. compare(key, aValue, bValue, indent+" ", &expectedOutput, &actualOutput, red, green, jsonPath, noise) } From 11c91330edc44105bdcc1ee2f11deeaf6134e5bf Mon Sep 17 00:00:00 2001 From: Yash Khare Date: Tue, 25 Mar 2025 21:25:06 +0530 Subject: [PATCH 2/5] remove extra fmt Signed-off-by: Yash Khare --- jsondiff.go | 1 - 1 file changed, 1 deletion(-) diff --git a/jsondiff.go b/jsondiff.go index 49dd721..2628e98 100644 --- a/jsondiff.go +++ b/jsondiff.go @@ -823,7 +823,6 @@ func compareAndColorizeMaps(a, b map[string]interface{}, indent string, red, gre continue } if !bHasKey { // If the key does not exist in the second map. - fmt.Println("idhar check for ", key) writeKeyValuePair(&expectedOutput, red(key), aValue, indent+" ", red) // Write the key-value pair with red color. continue // Move to the next key-value pair. } From 94e55f2953689c1465e28b6b73378499e07dc2a6 Mon Sep 17 00:00:00 2001 From: Yash Khare Date: Wed, 26 Mar 2025 12:17:21 +0530 Subject: [PATCH 3/5] fix: reverse condition for null checks Signed-off-by: Yash Khare --- jsondiff.go | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/jsondiff.go b/jsondiff.go index 2628e98..e4e2f03 100644 --- a/jsondiff.go +++ b/jsondiff.go @@ -225,27 +225,31 @@ func extractKey(diffString string) string { // colorFunc: The function to apply color to the value, if provided. func writeKeyValuePair(builder *strings.Builder, key string, value interface{}, indent string, applyColor func(a ...interface{}) string) { // Serialize the value to a pretty-printed JSON string. - switch reflect.TypeOf(value).Kind() { - case reflect.Map: - formattedValue := applyColor("{ ... }") - - builder.WriteString(fmt.Sprintf("%s\"%s\": %s,\n", indent, key, formattedValue)) - case reflect.Slice: - formattedValue := applyColor("[ ... ]") - - builder.WriteString(fmt.Sprintf("%s\"%s\": %s,\n", indent, key, formattedValue)) - default: - - serializedValue, _ := json.MarshalIndent(value, "", " ") - formattedValue := string(serializedValue) - - // Check if a color function is provided and the value is not empty. - if applyColor != nil && value != "" { - formattedValue = applyColor(formattedValue) + if (value ==nil){ + builder.WriteString(fmt.Sprintf("%s\"%s\": %s,\n", indent, key, "null")) + }else{ + switch reflect.TypeOf(value).Kind() { + case reflect.Map: + formattedValue := applyColor("{ ... }") + + builder.WriteString(fmt.Sprintf("%s\"%s\": %s,\n", indent, key, formattedValue)) + case reflect.Slice: + formattedValue := applyColor("[ ... ]") + + builder.WriteString(fmt.Sprintf("%s\"%s\": %s,\n", indent, key, formattedValue)) + default: + + serializedValue, _ := json.MarshalIndent(value, "", " ") + formattedValue := string(serializedValue) + + // Check if a color function is provided and the value is not empty. + if applyColor != nil && value != "" { + formattedValue = applyColor(formattedValue) + } + + // Write the key-value pair to the builder with or without colorization. + builder.WriteString(fmt.Sprintf("%s\"%s\": %s,\n", indent, key, formattedValue)) } - - // Write the key-value pair to the builder with or without colorization. - builder.WriteString(fmt.Sprintf("%s\"%s\": %s,\n", indent, key, formattedValue)) } } @@ -817,6 +821,7 @@ func compareAndColorizeMaps(a, b map[string]interface{}, indent string, red, gre bValue, bHasKey := b[key] // Get the corresponding value from the second map and check if the key exists. if aValue == nil && !bHasKey { expectedOutput.WriteString(fmt.Sprintf("%s\"%s\": \"Unsupported Type\",\n", indent+" ", red(key))) + actualOutput.WriteString(fmt.Sprintf("%s\"%s\": null,\n", indent+" ", red(key))) continue } if aValue == nil && bValue == nil { From 55a53859bdfdd75148e5c864450a60ed71734b1f Mon Sep 17 00:00:00 2001 From: Yash Khare Date: Wed, 26 Mar 2025 12:18:44 +0530 Subject: [PATCH 4/5] refactor: return check Signed-off-by: Yash Khare --- jsondiff.go | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/jsondiff.go b/jsondiff.go index e4e2f03..71fb915 100644 --- a/jsondiff.go +++ b/jsondiff.go @@ -225,31 +225,31 @@ func extractKey(diffString string) string { // colorFunc: The function to apply color to the value, if provided. func writeKeyValuePair(builder *strings.Builder, key string, value interface{}, indent string, applyColor func(a ...interface{}) string) { // Serialize the value to a pretty-printed JSON string. - if (value ==nil){ + if value == nil { builder.WriteString(fmt.Sprintf("%s\"%s\": %s,\n", indent, key, "null")) - }else{ - switch reflect.TypeOf(value).Kind() { - case reflect.Map: - formattedValue := applyColor("{ ... }") - - builder.WriteString(fmt.Sprintf("%s\"%s\": %s,\n", indent, key, formattedValue)) - case reflect.Slice: - formattedValue := applyColor("[ ... ]") - - builder.WriteString(fmt.Sprintf("%s\"%s\": %s,\n", indent, key, formattedValue)) - default: - - serializedValue, _ := json.MarshalIndent(value, "", " ") - formattedValue := string(serializedValue) - - // Check if a color function is provided and the value is not empty. - if applyColor != nil && value != "" { - formattedValue = applyColor(formattedValue) - } - - // Write the key-value pair to the builder with or without colorization. - builder.WriteString(fmt.Sprintf("%s\"%s\": %s,\n", indent, key, formattedValue)) + return + } + switch reflect.TypeOf(value).Kind() { + case reflect.Map: + formattedValue := applyColor("{ ... }") + + builder.WriteString(fmt.Sprintf("%s\"%s\": %s,\n", indent, key, formattedValue)) + case reflect.Slice: + formattedValue := applyColor("[ ... ]") + + builder.WriteString(fmt.Sprintf("%s\"%s\": %s,\n", indent, key, formattedValue)) + default: + + serializedValue, _ := json.MarshalIndent(value, "", " ") + formattedValue := string(serializedValue) + + // Check if a color function is provided and the value is not empty. + if applyColor != nil && value != "" { + formattedValue = applyColor(formattedValue) } + + // Write the key-value pair to the builder with or without colorization. + builder.WriteString(fmt.Sprintf("%s\"%s\": %s,\n", indent, key, formattedValue)) } } From 0002f2e38bf96c165cd52b85bebcf3243b89fc80 Mon Sep 17 00:00:00 2001 From: Yash Khare Date: Wed, 26 Mar 2025 13:00:49 +0530 Subject: [PATCH 5/5] reverse actual, expected Signed-off-by: Yash Khare --- jsondiff.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/jsondiff.go b/jsondiff.go index 71fb915..c1eb52d 100644 --- a/jsondiff.go +++ b/jsondiff.go @@ -226,7 +226,7 @@ func extractKey(diffString string) string { func writeKeyValuePair(builder *strings.Builder, key string, value interface{}, indent string, applyColor func(a ...interface{}) string) { // Serialize the value to a pretty-printed JSON string. if value == nil { - builder.WriteString(fmt.Sprintf("%s\"%s\": %s,\n", indent, key, "null")) + builder.WriteString(fmt.Sprintf("%s\"%s\": null,\n", indent, key)) return } switch reflect.TypeOf(value).Kind() { @@ -820,8 +820,8 @@ func compareAndColorizeMaps(a, b map[string]interface{}, indent string, red, gre for key, aValue := range a { bValue, bHasKey := b[key] // Get the corresponding value from the second map and check if the key exists. if aValue == nil && !bHasKey { - expectedOutput.WriteString(fmt.Sprintf("%s\"%s\": \"Unsupported Type\",\n", indent+" ", red(key))) - actualOutput.WriteString(fmt.Sprintf("%s\"%s\": null,\n", indent+" ", red(key))) + actualOutput.WriteString(fmt.Sprintf("%s\"%s\": \"Unsupported Type\",\n", indent+" ", red(key))) + expectedOutput.WriteString(fmt.Sprintf("%s\"%s\": null,\n", indent+" ", key)) continue } if aValue == nil && bValue == nil {