hashstructure icon indicating copy to clipboard operation
hashstructure copied to clipboard

ZeroNil has no effect on int pointers

Open andersarpi opened this issue 4 months ago • 0 comments

I just ran into this issue, took some time to figure out.

type t struct {
    A *int
}
var zero int

fmt.Println(hashstructure.Hash(t{}, hashstructure.FormatV2, nil))
fmt.Println(hashstructure.Hash(t{A: &zero}, hashstructure.FormatV2, nil))

opts := &hashstructure.HashOptions{ZeroNil: true}
fmt.Println(hashstructure.Hash(t{}, hashstructure.FormatV2, opts))
fmt.Println(hashstructure.Hash(t{A: &zero}, hashstructure.FormatV2, opts))

opts.ZeroNil = false
fmt.Println(hashstructure.Hash(t{}, hashstructure.FormatV2, opts))
fmt.Println(hashstructure.Hash(t{A: &zero}, hashstructure.FormatV2, opts))

The output from the code above is as follows, showing that the ZeroNil option has no effect.

16677325619216437773 <nil>
16677325619216437773 <nil>
16677325619216437773 <nil>
16677325619216437773 <nil>
16677325619216437773 <nil>
16677325619216437773 <nil>

Note that for other types I tried (string, bool) the setting did seem to work correctly.

andersarpi avatar Feb 12 '24 10:02 andersarpi