From a31cb708f80fbfe8765c07e55a24d46953e4d3d5 Mon Sep 17 00:00:00 2001 From: danielpygo Date: Fri, 31 Aug 2018 17:16:53 -0500 Subject: [PATCH] fielddiff and fulldiff share a method getdiff. fixed comparison between maps compareMaps. --- pkg/provenance/provenance.go | 65 +++++++----------------------------- 1 file changed, 12 insertions(+), 53 deletions(-) diff --git a/pkg/provenance/provenance.go b/pkg/provenance/provenance.go index e5b392e..92a1837 100644 --- a/pkg/provenance/provenance.go +++ b/pkg/provenance/provenance.go @@ -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) @@ -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 { @@ -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.")