From 186e52fbcd7e9e6732f538c8a242aac7ec4f52a7 Mon Sep 17 00:00:00 2001 From: Ayush Sharma Date: Wed, 11 Dec 2024 14:20:49 +0530 Subject: [PATCH 1/3] fix: added regex for noise Signed-off-by: Ayush Sharma --- jsondiff.go | 70 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 55 insertions(+), 15 deletions(-) diff --git a/jsondiff.go b/jsondiff.go index d527246..7db1e9a 100644 --- a/jsondiff.go +++ b/jsondiff.go @@ -318,8 +318,13 @@ func compareAndColorizeSlices(a, b []interface{}, indent string, red, green func default: // If values are not deeply equal, write the values with colors. prefixedValue := jsonPath + "[" + fmt.Sprint(i) + "]" - isNoised := checkNoise(prefixedValue, noise) + isNoised, regexArray := checkNoise(prefixedValue, noise) + + if isNoised && len(regexArray) > 0 { + isNoised, _ = MatchesAnyRegex(InterfaceToString(aValue), regexArray) + } if reflect.DeepEqual(aValue, bValue) || isNoised { + fmt.Println("Values are equal writing to both") expectedOutput.WriteString(fmt.Sprintf("%s[%d]: %v\n", indent, i, aValue)) actualOutput.WriteString(fmt.Sprintf("%s[%d]: %v\n", indent, i, bValue)) continue @@ -355,9 +360,9 @@ func serialize(value interface{}) string { func compare(key string, val1, val2 interface{}, indent string, expect, actual *strings.Builder, red, green func(a ...interface{}) string, jsonPath string, noise map[string][]string) { jsonPath = jsonPath + "." + key - isNoised := checkNoise(jsonPath, noise) + isNoised, regexArray := checkNoise(jsonPath, noise) - if isNoised { + if isNoised && len(regexArray) == 0 { return } @@ -399,8 +404,9 @@ func compare(key string, val1, val2 interface{}, indent string, expect, actual * // Default case for other types default: + isNoised, _ := MatchesAnyRegex(InterfaceToString(val1), regexArray) // Check if the values are not deeply equal - if !reflect.DeepEqual(val1, val2) { + if !reflect.DeepEqual(val1, val2) && !isNoised { // Marshal values to pretty-printed JSON strings val1Str, err := json.MarshalIndent(val1, "", " ") if err != nil { @@ -423,12 +429,16 @@ func compare(key string, val1, val2 interface{}, indent string, expect, actual * return } // If values are equal, write the value without color - valStr, err := json.MarshalIndent(val1, "", " ") + valStr1, err := json.MarshalIndent(val1, "", " ") + if err != nil { + return + } + valStr2, err := json.MarshalIndent(val2, "", " ") if err != nil { return } - expect.WriteString(fmt.Sprintf("%s\"%s\": %s,\n", indent, key, string(valStr))) - actual.WriteString(fmt.Sprintf("%s\"%s\": %s,\n", indent, key, string(valStr))) + expect.WriteString(fmt.Sprintf("%s\"%s\": %s,\n", indent, key, string(valStr1))) + actual.WriteString(fmt.Sprintf("%s\"%s\": %s,\n", indent, key, string(valStr2))) } } @@ -517,8 +527,8 @@ func separateAndColorize(diffStr string, noise map[string][]string) (string, str if actualKey != expectKey { continue } - isNoised := checkNoise(actualKey, noise) - if isNoised { + isNoised, regexArray := checkNoise(actualKey, noise) + if isNoised && len(regexArray) == 0 { continue } expectedText, actualText = compareAndColorizeSlices(expectsArray, actualsArray, " ", red, green, intialJsonPath, noise) @@ -829,7 +839,11 @@ func compareAndColorizeMaps(a, b map[string]interface{}, indent string, red, gre if _, aHasKey := a[key]; !aHasKey { // If the key does not exist in the first map. jsonPath = jsonPath + "." + key - isNoised := checkNoise(jsonPath, noise) + isNoised, regexArray := checkNoise(jsonPath, noise) + + if len(regexArray) != 0 { + isNoised = false + } if !isNoised { writeKeyValuePair(&actualOutput, green(key), bValue, indent+" ", green) // Write the key-value pair with green color. @@ -1013,13 +1027,39 @@ func normalizeJSON(input []byte) ([]byte, error) { return buffer.Bytes(), nil } -func checkNoise(key string, noise map[string][]string) bool { +func checkNoise(key string, noise map[string][]string) (bool, []string) { key = strings.TrimPrefix(key, ".") key = strings.ToLower(key) - for e := range noise { - if strings.Contains(key, e) { - return true + + if v2, ok := noise[key]; ok { + return true, v2 + } + + return false, []string{} // Return false if no noise path matched +} + +func MatchesAnyRegex(str string, regexArray []string) (bool, string) { + for _, pattern := range regexArray { + re := regexp.MustCompile(pattern) + if re.MatchString(str) { + fmt.Println("Matched with pattern: ", pattern) + return true, pattern } } - return false // Return false if no noise path matched + return false, "" +} + +func InterfaceToString(val interface{}) string { + switch v := val.(type) { + case int: + return fmt.Sprintf("%d", v) + case float64: + return fmt.Sprintf("%f", v) + case bool: + return fmt.Sprintf("%t", v) + case string: + return v + default: + return fmt.Sprintf("%v", v) + } } From b66ddc7f10f2f737f3ebc07c87a512825d7bfaca Mon Sep 17 00:00:00 2001 From: Ayush Sharma Date: Wed, 11 Dec 2024 14:25:22 +0530 Subject: [PATCH 2/3] fix: removed debug statements Signed-off-by: Ayush Sharma --- jsondiff.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/jsondiff.go b/jsondiff.go index 7db1e9a..13bf45b 100644 --- a/jsondiff.go +++ b/jsondiff.go @@ -324,7 +324,6 @@ func compareAndColorizeSlices(a, b []interface{}, indent string, red, green func isNoised, _ = MatchesAnyRegex(InterfaceToString(aValue), regexArray) } if reflect.DeepEqual(aValue, bValue) || isNoised { - fmt.Println("Values are equal writing to both") expectedOutput.WriteString(fmt.Sprintf("%s[%d]: %v\n", indent, i, aValue)) actualOutput.WriteString(fmt.Sprintf("%s[%d]: %v\n", indent, i, bValue)) continue @@ -1042,7 +1041,6 @@ func MatchesAnyRegex(str string, regexArray []string) (bool, string) { for _, pattern := range regexArray { re := regexp.MustCompile(pattern) if re.MatchString(str) { - fmt.Println("Matched with pattern: ", pattern) return true, pattern } } From d3c9b7a346ab9f8b0e8b4a73b6b59b01f58d4bad Mon Sep 17 00:00:00 2001 From: Ayush Sharma Date: Wed, 11 Dec 2024 14:39:56 +0530 Subject: [PATCH 3/3] fix: noise Signed-off-by: Ayush Sharma --- jsondiff.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsondiff.go b/jsondiff.go index 13bf45b..95b8c7e 100644 --- a/jsondiff.go +++ b/jsondiff.go @@ -526,7 +526,7 @@ func separateAndColorize(diffStr string, noise map[string][]string) (string, str if actualKey != expectKey { continue } - isNoised, regexArray := checkNoise(actualKey, noise) + isNoised, regexArray := checkNoise(actualKey[:len(actualKey)-1], noise) if isNoised && len(regexArray) == 0 { continue }