Skip to content

Commit 7c8cdd1

Browse files
committed
Remove the FormatV1/FormatV2 option
This is a new fork so we can start fresh.
1 parent 8c47e87 commit 7c8cdd1

4 files changed

+26
-95
lines changed

README.md

+1-10
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,6 @@ Standard `go get`:
3333
$ go get github.com/gohugoio/hashstructure
3434
```
3535

36-
**Note on v2:** It is highly recommended you use the "v2" release since this
37-
fixes some significant hash collisions issues from v1. In practice, we used
38-
v1 for many years in real projects at HashiCorp and never had issues, but it
39-
is highly dependent on the shape of the data you're hashing and how you use
40-
those hashes.
41-
42-
When using v2+, you can still generate weaker v1 hashes by using the
43-
`FormatV1` format when calling `Hash`.
44-
4536
## Usage & Example
4637

4738
For usage and examples see the [Godoc](http://godoc.org/github.com/mitchellh/hashstructure).
@@ -65,7 +56,7 @@ v := ComplexStruct{
6556
},
6657
}
6758

68-
hash, err := hashstructure.Hash(v, hashstructure.FormatV2, nil)
59+
hash, err := hashstructure.Hash(v, nil)
6960
if err != nil {
7061
panic(err)
7162
}

hashstructure.go

+6-37
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,6 @@ type HashOptions struct {
3939
UseStringer bool
4040
}
4141

42-
// Format specifies the hashing process used. Different formats typically
43-
// generate different hashes for the same value and have different properties.
44-
type Format uint
45-
46-
const (
47-
// To disallow the zero value
48-
formatInvalid Format = iota
49-
50-
// FormatV1 is the format used in v1.x of this library. This has the
51-
// downsides noted in issue #18 but allows simultaneous v1/v2 usage.
52-
FormatV1
53-
54-
// FormatV2 is the current recommended format and fixes the issues
55-
// noted in FormatV1.
56-
FormatV2
57-
58-
formatMax // so we can easily find the end
59-
)
60-
6142
// Hash returns the hash value of an arbitrary value.
6243
//
6344
// If opts is nil, then default options will be used. See HashOptions
@@ -94,12 +75,7 @@ const (
9475
//
9576
// - "string" - The field will be hashed as a string, only works when the
9677
// field implements fmt.Stringer
97-
func Hash(v interface{}, format Format, opts *HashOptions) (uint64, error) {
98-
// Validate our format
99-
if format <= formatInvalid || format >= formatMax {
100-
return 0, &ErrFormat{}
101-
}
102-
78+
func Hash(v interface{}, opts *HashOptions) (uint64, error) {
10379
// Create default options
10480
if opts == nil {
10581
opts = &HashOptions{}
@@ -116,7 +92,6 @@ func Hash(v interface{}, format Format, opts *HashOptions) (uint64, error) {
11692

11793
// Create our walker and walk the structure
11894
w := &walker{
119-
format: format,
12095
h: opts.Hasher,
12196
tag: opts.TagName,
12297
zeronil: opts.ZeroNil,
@@ -128,7 +103,6 @@ func Hash(v interface{}, format Format, opts *HashOptions) (uint64, error) {
128103
}
129104

130105
type walker struct {
131-
format Format
132106
h hash.Hash64
133107
tag string
134108
zeronil bool
@@ -267,10 +241,8 @@ func (w *walker) visit(v reflect.Value, opts *visitOpts) (uint64, error) {
267241
h = hashUpdateUnordered(h, fieldHash)
268242
}
269243

270-
if w.format != FormatV1 {
271-
// Important: read the docs for hashFinishUnordered
272-
h = hashFinishUnordered(w.h, h)
273-
}
244+
// Important: read the docs for hashFinishUnordered
245+
h = hashFinishUnordered(w.h, h)
274246

275247
return h, nil
276248

@@ -373,11 +345,8 @@ func (w *walker) visit(v reflect.Value, opts *visitOpts) (uint64, error) {
373345
fieldHash := hashUpdateOrdered(w.h, kh, vh)
374346
h = hashUpdateUnordered(h, fieldHash)
375347
}
376-
377-
if w.format != FormatV1 {
378-
// Important: read the docs for hashFinishUnordered
379-
h = hashFinishUnordered(w.h, h)
380-
}
348+
// Important: read the docs for hashFinishUnordered
349+
h = hashFinishUnordered(w.h, h)
381350
}
382351

383352
return h, nil
@@ -405,7 +374,7 @@ func (w *walker) visit(v reflect.Value, opts *visitOpts) (uint64, error) {
405374
}
406375
}
407376

408-
if set && w.format != FormatV1 {
377+
if set {
409378
// Important: read the docs for hashFinishUnordered
410379
h = hashFinishUnordered(w.h, h)
411380
}

hashstructure_examples_test.go

+1-28
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func ExampleHash() {
2121
},
2222
}
2323

24-
hash, err := Hash(v, FormatV2, nil)
24+
hash, err := Hash(v, nil)
2525
if err != nil {
2626
panic(err)
2727
}
@@ -30,30 +30,3 @@ func ExampleHash() {
3030
// Output:
3131
// 1839806922502695369
3232
}
33-
34-
func ExampleHash_v1() {
35-
type ComplexStruct struct {
36-
Name string
37-
Age uint
38-
Metadata map[string]interface{}
39-
}
40-
41-
v := ComplexStruct{
42-
Name: "mitchellh",
43-
Age: 64,
44-
Metadata: map[string]interface{}{
45-
"car": true,
46-
"location": "California",
47-
"siblings": []string{"Bob", "John"},
48-
},
49-
}
50-
51-
hash, err := Hash(v, FormatV1, nil)
52-
if err != nil {
53-
panic(err)
54-
}
55-
56-
fmt.Printf("%d", hash)
57-
// Output:
58-
// 6691276962590150517
59-
}

hashstructure_test.go

+18-20
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"time"
88
)
99

10-
var testFormat = FormatV2
11-
1210
func TestHash_identity(t *testing.T) {
1311
cases := []interface{}{
1412
nil,
@@ -42,7 +40,7 @@ func TestHash_identity(t *testing.T) {
4240
// in the runtime in terms of ordering.
4341
valuelist := make([]uint64, 100)
4442
for i := range valuelist {
45-
v, err := Hash(tc, testFormat, nil)
43+
v, err := Hash(tc, nil)
4644
if err != nil {
4745
t.Fatalf("Error: %s\n\n%#v", err, tc)
4846
}
@@ -171,13 +169,13 @@ func TestHash_equal(t *testing.T) {
171169
for i, tc := range cases {
172170
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
173171
t.Logf("Hashing: %#v", tc.One)
174-
one, err := Hash(tc.One, testFormat, nil)
172+
one, err := Hash(tc.One, nil)
175173
t.Logf("Result: %d", one)
176174
if err != nil {
177175
t.Fatalf("Failed to hash %#v: %s", tc.One, err)
178176
}
179177
t.Logf("Hashing: %#v", tc.Two)
180-
two, err := Hash(tc.Two, testFormat, nil)
178+
two, err := Hash(tc.Two, nil)
181179
t.Logf("Result: %d", two)
182180
if err != nil {
183181
t.Fatalf("Failed to hash %#v: %s", tc.Two, err)
@@ -271,11 +269,11 @@ func TestHash_equalIgnore(t *testing.T) {
271269
}
272270

273271
for _, tc := range cases {
274-
one, err := Hash(tc.One, testFormat, nil)
272+
one, err := Hash(tc.One, nil)
275273
if err != nil {
276274
t.Fatalf("Failed to hash %#v: %s", tc.One, err)
277275
}
278-
two, err := Hash(tc.Two, testFormat, nil)
276+
two, err := Hash(tc.Two, nil)
279277
if err != nil {
280278
t.Fatalf("Failed to hash %#v: %s", tc.Two, err)
281279
}
@@ -327,7 +325,7 @@ func TestHash_stringTagError(t *testing.T) {
327325
}
328326

329327
for _, tc := range cases {
330-
_, err := Hash(tc.Test, testFormat, nil)
328+
_, err := Hash(tc.Test, nil)
331329
if err != nil {
332330
if ens, ok := err.(*ErrNotStringer); ok {
333331
if ens.Field != tc.Field {
@@ -400,11 +398,11 @@ func TestHash_equalNil(t *testing.T) {
400398
}
401399

402400
for _, tc := range cases {
403-
one, err := Hash(tc.One, testFormat, &HashOptions{ZeroNil: tc.ZeroNil})
401+
one, err := Hash(tc.One, &HashOptions{ZeroNil: tc.ZeroNil})
404402
if err != nil {
405403
t.Fatalf("Failed to hash %#v: %s", tc.One, err)
406404
}
407-
two, err := Hash(tc.Two, testFormat, &HashOptions{ZeroNil: tc.ZeroNil})
405+
two, err := Hash(tc.Two, &HashOptions{ZeroNil: tc.ZeroNil})
408406
if err != nil {
409407
t.Fatalf("Failed to hash %#v: %s", tc.Two, err)
410408
}
@@ -445,11 +443,11 @@ func TestHash_equalSet(t *testing.T) {
445443
}
446444

447445
for _, tc := range cases {
448-
one, err := Hash(tc.One, testFormat, nil)
446+
one, err := Hash(tc.One, nil)
449447
if err != nil {
450448
t.Fatalf("Failed to hash %#v: %s", tc.One, err)
451449
}
452-
two, err := Hash(tc.Two, testFormat, nil)
450+
two, err := Hash(tc.Two, nil)
453451
if err != nil {
454452
t.Fatalf("Failed to hash %#v: %s", tc.Two, err)
455453
}
@@ -491,11 +489,11 @@ func TestHash_includable(t *testing.T) {
491489
}
492490

493491
for _, tc := range cases {
494-
one, err := Hash(tc.One, testFormat, nil)
492+
one, err := Hash(tc.One, nil)
495493
if err != nil {
496494
t.Fatalf("Failed to hash %#v: %s", tc.One, err)
497495
}
498-
two, err := Hash(tc.Two, testFormat, nil)
496+
two, err := Hash(tc.Two, nil)
499497
if err != nil {
500498
t.Fatalf("Failed to hash %#v: %s", tc.Two, err)
501499
}
@@ -542,11 +540,11 @@ func TestHash_ignoreZeroValue(t *testing.T) {
542540
}
543541

544542
for _, tc := range cases {
545-
hashA, err := Hash(structA, testFormat, &HashOptions{IgnoreZeroValue: tc.IgnoreZeroValue})
543+
hashA, err := Hash(structA, &HashOptions{IgnoreZeroValue: tc.IgnoreZeroValue})
546544
if err != nil {
547545
t.Fatalf("Failed to hash %#v: %s", structA, err)
548546
}
549-
hashB, err := Hash(structB, testFormat, &HashOptions{IgnoreZeroValue: tc.IgnoreZeroValue})
547+
hashB, err := Hash(structB, &HashOptions{IgnoreZeroValue: tc.IgnoreZeroValue})
550548
if err != nil {
551549
t.Fatalf("Failed to hash %#v: %s", structB, err)
552550
}
@@ -581,11 +579,11 @@ func TestHash_includableMap(t *testing.T) {
581579
}
582580

583581
for _, tc := range cases {
584-
one, err := Hash(tc.One, testFormat, nil)
582+
one, err := Hash(tc.One, nil)
585583
if err != nil {
586584
t.Fatalf("Failed to hash %#v: %s", tc.One, err)
587585
}
588-
two, err := Hash(tc.Two, testFormat, nil)
586+
two, err := Hash(tc.Two, nil)
589587
if err != nil {
590588
t.Fatalf("Failed to hash %#v: %s", tc.Two, err)
591589
}
@@ -643,7 +641,7 @@ func TestHash_hashable(t *testing.T) {
643641

644642
for i, tc := range cases {
645643
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
646-
one, err := Hash(tc.One, testFormat, nil)
644+
one, err := Hash(tc.One, nil)
647645
if tc.Err != "" {
648646
if err == nil {
649647
t.Fatal("expected error")
@@ -659,7 +657,7 @@ func TestHash_hashable(t *testing.T) {
659657
t.Fatalf("Failed to hash %#v: %s", tc.One, err)
660658
}
661659

662-
two, err := Hash(tc.Two, testFormat, nil)
660+
two, err := Hash(tc.Two, nil)
663661
if err != nil {
664662
t.Fatalf("Failed to hash %#v: %s", tc.Two, err)
665663
}

0 commit comments

Comments
 (0)