Skip to content

Commit 3f507d7

Browse files
committed
Ensure generated code does not yield a 0 for hash values
1 parent ae605c3 commit 3f507d7

File tree

5 files changed

+86
-46
lines changed

5 files changed

+86
-46
lines changed

examples/structs/gen_struct.go

+22-12
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,22 @@ func done(data interface{}) pubsub.Paths {
1616
}
1717

1818
func hashBool(data bool) uint64 {
19+
// 0 is reserved
1920
if data {
2021
return 2
2122
}
2223
return 1
2324
}
2425

26+
func hashUint64(data uint64) uint64 {
27+
// 0 is reserved
28+
if data == 0 {
29+
return 1
30+
}
31+
32+
return data
33+
}
34+
2535
var tableECMA = crc64.MakeTable(crc64.ECMA)
2636

2737
func _a(data interface{}) pubsub.Paths {
@@ -32,7 +42,7 @@ func _a(data interface{}) pubsub.Paths {
3242
return 0, pubsub.TreeTraverser(_b), true
3343
case 1:
3444

35-
return crc64.Checksum([]byte(data.(*someType).a), tableECMA) + 1, pubsub.TreeTraverser(_b), true
45+
return hashUint64(crc64.Checksum([]byte(data.(*someType).a), tableECMA)), pubsub.TreeTraverser(_b), true
3646
default:
3747
return 0, nil, false
3848
}
@@ -50,7 +60,7 @@ func _b(data interface{}) pubsub.Paths {
5060
}), true
5161
case 1:
5262

53-
return crc64.Checksum([]byte(data.(*someType).b), tableECMA) + 1,
63+
return hashUint64(crc64.Checksum([]byte(data.(*someType).b), tableECMA)),
5464
pubsub.TreeTraverser(func(data interface{}) pubsub.Paths {
5565
return ___w_x
5666
}), true
@@ -115,7 +125,7 @@ func _w_i(data interface{}) pubsub.Paths {
115125
return 0, pubsub.TreeTraverser(_w_j), true
116126
case 1:
117127

118-
return crc64.Checksum([]byte(data.(*someType).w.i), tableECMA) + 1, pubsub.TreeTraverser(_w_j), true
128+
return hashUint64(crc64.Checksum([]byte(data.(*someType).w.i), tableECMA)), pubsub.TreeTraverser(_w_j), true
119129
default:
120130
return 0, nil, false
121131
}
@@ -130,7 +140,7 @@ func _w_j(data interface{}) pubsub.Paths {
130140
return 0, pubsub.TreeTraverser(done), true
131141
case 1:
132142

133-
return crc64.Checksum([]byte(data.(*someType).w.j), tableECMA) + 1, pubsub.TreeTraverser(done), true
143+
return hashUint64(crc64.Checksum([]byte(data.(*someType).w.j), tableECMA)), pubsub.TreeTraverser(done), true
134144
default:
135145
return 0, nil, false
136146
}
@@ -168,7 +178,7 @@ func _x_i(data interface{}) pubsub.Paths {
168178
return 0, pubsub.TreeTraverser(_x_j), true
169179
case 1:
170180

171-
return crc64.Checksum([]byte(data.(*someType).x.i), tableECMA) + 1, pubsub.TreeTraverser(_x_j), true
181+
return hashUint64(crc64.Checksum([]byte(data.(*someType).x.i), tableECMA)), pubsub.TreeTraverser(_x_j), true
172182
default:
173183
return 0, nil, false
174184
}
@@ -183,7 +193,7 @@ func _x_j(data interface{}) pubsub.Paths {
183193
return 0, pubsub.TreeTraverser(done), true
184194
case 1:
185195

186-
return crc64.Checksum([]byte(data.(*someType).x.j), tableECMA) + 1, pubsub.TreeTraverser(done), true
196+
return hashUint64(crc64.Checksum([]byte(data.(*someType).x.j), tableECMA)), pubsub.TreeTraverser(done), true
187197
default:
188198
return 0, nil, false
189199
}
@@ -228,14 +238,14 @@ func StructTravCreatePath(f *someTypeFilter) []uint64 {
228238

229239
if f.a != nil {
230240

231-
path = append(path, crc64.Checksum([]byte(*f.a), tableECMA)+1)
241+
path = append(path, hashUint64(crc64.Checksum([]byte(*f.a), tableECMA)))
232242
} else {
233243
path = append(path, 0)
234244
}
235245

236246
if f.b != nil {
237247

238-
path = append(path, crc64.Checksum([]byte(*f.b), tableECMA)+1)
248+
path = append(path, hashUint64(crc64.Checksum([]byte(*f.b), tableECMA)))
239249
} else {
240250
path = append(path, 0)
241251
}
@@ -269,14 +279,14 @@ func createPath__w(f *wFilter) []uint64 {
269279

270280
if f.i != nil {
271281

272-
path = append(path, crc64.Checksum([]byte(*f.i), tableECMA)+1)
282+
path = append(path, hashUint64(crc64.Checksum([]byte(*f.i), tableECMA)))
273283
} else {
274284
path = append(path, 0)
275285
}
276286

277287
if f.j != nil {
278288

279-
path = append(path, crc64.Checksum([]byte(*f.j), tableECMA)+1)
289+
path = append(path, hashUint64(crc64.Checksum([]byte(*f.j), tableECMA)))
280290
} else {
281291
path = append(path, 0)
282292
}
@@ -299,14 +309,14 @@ func createPath__x(f *xFilter) []uint64 {
299309

300310
if f.i != nil {
301311

302-
path = append(path, crc64.Checksum([]byte(*f.i), tableECMA)+1)
312+
path = append(path, hashUint64(crc64.Checksum([]byte(*f.i), tableECMA)))
303313
} else {
304314
path = append(path, 0)
305315
}
306316

307317
if f.j != nil {
308318

309-
path = append(path, crc64.Checksum([]byte(*f.j), tableECMA)+1)
319+
path = append(path, hashUint64(crc64.Checksum([]byte(*f.j), tableECMA)))
310320
} else {
311321
path = append(path, 0)
312322
}

gen_struct_test.go

+18-8
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,22 @@ func done(data interface{}) pubsub.Paths {
1616
}
1717

1818
func hashBool(data bool) uint64 {
19+
// 0 is reserved
1920
if data {
2021
return 2
2122
}
2223
return 1
2324
}
2425

26+
func hashUint64(data uint64) uint64 {
27+
// 0 is reserved
28+
if data == 0 {
29+
return 1
30+
}
31+
32+
return data
33+
}
34+
2535
var tableECMA = crc64.MakeTable(crc64.ECMA)
2636

2737
func _a(data interface{}) pubsub.Paths {
@@ -32,7 +42,7 @@ func _a(data interface{}) pubsub.Paths {
3242
return 0, pubsub.TreeTraverser(_b), true
3343
case 1:
3444

35-
return uint64(data.(*testStruct).a) + 1, pubsub.TreeTraverser(_b), true
45+
return hashUint64(uint64(data.(*testStruct).a)), pubsub.TreeTraverser(_b), true
3646
default:
3747
return 0, nil, false
3848
}
@@ -50,7 +60,7 @@ func _b(data interface{}) pubsub.Paths {
5060
}), true
5161
case 1:
5262

53-
return uint64(data.(*testStruct).b) + 1,
63+
return hashUint64(uint64(data.(*testStruct).b)),
5464
pubsub.TreeTraverser(func(data interface{}) pubsub.Paths {
5565
return ___aa_bb
5666
}), true
@@ -115,7 +125,7 @@ func _aa_a(data interface{}) pubsub.Paths {
115125
return 0, pubsub.TreeTraverser(done), true
116126
case 1:
117127

118-
return uint64(data.(*testStruct).aa.a) + 1, pubsub.TreeTraverser(done), true
128+
return hashUint64(uint64(data.(*testStruct).aa.a)), pubsub.TreeTraverser(done), true
119129
default:
120130
return 0, nil, false
121131
}
@@ -153,7 +163,7 @@ func _bb_b(data interface{}) pubsub.Paths {
153163
return 0, pubsub.TreeTraverser(done), true
154164
case 1:
155165

156-
return uint64(data.(*testStruct).bb.b) + 1, pubsub.TreeTraverser(done), true
166+
return hashUint64(uint64(data.(*testStruct).bb.b)), pubsub.TreeTraverser(done), true
157167
default:
158168
return 0, nil, false
159169
}
@@ -196,14 +206,14 @@ func testStructTravCreatePath(f *testStructFilter) []uint64 {
196206

197207
if f.a != nil {
198208

199-
path = append(path, uint64(*f.a)+1)
209+
path = append(path, hashUint64(uint64(*f.a)))
200210
} else {
201211
path = append(path, 0)
202212
}
203213

204214
if f.b != nil {
205215

206-
path = append(path, uint64(*f.b)+1)
216+
path = append(path, hashUint64(uint64(*f.b)))
207217
} else {
208218
path = append(path, 0)
209219
}
@@ -237,7 +247,7 @@ func createPath__aa(f *testStructAFilter) []uint64 {
237247

238248
if f.a != nil {
239249

240-
path = append(path, uint64(*f.a)+1)
250+
path = append(path, hashUint64(uint64(*f.a)))
241251
} else {
242252
path = append(path, 0)
243253
}
@@ -260,7 +270,7 @@ func createPath__bb(f *testStructBFilter) []uint64 {
260270

261271
if f.b != nil {
262272

263-
path = append(path, uint64(*f.b)+1)
273+
path = append(path, hashUint64(uint64(*f.b)))
264274
} else {
265275
path = append(path, 0)
266276
}

0 commit comments

Comments
 (0)