Repository: mbrt/gmailctl
Pull request #114 (2020-04-29 17:24:45)

Remove structhash dependency.
Hashing a struct can be done in 3 lines, so dropping the dependency.


Patch:
diff --git a/go.mod b/go.mod
index 32f4e02..011bca8 100644
--- a/go.mod
+++ b/go.mod
@@ -4,7 +4,6 @@ go 1.12
 
 require (
 	cloud.google.com/go v0.41.0 // indirect
-	github.com/cnf/structhash v0.0.0-20180104161610-62a607eb0224
 	github.com/go-bindata/go-bindata v3.1.2+incompatible // indirect
 	github.com/google/go-jsonnet v0.15.0
 	github.com/hashicorp/go-multierror v1.1.0
diff --git a/go.sum b/go.sum
index dcef4c3..30d32b8 100644
--- a/go.sum
+++ b/go.sum
@@ -16,8 +16,6 @@ github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+Ce
 github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
 github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
 github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
-github.com/cnf/structhash v0.0.0-20180104161610-62a607eb0224 h1:rnCKRrdSBqc061l0CDuYB+7X3w6w8IK/VCSChJXv62g=
-github.com/cnf/structhash v0.0.0-20180104161610-62a607eb0224/go.mod h1:pCxVEbcm3AMg7ejXyorUXi6HQCzOIBf7zEDVPtw0/U4=
 github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
 github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
 github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
diff --git a/pkg/filter/diff.go b/pkg/filter/diff.go
index d07dc1f..31dc31a 100644
--- a/pkg/filter/diff.go
+++ b/pkg/filter/diff.go
@@ -1,11 +1,11 @@
 package filter
 
 import (
+	"crypto/sha256"
 	"fmt"
 	"sort"
 	"strings"
 
-	"github.com/cnf/structhash"
 	"github.com/pmezard/go-difflib/difflib"
 
 	"github.com/mbrt/gmailctl/pkg/graph"
@@ -145,13 +145,19 @@ func hashFilter(f Filter) hashedFilter {
 		Action:   f.Action,
 		Criteria: f.Criteria,
 	}
-	h, err := structhash.Hash(noIDFilter, 1)
-	if err != nil {
-		panic("hash cannot fail, unreachable")
-	}
+	h := hashStruct(noIDFilter)
 	return hashedFilter{h, f}
 }
 
+func hashStruct(a interface{}) string {
+	h := sha256.New()
+	if _, err := h.Write([]byte(fmt.Sprintf("%#v", a))); err != nil {
+		// This should be unreachable.
+		panic(err)
+	}
+	return fmt.Sprintf("%x", h.Sum(nil))
+}
+
 // reorderWithHungarian reorders the two lists to make them look as similar as
 // possible based on the hungarian algorithm.
 //


Test patch:
diff --git a/pkg/filter/diff_test.go b/pkg/filter/diff_test.go
index 3305a89..b0ac2d7 100644
--- a/pkg/filter/diff_test.go
+++ b/pkg/filter/diff_test.go
@@ -289,7 +289,7 @@ func TestDiffRemove(t *testing.T) {
 
 	fd, err := Diff(old, new)
 	expected := FiltersDiff{
-		Removed: Filters{old[0], old[2]},
+		Removed: Filters{old[2], old[0]},
 	}
 
 	assert.Nil(t, err)
diff --git a/testdata/local.bigdiff.diff b/testdata/local.bigdiff.diff
index 88aa026..3b3228b 100644
--- a/testdata/local.bigdiff.diff
+++ b/testdata/local.bigdiff.diff
@@ -3,23 +3,23 @@ Filters:
 +++ TO BE APPLIED
 @@ -1,32 +1,55 @@
  * Criteria:
--    query: {"buy this thing" "very important!!!"}
-+    query: list:foobaz.mail.com -"action needed"
+-    from: {spammer1 spammer2}
++    from: spammer2
    Actions:
      delete
  
  * Criteria:
-+    from: spammer1
-     subject: "spam mail"
+-    query: {"buy this thing" "very important!!!"}
++    query: "buy this thing"
    Actions:
      delete
  
  * Criteria:
--    to: myalias@gmail.com
--    query: list:{list1 list2 list3}
-+    from: baz+zuz@mail.com
+-    query: list:{list1 list2 list3} -to:{pippo@gmail.com pippo@hotmail.com}
++    query: list:{list3 list1 list4 list6} -to:none@gmail.com
    Actions:
-     mark as important
+-    apply label: onemorelabel
++    apply label: thirdlabel
  
  * Criteria:
 -    query: list:{list1 list2 list3} -to:{pippo@gmail.com pippo@hotmail.com}
@@ -29,40 +29,37 @@ Filters:
      apply label: maillist
  
  * Criteria:
--    query: list:{list1 list2 list3} -to:{pippo@gmail.com pippo@hotmail.com}
--  Actions:
--    apply label: onemorelabel
--
--* Criteria:
--    from: {spammer1 spammer2}
-+    query: "buy this thing"
+-    to: myalias@gmail.com
+-    query: list:{list1 list2 list3}
++    from: baz+zuz@mail.com
+   Actions:
+     mark as important
+ 
+ * Criteria:
++    from: spammer1
+     subject: "spam mail"
    Actions:
      delete
  
-+* Criteria:
-+    from: spammer2
-+  Actions:
-+    delete
-+
 +* Criteria:
 +    query: list:{list3 list1 list4 list6} -to:none@gmail.com
 +  Actions:
 +    apply label: differentlabel
 +
 +* Criteria:
-+    from: notfriend@gmail.com
-+    subject: "hey there"
++    query: list:foobaz.mail.com -"action needed"
 +  Actions:
-+    archive
-+    star
++    delete
 +
 +* Criteria:
-+    query: list:{list3 list1 list4 list6} -to:none@gmail.com
++    query: -to:none@gmail.com
 +  Actions:
-+    apply label: thirdlabel
++    archive
++    star
 +
 +* Criteria:
-+    query: -to:none@gmail.com
++    from: notfriend@gmail.com
++    subject: "hey there"
 +  Actions:
 +    archive
 +    star
diff --git a/testdata/local.bigdiffj.diff b/testdata/local.bigdiffj.diff
index 404c2e8..06413d9 100644
--- a/testdata/local.bigdiffj.diff
+++ b/testdata/local.bigdiffj.diff
@@ -2,6 +2,12 @@ Filters:
 --- Current
 +++ TO BE APPLIED
 @@ -1,32 +1,52 @@
+ * Criteria:
+-    from: {spammer1 spammer2}
++    from: spammer2
+   Actions:
+     delete
+ 
  * Criteria:
 +    from: spammer1
      subject: "spam mail"
@@ -27,22 +33,18 @@ Filters:
 -    query: list:{list1 list2 list3} -to:{pippo@gmail.com pippo@hotmail.com}
 +    query: list:{list3 list1 list4 list6} -to:none@gmail.com
    Actions:
-     archive
-     apply label: maillist
+-    apply label: onemorelabel
++    apply label: thirdlabel
  
  * Criteria:
 -    query: list:{list1 list2 list3} -to:{pippo@gmail.com pippo@hotmail.com}
--  Actions:
--    apply label: onemorelabel
--
--* Criteria:
--    from: {spammer1 spammer2}
-+    query: "buy this thing"
++    query: list:{list3 list1 list4 list6} -to:none@gmail.com
    Actions:
-     delete
+     archive
+     apply label: maillist
  
 +* Criteria:
-+    from: spammer2
++    query: "buy this thing"
 +  Actions:
 +    delete
 +
@@ -52,11 +54,6 @@ Filters:
 +    apply label: differentlabel
 +
 +* Criteria:
-+    query: list:{list3 list1 list4 list6} -to:none@gmail.com
-+  Actions:
-+    apply label: thirdlabel
-+
-+* Criteria:
 +    from: notfriend@gmail.com
 +    subject: "hey there"
 +    query: -to:none@gmail.com
diff --git a/testdata/local.deleteall.diff b/testdata/local.deleteall.diff
index 7428421..43bfd5b 100644
--- a/testdata/local.deleteall.diff
+++ b/testdata/local.deleteall.diff
@@ -3,30 +3,25 @@ Filters:
 +++ TO BE APPLIED
 @@ -1,37 +1 @@
 -* Criteria:
--    to: myalias@gmail.com
--    query: list:{list1 list2 list3}
+-    query: {"buy this thing" "very important!!!"}
 -  Actions:
--    mark as important
+-    delete
  
 -* Criteria:
--    from: {spammer1 spammer2}
+-    query: list:{list1 list2 list3} -to:{pippo@gmail.com pippo@hotmail.com}
 -  Actions:
--    delete
+-    apply label: onemorelabel
 -
 -* Criteria:
--    to: pippo+spammy@gmail.com
+-    from: {spammer1 spammer2}
 -  Actions:
 -    delete
 -
 -* Criteria:
--    query: list:{list1 list2 list3} -to:{pippo@gmail.com pippo@hotmail.com}
--  Actions:
--    apply label: onemorelabel
--
--* Criteria:
--    query: {"buy this thing" "very important!!!"}
+-    to: myalias@gmail.com
+-    query: list:{list1 list2 list3}
 -  Actions:
--    delete
+-    mark as important
 -
 -* Criteria:
 -    query: list:{list1 list2 list3} -to:{pippo@gmail.com pippo@hotmail.com}
@@ -39,3 +34,8 @@ Filters:
 -  Actions:
 -    delete
 -
+-* Criteria:
+-    to: pippo+spammy@gmail.com
+-  Actions:
+-    delete
+-