diff --git a/_rsrch/nodesolution/impls/bench_test.go b/_rsrch/nodesolution/impls/bench_test.go index 305430a7e3d2f1da65586151f1421f4eb2a402ba..9bd9457af37e1343145782082053b5485ceed0a2 100644 --- a/_rsrch/nodesolution/impls/bench_test.go +++ b/_rsrch/nodesolution/impls/bench_test.go @@ -5,6 +5,7 @@ import ( "testing" ipld "github.com/ipld/go-ipld-prime/_rsrch/nodesolution" + "github.com/ipld/go-ipld-prime/must" ) var sink interface{} @@ -22,9 +23,7 @@ func BenchmarkMap3nBaselineNativeMapAssignSimpleKeys(b *testing.B) { func BenchmarkMap3nBaselineJsonUnmarshalMapSimpleKeys(b *testing.B) { for i := 0; i < b.N; i++ { var x = make(map[string]int, 3) - if err := json.Unmarshal([]byte(`{"whee":1,"woot":2,"waga":3}`), &x); err != nil { - panic(err) - } + must.NotError(json.Unmarshal([]byte(`{"whee":1,"woot":2,"waga":3}`), &x)) sink = x } } @@ -37,27 +36,13 @@ func BenchmarkMap3nFeedGenericMapSimpleKeys(b *testing.B) { if err != nil { panic(err) } - if err := ma.AssembleKey().AssignString("whee"); err != nil { - panic(err) - } - if err := ma.AssembleValue().AssignInt(1); err != nil { - panic(err) - } - if err := ma.AssembleKey().AssignString("woot"); err != nil { - panic(err) - } - if err := ma.AssembleValue().AssignInt(2); err != nil { - panic(err) - } - if err := ma.AssembleKey().AssignString("waga"); err != nil { - panic(err) - } - if err := ma.AssembleValue().AssignInt(3); err != nil { - panic(err) - } - if err := ma.Done(); err != nil { - panic(err) - } + must.NotError(ma.AssembleKey().AssignString("whee")) + must.NotError(ma.AssembleValue().AssignInt(1)) + must.NotError(ma.AssembleKey().AssignString("woot")) + must.NotError(ma.AssembleValue().AssignInt(2)) + must.NotError(ma.AssembleKey().AssignString("waga")) + must.NotError(ma.AssembleValue().AssignInt(3)) + must.NotError(ma.Done()) if n, err := nb.Build(); err != nil { panic(err) } else { @@ -74,27 +59,13 @@ func BenchmarkMap3nFeedGennedMapSimpleKeys(b *testing.B) { if err != nil { panic(err) } - if err := ma.AssembleKey().AssignString("whee"); err != nil { - panic(err) - } - if err := ma.AssembleValue().AssignInt(1); err != nil { - panic(err) - } - if err := ma.AssembleKey().AssignString("woot"); err != nil { - panic(err) - } - if err := ma.AssembleValue().AssignInt(2); err != nil { - panic(err) - } - if err := ma.AssembleKey().AssignString("waga"); err != nil { - panic(err) - } - if err := ma.AssembleValue().AssignInt(3); err != nil { - panic(err) - } - if err := ma.Done(); err != nil { - panic(err) - } + must.NotError(ma.AssembleKey().AssignString("whee")) + must.NotError(ma.AssembleValue().AssignInt(1)) + must.NotError(ma.AssembleKey().AssignString("woot")) + must.NotError(ma.AssembleValue().AssignInt(2)) + must.NotError(ma.AssembleKey().AssignString("waga")) + must.NotError(ma.AssembleValue().AssignInt(3)) + must.NotError(ma.Done()) if n, err := nb.Build(); err != nil { panic(err) } else { @@ -114,27 +85,19 @@ func BenchmarkMap3nFeedGennedMapSimpleKeysDirectly(b *testing.B) { if va, err := ma.AssembleDirectly("whee"); err != nil { panic(err) } else { - if err := va.AssignInt(1); err != nil { - panic(err) - } + must.NotError(va.AssignInt(1)) } if va, err := ma.AssembleDirectly("woot"); err != nil { panic(err) } else { - if err := va.AssignInt(2); err != nil { - panic(err) - } + must.NotError(va.AssignInt(2)) } if va, err := ma.AssembleDirectly("waga"); err != nil { panic(err) } else { - if err := va.AssignInt(3); err != nil { - panic(err) - } - } - if err := ma.Done(); err != nil { - panic(err) + must.NotError(va.AssignInt(3)) } + must.NotError(ma.Done()) if n, err := nb.Build(); err != nil { panic(err) } else { diff --git a/_rsrch/nodesolution/impls/map_test.go b/_rsrch/nodesolution/impls/map_test.go index 577194db62683ee588fc5cdd5b33574fed5f0055..4189aab8d854bfd0de784df685d02a2943b90cc6 100644 --- a/_rsrch/nodesolution/impls/map_test.go +++ b/_rsrch/nodesolution/impls/map_test.go @@ -3,9 +3,10 @@ package impls import ( "testing" - ipld "github.com/ipld/go-ipld-prime/_rsrch/nodesolution" - wish "github.com/warpfork/go-wish" + + ipld "github.com/ipld/go-ipld-prime/_rsrch/nodesolution" + "github.com/ipld/go-ipld-prime/must" ) func TestGennedMapIntValues(t *testing.T) { @@ -19,30 +20,14 @@ func CheckMaps(t *testing.T, ns ipld.NodeStyle) { t.Run("map node, str:int, 3 entries", func(t *testing.T) { nb := ns.NewBuilder() ma, err := nb.BeginMap(3) - if err != nil { - panic(err) - } - if err := ma.AssembleKey().AssignString("whee"); err != nil { - panic(err) - } - if err := ma.AssembleValue().AssignInt(1); err != nil { - panic(err) - } - if err := ma.AssembleKey().AssignString("woot"); err != nil { - panic(err) - } - if err := ma.AssembleValue().AssignInt(2); err != nil { - panic(err) - } - if err := ma.AssembleKey().AssignString("waga"); err != nil { - panic(err) - } - if err := ma.AssembleValue().AssignInt(3); err != nil { - panic(err) - } - if err := ma.Done(); err != nil { - panic(err) - } + must.NotError(err) + must.NotError(ma.AssembleKey().AssignString("whee")) + must.NotError(ma.AssembleValue().AssignInt(1)) + must.NotError(ma.AssembleKey().AssignString("woot")) + must.NotError(ma.AssembleValue().AssignInt(2)) + must.NotError(ma.AssembleKey().AssignString("waga")) + must.NotError(ma.AssembleValue().AssignInt(3)) + must.NotError(ma.Done()) n, err := nb.Build() if err != nil { wish.Require(t, err, wish.ShouldEqual, nil) diff --git a/must/must.go b/must/must.go index 134c5436bbe18a676eb122fa65539186bbeff46a..c15a0b66fa1d874dff1ab04942ff8e2be5ea8495 100644 --- a/must/must.go +++ b/must/must.go @@ -21,6 +21,15 @@ import ( "github.com/ipld/go-ipld-prime/impl/typed" ) +// must.NotError simply panics if given an error. +// It helps turn multi-line code into one-liner code in situations where +// you simply don't care. +func NotError(e error) { + if e != nil { + panic(e) + } +} + // must.Node helps write pointfree/chainable-style code // by taking a Node and an error and transforming any error into a panic. //