From e6c211564ec8e112f1abeea42435dd8425887ea9 Mon Sep 17 00:00:00 2001 From: tdakkota Date: Mon, 30 May 2022 15:55:09 +0300 Subject: [PATCH] feat: add Into and Must generic helpers --- example_Into_test.go | 20 ++++++++++++++++++++ example_Must_test.go | 17 +++++++++++++++++ into.go | 9 +++++++++ must.go | 10 ++++++++++ must_test.go | 24 ++++++++++++++++++++++++ 5 files changed, 80 insertions(+) create mode 100644 example_Into_test.go create mode 100644 example_Must_test.go create mode 100644 into.go create mode 100644 must.go create mode 100644 must_test.go diff --git a/example_Into_test.go b/example_Into_test.go new file mode 100644 index 0000000..e70ed60 --- /dev/null +++ b/example_Into_test.go @@ -0,0 +1,20 @@ +package errors_test + +import ( + "fmt" + "os" + + "github.com/go-faster/errors" +) + +func ExampleInto() { + _, err := os.Open("non-existing") + if err != nil { + if pathError, ok := errors.Into[*os.PathError](err); ok { + fmt.Println("Failed at path:", pathError.Path) + } + } + + // Output: + // Failed at path: non-existing +} diff --git a/example_Must_test.go b/example_Must_test.go new file mode 100644 index 0000000..c0326b4 --- /dev/null +++ b/example_Must_test.go @@ -0,0 +1,17 @@ +package errors_test + +import ( + "fmt" + "regexp" + + "github.com/go-faster/errors" +) + +func ExampleMust() { + // Same as regexp.MustCompile, but generic. + r := errors.Must(regexp.Compile(`\d+`)) + fmt.Println(r.String()) + + // Output: + // \d+ +} diff --git a/into.go b/into.go new file mode 100644 index 0000000..7a867d6 --- /dev/null +++ b/into.go @@ -0,0 +1,9 @@ +package errors + +// Into finds the first error in err's chain that matches target type T, and if so, returns it. +// +// Into is type-safe alternative to As. +func Into[T error](err error) (val T, ok bool) { + ok = As(err, &val) + return val, ok +} diff --git a/must.go b/must.go new file mode 100644 index 0000000..e9c83c3 --- /dev/null +++ b/must.go @@ -0,0 +1,10 @@ +package errors + +// Must is a generic helper, like template.Must, that wraps a call to a function returning (T, error) +// and panics if the error is non-nil. +func Must[T any](val T, err error) T { + if err != nil { + panic(err) + } + return val +} diff --git a/must_test.go b/must_test.go new file mode 100644 index 0000000..b3a579b --- /dev/null +++ b/must_test.go @@ -0,0 +1,24 @@ +package errors + +import ( + "testing" +) + +func TestMust(t *testing.T) { + if got := Must(10, nil); got != 10 { + t.Fatalf("Expected %+v, got %+v", 10, got) + } + + panics := func() (r bool) { + defer func() { + if recover() != nil { + r = true + } + }() + Must(10, New("test error")) + return r + }() + if !panics { + t.Fatal("Panic expected") + } +}