From 2878d389c87ed5345004eeb446b6fbbfbd2b89ac Mon Sep 17 00:00:00 2001 From: donutloop Date: Sat, 22 Jan 2022 14:25:31 +0100 Subject: [PATCH] error: Add unwrap (#24) Add a unwrap func helper * Unwrap if err is nil then it returns a valid value otherwise it panics --- README.md | 26 ++++++++++++++++++++++++++ README_zh-CN.md | 26 ++++++++++++++++++++++++++ xerror/xerror.go | 14 ++++++++++++++ xerror/xerror_test.go | 24 ++++++++++++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 xerror/xerror.go create mode 100644 xerror/xerror_test.go diff --git a/README.md b/README.md index 2ad243f..16fb872 100644 --- a/README.md +++ b/README.md @@ -588,3 +588,29 @@ func IsIpV6(ipstr string) bool //check if the string is a ipv6 address func IsStrongPassword(password string, length int) bool //check if the string is strong password (alpha(lower+upper) + number + special chars(!@#$%^&*()?><)) func IsWeakPassword(password string) bool //check if the string is weak password(only letter or only number or letter + number) ``` + +### 14. error helpers + +- Contain functions to handle errors +- Usage: import "github.com/duke-git/lancet/xerror" + +```go +package main + +import ( + "fmt" + "io/ioutil" + "log" + "github.com/duke-git/lancet/errors" +) + +func main() { + x := Unwrap(strconv.Atoi("42")) // Unwrap if err is nil then it returns a valid value otherwise it panics +} +``` + +- Function list: + +```go +Unwrap[T any](val T, err error) +``` \ No newline at end of file diff --git a/README_zh-CN.md b/README_zh-CN.md index f703d34..fd790fc 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -589,3 +589,29 @@ func IsIpV6(ipstr string) bool //判断字符串是否是ipv6 func IsStrongPassword(password string, length int) bool //判断字符串是否是强密码(大小写字母+数字+特殊字符) func IsWeakPassword(password string) bool //判断字符串是否是弱密码(只有字母或数字) ``` + +### 14. error helpers + +- Contain functions to handle errors +- Usage: import "github.com/duke-git/lancet/xerror" + +```go +package main + +import ( + "fmt" + "io/ioutil" + "log" + "github.com/duke-git/lancet/errors" +) + +func main() { + x := Unwrap(strconv.Atoi("42")) +} +``` + +- Function list: + +```go +Unwrap[T any](val T, err error) +``` \ No newline at end of file diff --git a/xerror/xerror.go b/xerror/xerror.go new file mode 100644 index 0000000..f98914a --- /dev/null +++ b/xerror/xerror.go @@ -0,0 +1,14 @@ +// Copyright 2021 dudaodong@gmail.com. All rights reserved. +// Use of this source code is governed by MIT license + +// Package xerror implements helpers for errors +package xerror + +// Unwrap if err is nil then it returns a valid value +// If err is not nil, Unwrap panics with err. +func Unwrap[T any](val T, err error) T { + if err != nil { + panic(err) + } + return val +} diff --git a/xerror/xerror_test.go b/xerror/xerror_test.go new file mode 100644 index 0000000..12fff85 --- /dev/null +++ b/xerror/xerror_test.go @@ -0,0 +1,24 @@ +package xerror + +import ( + "github.com/duke-git/lancet/internal" + "strconv" + "testing" +) + +func TestUnwrap(t *testing.T) { + assert := internal.NewAssert(t, "TestUnwrap") + assert.Equal(42, Unwrap(strconv.Atoi("42"))) +} + +func TestUnwrapFail(t *testing.T) { + assert := internal.NewAssert(t, "TestUnwrapFail") + + _, err := strconv.Atoi("4o2") + defer func() { + v := recover() + assert.Equal(err.Error(), v.(*strconv.NumError).Error()) + }() + + Unwrap(strconv.Atoi("4o2")) +}