hashstructure icon indicating copy to clipboard operation
hashstructure copied to clipboard

Cyclic structure support

Open ferhatelmas opened this issue 9 years ago • 2 comments

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.

ferhatelmas avatar Feb 09 '16 12:02 ferhatelmas

Fixed the 0 hash values, what a bad bug. We've added a regression as well.

mitchellh avatar Feb 09 '16 21:02 mitchellh

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.

atombender avatar Apr 18 '18 00:04 atombender