From ddb22a81690c66cec43527275f129a86185f91c9 Mon Sep 17 00:00:00 2001 From: Zhizhen He Date: Fri, 15 May 2020 11:57:39 +0800 Subject: [PATCH] Rename parameters to indicate direction (#42) --- storage.go | 35 +++++++++++++++++++---------------- storage_test.go | 2 +- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/storage.go b/storage.go index ed6fe55d..13d5aeaa 100644 --- a/storage.go +++ b/storage.go @@ -54,31 +54,34 @@ func (object Object) HasExtension(extension string) bool { } // GetObjectSliceDiff takes two objects slices and returns an ObjectSliceDiff -func GetObjectSliceDiff(os1 []Object, os2 []Object, timestampTolerance time.Duration) ObjectSliceDiff { +func GetObjectSliceDiff(prev []Object, curr []Object, timestampTolerance time.Duration) ObjectSliceDiff { var diff ObjectSliceDiff - om1 := make(map[string]Object) - om2 := make(map[string]Object) - for _, o1 := range os1 { - om1[o1.Path] = o1 + pos := make(map[string]Object) + cos := make(map[string]Object) + for _, o := range prev { + pos[o.Path] = o } - for _, o2 := range os2 { - om2[o2.Path] = o2 + for _, o := range curr { + cos[o.Path] = o } - for _, o1 := range os1 { - if o2, found := om2[o1.Path]; found { - if o2.LastModified.Sub(o1.LastModified) > timestampTolerance { - diff.Updated = append(diff.Updated, o2) + // for every object in the previous slice, if it exists in the current slice, check if it is *considered as* updated; + // otherwise, mark it as removed + for _, p := range prev { + if c, found := cos[p.Path]; found { + if c.LastModified.Sub(p.LastModified) > timestampTolerance { + diff.Updated = append(diff.Updated, c) } } else { - diff.Removed = append(diff.Removed, o1) + diff.Removed = append(diff.Removed, p) } } - - for _, o2 := range os2 { - if _, found := om1[o2.Path]; !found { - diff.Added = append(diff.Added, o2) + // for every object in the current slice, if it does not exist in the previous slice, mark it as added + for _, c := range curr { + if _, found := pos[c.Path]; !found { + diff.Added = append(diff.Added, c) } } + // if any object is marked as removed or added or updated, set change to true diff.Change = len(diff.Removed)+len(diff.Added)+len(diff.Updated) > 0 return diff } diff --git a/storage_test.go b/storage_test.go index d9f6d9cd..0391b559 100644 --- a/storage_test.go +++ b/storage_test.go @@ -192,7 +192,7 @@ func (suite *StorageTestSuite) TestGetObjectSliceDiff() { LastModified: now, }, } - os2 := []Object{} + var os2 []Object diff := GetObjectSliceDiff(os1, os2, time.Duration(0)) suite.True(diff.Change, "change detected") suite.Equal(diff.Removed, os1, "removed slice populated")