forked from fortio/fortio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecksum.go
31 lines (26 loc) · 844 Bytes
/
checksum.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
// Copyright 2015 Michal Witkowski. All Rights Reserved.
// See LICENSE for licensing terms.
package dflag
import (
"flag"
"hash/fnv"
"sync"
)
// @todo Temporary fix for race condition happening in the test:
// spf13/pflag.FlagSet.VisitAll seems to be prone to a race condition
// This fixes that but I'm not sure how much slower does it make the codebase.
var visitAllMutex = &sync.Mutex{}
// ChecksumFlagSet will generate a FNV of the *set* values in a FlagSet.
func ChecksumFlagSet(flagSet *flag.FlagSet, flagFilter func(flag *flag.Flag) bool) []byte {
h := fnv.New32a()
visitAllMutex.Lock()
defer visitAllMutex.Unlock()
flagSet.VisitAll(func(flag *flag.Flag) {
if flagFilter != nil && !flagFilter(flag) {
return
}
_, _ = h.Write([]byte(flag.Name))
_, _ = h.Write([]byte(flag.Value.String()))
})
return h.Sum(nil)
}