Skip to content

Commit

Permalink
Added small refactor to the json comparator (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmartin82 authored May 27, 2019
1 parent dfc1cc8 commit 1878ed6
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions pkg/match/payload/json_comparator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ package payload
import (
"encoding/json"
"reflect"
"strings"
)

type JSONComparator struct {
}

func isArray(s1 string) bool {
return len(s1) > 0 && s1[0] == '['
func isArray(s string) bool {
st := strings.TrimLeft(s, " ")
return len(st) > 0 && st[0] == '['
}

func (jc *JSONComparator) compareArray(s1, s2 string) bool {

var o1 []interface{}
var o2 []interface{}

func (js *JSONComparator) doCompare(s1, s2 string, o1, o2 interface{}) bool {
var err error
err = json.Unmarshal([]byte(s1), &o1)
if err != nil {
Expand All @@ -32,22 +30,18 @@ func (jc *JSONComparator) compareArray(s1, s2 string) bool {

func (jc *JSONComparator) Compare(s1, s2 string) bool {

if isArray(s1) != isArray(s2) {
return false
}

if isArray(s1) || isArray(s2) {
return jc.compareArray(s1, s2)
var o1 []interface{}
var o2 []interface{}
return jc.doCompare(s1, s2, o1, o2)
}

var o1 interface{}
var o2 interface{}

var err error
err = json.Unmarshal([]byte(s1), &o1)
if err != nil {
return false
}
err = json.Unmarshal([]byte(s2), &o2)
if err != nil {
return false
}

return reflect.DeepEqual(o1, o2)
}
return jc.doCompare(s1, s2, o1, o2)
}

0 comments on commit 1878ed6

Please sign in to comment.