-
Notifications
You must be signed in to change notification settings - Fork 10
/
compute_tags_hash.go
38 lines (34 loc) · 1 KB
/
compute_tags_hash.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package utils
import (
"hash/fnv"
"sort"
"strconv"
)
// ComputeTagsHash returns a hash of the supplied tags.
func ComputeTagsHash(tags []string) string {
hash := ""
if len(tags) > 0 {
// do not sort original slice
tags = copyArray(tags)
h := fnv.New64()
sort.Strings(tags)
for _, i := range tags {
h.Write([]byte(i)) //nolint:errcheck
}
hash = strconv.FormatUint(h.Sum64(), 16)
}
return hash
}
// copyArray makes sure the tagger does not return internal slices
// that could be modified by others, by explicitly copying the slice
// contents to a new slice. As strings are references, the size of
// the new array is small enough.
func copyArray(source []string) []string {
copied := make([]string, len(source))
copy(copied, source)
return copied
}