Skip to content

Commit

Permalink
fielddiff and fulldiff share a method getdiff. fixed comparison betwe…
Browse files Browse the repository at this point in the history
…en maps compareMaps.
  • Loading branch information
djarotech committed Aug 31, 2018
1 parent d372c69 commit a31cb70
Showing 1 changed file with 12 additions and 53 deletions.
65 changes: 12 additions & 53 deletions pkg/provenance/provenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,52 +455,7 @@ func (o ObjectLineage) FullDiff(vNumStart, vNumEnd int) string {
for attribute, data1 := range sp1.AttributeToData {
data2, ok := sp2.AttributeToData[attribute] //check if the attribute even exists
if ok {
str1, ok1 := data1.(string)
str2, ok2 := data2.(string)
if ok1 && ok2 && str1 != str2 {
fmt.Fprintf(&b, "Found diff on attribute %s:\n", attribute)
fmt.Fprintf(&b, "\tVersion %d: %s\n", vNumStart, data1)
fmt.Fprintf(&b, "\tVersion %d: %s\n", vNumEnd, data2)
}
int1, ok1 := data1.(int)
int2, ok2 := data2.(int)
if ok1 && ok2 && int1 != int2 {
fmt.Fprintf(&b, "Found diff on attribute %s:\n", attribute)
fmt.Fprintf(&b, "\tVersion %d: %d\n", vNumStart, int1)
fmt.Fprintf(&b, "\tVersion %d: %d\n", vNumEnd, int2)
}
strArray1, ok1 := data1.([]string)
strArray2, ok2 := data2.([]string)
if ok1 && ok2 {
for _, str := range strArray1 {
found := false
for _, val := range strArray2 {
if str == val {
found = true
}
}
if !found { // if an element does not have a match in the next version
fmt.Fprintf(&b, "Found diff on attribute %s:\n", attribute)
fmt.Fprintf(&b, "\tVersion %d: %s\n", vNumStart, strArray1)
fmt.Fprintf(&b, "\tVersion %d: %s\n", vNumEnd, strArray2)
}
}
}
strMap1, ok1 := data1.([]map[string]string)
strMap2, ok2 := data2.([]map[string]string)
if ok1 && ok2 {
if len(strMap1) != len(strMap2) {
fmt.Fprintf(&b, "Found diff on attribute %s:\n", attribute)
fmt.Fprintf(&b, "\tVersion %d: %s\n", vNumStart, strMap1)
fmt.Fprintf(&b, "\tVersion %d: %s\n", vNumEnd, strMap2)
}
if !compareMaps(strMap1,strMap2){
fmt.Fprintf(&b, "Found diff on attribute %s:\n", attribute)
fmt.Fprintf(&b, "\tVersion %d: %s\n", vNumStart, strMap1)
fmt.Fprintf(&b, "\tVersion %d: %s\n", vNumEnd, strMap2)
}
}

getDiff(b, attribute, data1, data2, vNumStart, vNumEnd)
} else { //for the case where a key exists in spec 1 that doesn't exist in spec 2
fmt.Fprintf(&b, "Found diff on attribute %s:\n", attribute)
fmt.Fprintf(&b, "\tVersion %d: %s\n", vNumStart, data1)
Expand All @@ -517,13 +472,7 @@ func (o ObjectLineage) FullDiff(vNumStart, vNumEnd int) string {
}
return b.String()
}
func (o ObjectLineage) FieldDiff(fieldName string, vNumStart, vNumEnd int) string {
var b strings.Builder
data1, ok1 := o[vNumStart].AttributeToData[fieldName]
data2, ok2 := o[vNumEnd].AttributeToData[fieldName]

switch {
case ok1 && ok2:
func getDiff(b strings.Builder, fieldName string, data1, data2 interface{}, vNumStart, vNumEnd int) string{
str1, ok1 := data1.(string)
str2, ok2 := data2.(string)
if ok1 && ok2 && str1 != str2 {
Expand Down Expand Up @@ -570,6 +519,16 @@ func (o ObjectLineage) FieldDiff(fieldName string, vNumStart, vNumEnd int) strin
}
}


return b.String()
}
func (o ObjectLineage) FieldDiff(fieldName string, vNumStart, vNumEnd int) string {
var b strings.Builder
data1, ok1 := o[vNumStart].AttributeToData[fieldName]
data2, ok2 := o[vNumEnd].AttributeToData[fieldName]
switch {
case ok1 && ok2:
return getDiff(b, fieldName,data1, data2, vNumStart, vNumEnd)
case !ok1 && ok2:
fmt.Fprintf(&b, "Found diff on attribute %s:\n", fieldName)
fmt.Fprintf(&b, "\tVersion %d: %s\n", vNumStart, "No attribute found.")
Expand Down

0 comments on commit a31cb70

Please sign in to comment.