hashstructure
hashstructure copied to clipboard
Cyclic structure support
package main
import (
"fmt"
"github.com/mitchellh/hashstructure"
)
func hash(i interface{}) {
hash, err := hashstructure.Hash(i, nil)
if err != nil {
panic(err)
}
fmt.Printf("%v: %d\n", i, hash)
}
func main() {
m := map[string]interface{}{}
m["1"] = "1"
// prints 0
hash(m)
m["2"] = "2"
// prints 0
hash(m)
m["3"] = 3
// does not print 0
hash(m)
m["m"] = m
// fatal error: stack overflow
hash(m)
}
Cyclic structures aren't supported. Is it expected and/or any plan to add support ?
Also note that in the first two cases, hash is same even if map is updated.
Fixed the 0 hash values, what a bad bug. We've added a regression as well.
I would also like this. Since cycle detection requires maintaining state, which comes with allocation overhead, I propose that it's implemented as an option that defaults to false.