Skip to content

Commit 8c7c5ca

Browse files
authored
refactor: drop hashstructure dependency (#223)
* refactor: drop hashstructure dependency hashstructure was archived and it is no longer maintained we are only using the library to hash a slice of strings, implement the logic in a custom method and drop the library * lint: update notice * test: verify custom hash function is equivalent to hashstructure lib
1 parent 00c95bb commit 8c7c5ca

File tree

5 files changed

+36
-37
lines changed

5 files changed

+36
-37
lines changed

NOTICE.txt

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -795,37 +795,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
795795
SOFTWARE.
796796

797797

798-
--------------------------------------------------------------------------------
799-
Dependency : github.com/mitchellh/hashstructure
800-
Version: v1.1.0
801-
Licence type (autodetected): MIT
802-
--------------------------------------------------------------------------------
803-
804-
Contents of probable licence file $GOMODCACHE/github.com/mitchellh/[email protected]/LICENSE:
805-
806-
The MIT License (MIT)
807-
808-
Copyright (c) 2016 Mitchell Hashimoto
809-
810-
Permission is hereby granted, free of charge, to any person obtaining a copy
811-
of this software and associated documentation files (the "Software"), to deal
812-
in the Software without restriction, including without limitation the rights
813-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
814-
copies of the Software, and to permit persons to whom the Software is
815-
furnished to do so, subject to the following conditions:
816-
817-
The above copyright notice and this permission notice shall be included in
818-
all copies or substantial portions of the Software.
819-
820-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
821-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
822-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
823-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
824-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
825-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
826-
THE SOFTWARE.
827-
828-
829798
--------------------------------------------------------------------------------
830799
Dependency : github.com/rcrowley/go-metrics
831800
Version: v0.0.0-20201227073835-cf1acfcdf475

filewatcher/filewatcher.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
package filewatcher
1919

2020
import (
21+
"encoding/binary"
22+
"hash/fnv"
2123
"os"
2224
"time"
2325

24-
"github.com/mitchellh/hashstructure"
25-
2626
"github.com/elastic/elastic-agent-libs/logp"
2727
)
2828

@@ -77,7 +77,7 @@ func (f *FileWatcher) Scan() ([]string, bool, error) {
7777
files = append(files, path)
7878
}
7979

80-
hash, err := hashstructure.Hash(files, nil)
80+
hash, err := hash(files)
8181
if err != nil {
8282
return files, true, err
8383
}
@@ -90,3 +90,28 @@ func (f *FileWatcher) Scan() ([]string, bool, error) {
9090

9191
return files, true, nil
9292
}
93+
94+
func hash(files []string) (uint64, error) {
95+
var u uint64
96+
97+
for _, f := range files {
98+
current := hashString(f)
99+
100+
h := fnv.New64()
101+
if err := binary.Write(h, binary.LittleEndian, u); err != nil {
102+
return 0, err
103+
}
104+
if err := binary.Write(h, binary.LittleEndian, current); err != nil {
105+
return 0, err
106+
}
107+
u = h.Sum64()
108+
}
109+
110+
return u, nil
111+
}
112+
113+
func hashString(s string) uint64 {
114+
h := fnv.New64()
115+
h.Write([]byte(s))
116+
return h.Sum64()
117+
}

filewatcher/filewatcher_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,11 @@ func TestFileWatcher(t *testing.T) {
7878
assert.NoError(t, err)
7979
assert.True(t, changed, "'changed' must be true, one file has been removed")
8080
}
81+
82+
func TestHash(t *testing.T) {
83+
files := []string{"file-1", "file-2", "file-3"}
84+
i, err := hash(files)
85+
assert.NoError(t, err)
86+
// ensure custom hash function returns the same result as deprecated hashstructure lib
87+
assert.Equal(t, uint64(11400963159482616226), i)
88+
}

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ require (
1111
github.com/gofrs/uuid/v5 v5.2.0
1212
github.com/magefile/mage v1.13.0
1313
github.com/mattn/go-colorable v0.1.12
14-
github.com/mitchellh/hashstructure v1.1.0
1514
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475
1615
github.com/spf13/cobra v1.7.0
1716
github.com/stretchr/testify v1.9.0

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb
5151
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
5252
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
5353
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
54-
github.com/mitchellh/hashstructure v1.1.0 h1:P6P1hdjqAAknpY/M1CGipelZgp+4y9ja9kmUZPXP+H0=
55-
github.com/mitchellh/hashstructure v1.1.0/go.mod h1:xUDAozZz0Wmdiufv0uyhnHkUTN6/6d8ulp4AwfLKrmA=
5654
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
5755
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
5856
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

0 commit comments

Comments
 (0)