From d89127dd21e35efc939dc9b2673bff51b81636a3 Mon Sep 17 00:00:00 2001 From: yangyile Date: Wed, 20 Nov 2024 18:27:31 +0700 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=AE=80=E5=8D=95=E7=9A=84?= =?UTF-8?q?=E5=A4=9A=E8=BF=94=E5=9B=9E=E5=80=BC=E9=94=99=E8=AF=AF=E6=A3=80?= =?UTF-8?q?=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 89 ++++++++++++++++++ check_value.go | 12 +++ check_vpe.go | 8 ++ internal/demos/demo3/main.go | 135 +++++++++++++++++++++++++++ nc.go | 95 +++++++++++++++++++ nc_test.go | 129 ++++++++++++++++++++++++++ np.go | 95 +++++++++++++++++++ np_test.go | 174 +++++++++++++++++++++++++++++++++++ nv.go | 50 ++++++++++ nv_test.go | 129 ++++++++++++++++++++++++++ utils_check.go | 8 +- 11 files changed, 922 insertions(+), 2 deletions(-) create mode 100644 internal/demos/demo3/main.go create mode 100644 nc.go create mode 100644 nc_test.go create mode 100644 np.go create mode 100644 np_test.go create mode 100644 nv.go create mode 100644 nv_test.go diff --git a/README.md b/README.md index bc5dfd2..68a086d 100644 --- a/README.md +++ b/README.md @@ -140,6 +140,95 @@ See [demo2](internal/demos/demo2/main.go) This way, you can check if the error exists at each step and continue with the result without writing long error-handling logic. +### Example Usage + +This section demonstrates how you can simplify error handling and result validation using the `done` package. By using `done.P1`, `done.P2`, and `done.P3`, you can reduce the complexity of your error checks and nil checks, making your code more readable. + +#### Example 1: Checking Results from `run1()` + +Before using `done.P1`, we would have to manually check the error and handle nil values for the result: + +```go +a, err := run1() +if err != nil { + panic(err) // If an error occurs, trigger a panic +} +if a == nil { + panic("a is nil") // If 'a' is nil, trigger panic +} +fmt.Println(a) // Output the result +``` + +With `done.P1`, you can perform the same checks in a cleaner, more concise way: + +```go +a := done.P1(run1()) // done.P1 handles error checking and nil validation +fmt.Println(a) // Output the result +``` + +#### Example 2: Handling Multiple Results from `run2()` + +For functions that return multiple values, such as `run2()`, you can use a similar approach: + +```go +a, b, err := run2() +if err != nil { + panic(err) // If an error occurs, trigger a panic +} +if a == nil { + panic("a is nil") // If 'a' is nil, trigger panic +} +if b == nil { + panic("b is nil") // If 'b' is nil, trigger panic +} +fmt.Println(a, b) // Output both 'a' and 'b' +``` + +With `done.P2`, the error handling and nil checks are simplified: + +```go +a, b := done.P2(run2()) // done.P2 simplifies error checking and validation +fmt.Println(a, b) // Output both 'a' and 'b' +``` + +#### Example 3: Handling Multiple Results from `run3()` + +For functions that return even more values, like `run3()`, you can extend this pattern: + +```go +a, b, c, err := run3() +if err != nil { + panic(err) // If an error occurs, trigger a panic +} +if a == nil { + panic("a is nil") // If 'a' is nil, trigger panic +} +if b == nil { + panic("b is nil") // If 'b' is nil, trigger panic +} +if c == nil { + panic("c is nil") // If 'c' is nil, trigger panic +} +fmt.Println(a, b, c) // Output 'a', 'b', and 'c' +``` + +Using `done.P3` simplifies error handling and validation for multiple results: + +```go +a, b, c := done.P3(run3()) // done.P3 simplifies error checking and validation +fmt.Println(a, b, c) // Output 'a', 'b', and 'c' +``` + +See [demo3](internal/demos/demo3/main.go) + +This way you can avoid checking whether the return value has errors or null values, which is very convenient. + +There are also these methods that can provide additional convenience. +`done.P1() - done.P9()` +`done.C1() - done.C9()` +`done.V1() - done.V9()` +This avoids always checking if there are errors in the return values. + ## Usage Scenarios This package is ideal for projects where: diff --git a/check_value.go b/check_value.go index c15347b..51bf9a8 100644 --- a/check_value.go +++ b/check_value.go @@ -59,3 +59,15 @@ func None[V comparable](v V) { panic(errors.New("SHOULD BE NONE BUT NOT NONE")) } } + +func Null[V *any](v any) { + if v != nil { + panic(errors.New("SHOULD BE NULL BUT IS FULL")) + } +} + +func Full[V *any](v any) { + if v == nil { + panic(errors.New("SHOULD BE FULL BUT IS NULL")) + } +} diff --git a/check_vpe.go b/check_vpe.go index dc6fdf3..465048b 100644 --- a/check_vpe.go +++ b/check_vpe.go @@ -42,3 +42,11 @@ func (a *Vpe[V]) Zero() { func (a *Vpe[V]) None() { None(a.Done()) } + +func (a *Vpe[V]) Null() { + Null(a.Done()) +} + +func (a *Vpe[V]) Full() { + Full(a.Done()) +} diff --git a/internal/demos/demo3/main.go b/internal/demos/demo3/main.go new file mode 100644 index 0000000..377dc59 --- /dev/null +++ b/internal/demos/demo3/main.go @@ -0,0 +1,135 @@ +package main + +import ( + "fmt" + + "github.com/yyle88/done" +) + +func main() { + // Directly checking the result of run1 with error handling + // 使用直接方法检查 run1 的结果并处理错误 + { + a, err := run1() + if err != nil { + panic(err) // If an error occurs, trigger a panic + // 如果发生错误,触发 panic + } + if a == nil { + panic("a is nil") // If 'a' is nil, trigger panic + // 如果 'a' 为 nil,触发 panic + } + fmt.Println(a) // Output the result + // 输出结果 + } + + // Using done.P1 for cleaner error handling and result validation + // 使用 done.P1 简化错误处理和结果验证 + { + a := done.P1(run1()) // done.P1 handles error checking and nil validation + // done.P1 处理错误检查和 nil 验证 + fmt.Println(a) // Output the result + // 输出结果 + } + + // Directly checking the result of run2 with error handling + // 使用直接方法检查 run2 的结果并处理错误 + { + a, b, err := run2() + if err != nil { + panic(err) // If an error occurs, trigger a panic + // 如果发生错误,触发 panic + } + if a == nil { + panic("a is nil") // If 'a' is nil, trigger panic + // 如果 'a' 为 nil,触发 panic + } + if b == nil { + panic("b is nil") // If 'b' is nil, trigger panic + // 如果 'b' 为 nil,触发 panic + } + fmt.Println(a, b) // Output both 'a' and 'b' + // 输出 'a' 和 'b' + } + + // Using done.P2 for more concise error handling and result validation + // 使用 done.P2 使错误处理和结果验证更简洁 + { + a, b := done.P2(run2()) // done.P2 simplifies error checking and validation + // done.P2 简化了错误检查和验证 + fmt.Println(a, b) // Output both 'a' and 'b' + // 输出 'a' 和 'b' + } + + // Directly checking the result of run3 with error handling + // 使用直接方法检查 run3 的结果并处理错误 + { + a, b, c, err := run3() + if err != nil { + panic(err) // If an error occurs, trigger a panic + // 如果发生错误,触发 panic + } + if a == nil { + panic("a is nil") // If 'a' is nil, trigger panic + // 如果 'a' 为 nil,触发 panic + } + if b == nil { + panic("b is nil") // If 'b' is nil, trigger panic + // 如果 'b' 为 nil,触发 panic + } + if c == nil { + panic("c is nil") // If 'c' is nil, trigger panic + // 如果 'c' 为 nil,触发 panic + } + fmt.Println(a, b, c) // Output 'a', 'b', and 'c' + // 输出 'a','b' 和 'c' + } + + // Using done.P3 to simplify error handling and result validation for run3 + // 使用 done.P3 简化 run3 的错误处理和结果验证 + { + a, b, c := done.P3(run3()) // done.P3 simplifies error checking and validation + // done.P3 简化了错误检查和验证 + fmt.Println(a, b, c) // Output 'a', 'b', and 'c' + // 输出 'a','b' 和 'c' + } +} + +// -------------------------- +// Simulated business logic +// -------------------------- + +// Defining mock types for demonstration with fields +// 为演示定义模拟类型并添加字段 +type A struct { + ID int + Name string +} + +type B struct { + Age int + City string +} + +type C struct { + Price float64 + Code string +} + +// run1 simulates returning an instance of A and error +// run1 模拟返回 A 的实例和错误 +func run1() (*A, error) { + return &A{ID: 1, Name: "Alice"}, nil +} + +// run2 simulates returning instances of A and B, and error +// run2 模拟返回 A 和 B 的实例以及错误 +func run2() (*A, *B, error) { + return &A{ID: 1, Name: "Alice"}, &B{Age: 30, City: "New York"}, nil +} + +// run3 simulates returning instances of A, B, and C, and error +// run3 模拟返回 A, B 和 C 的实例以及错误 +func run3() (*A, *B, *C, error) { + return &A{ID: 1, Name: "Alice"}, &B{Age: 30, City: "New York"}, &C{Price: 100.5, Code: "XYZ123"}, nil +} diff --git a/nc.go b/nc.go new file mode 100644 index 0000000..b8e27bf --- /dev/null +++ b/nc.go @@ -0,0 +1,95 @@ +package done + +func C0(err error) { + Must(err) +} + +func C1[T1 comparable](v1 T1, err error) T1 { + Must(err) + Nice(v1) + return v1 +} + +func C2[T1, T2 comparable](v1 T1, v2 T2, err error) (T1, T2) { + Must(err) + Nice(v1) + Nice(v2) + return v1, v2 +} + +func C3[T1, T2, T3 comparable](v1 T1, v2 T2, v3 T3, err error) (T1, T2, T3) { + Must(err) + Nice(v1) + Nice(v2) + Nice(v3) + return v1, v2, v3 +} + +func C4[T1, T2, T3, T4 comparable](v1 T1, v2 T2, v3 T3, v4 T4, err error) (T1, T2, T3, T4) { + Must(err) + Nice(v1) + Nice(v2) + Nice(v3) + Nice(v4) + return v1, v2, v3, v4 +} + +func C5[T1, T2, T3, T4, T5 comparable](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, err error) (T1, T2, T3, T4, T5) { + Must(err) + Nice(v1) + Nice(v2) + Nice(v3) + Nice(v4) + Nice(v5) + return v1, v2, v3, v4, v5 +} + +func C6[T1, T2, T3, T4, T5, T6 comparable](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, v6 T6, err error) (T1, T2, T3, T4, T5, T6) { + Must(err) + Nice(v1) + Nice(v2) + Nice(v3) + Nice(v4) + Nice(v5) + Nice(v6) + return v1, v2, v3, v4, v5, v6 +} + +func C7[T1, T2, T3, T4, T5, T6, T7 comparable](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, v6 T6, v7 T7, err error) (T1, T2, T3, T4, T5, T6, T7) { + Must(err) + Nice(v1) + Nice(v2) + Nice(v3) + Nice(v4) + Nice(v5) + Nice(v6) + Nice(v7) + return v1, v2, v3, v4, v5, v6, v7 +} + +func C8[T1, T2, T3, T4, T5, T6, T7, T8 comparable](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, v6 T6, v7 T7, v8 T8, err error) (T1, T2, T3, T4, T5, T6, T7, T8) { + Must(err) + Nice(v1) + Nice(v2) + Nice(v3) + Nice(v4) + Nice(v5) + Nice(v6) + Nice(v7) + Nice(v8) + return v1, v2, v3, v4, v5, v6, v7, v8 +} + +func C9[T1, T2, T3, T4, T5, T6, T7, T8, T9 comparable](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, v6 T6, v7 T7, v8 T8, v9 T9, err error) (T1, T2, T3, T4, T5, T6, T7, T8, T9) { + Must(err) + Nice(v1) + Nice(v2) + Nice(v3) + Nice(v4) + Nice(v5) + Nice(v6) + Nice(v7) + Nice(v8) + Nice(v9) + return v1, v2, v3, v4, v5, v6, v7, v8, v9 +} diff --git a/nc_test.go b/nc_test.go new file mode 100644 index 0000000..1a0c7c9 --- /dev/null +++ b/nc_test.go @@ -0,0 +1,129 @@ +package done_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + "github.com/yyle88/done" +) + +func TestC0(t *testing.T) { + done.C0(error(nil)) +} + +func TestC1(t *testing.T) { + run := func() (string, error) { + return "hello", nil + } + + v1 := done.C1(run()) + require.Equal(t, "hello", v1) +} + +func TestC2(t *testing.T) { + run := func() (int, float64, error) { + return 42, 3.14, nil + } + + v1, v2 := done.C2(run()) + require.Equal(t, 42, v1) + require.Equal(t, 3.14, v2) +} + +func TestC3(t *testing.T) { + run := func() (bool, byte, rune, error) { + return true, 'A', '中', nil + } + + v1, v2, v3 := done.C3(run()) + require.True(t, v1) + require.Equal(t, byte('A'), v2) + require.Equal(t, '中', v3) +} + +func TestC4(t *testing.T) { + run := func() (string, int, uint, float32, error) { + return "world", -7, 7, 1.23, nil + } + + v1, v2, v3, v4 := done.C4(run()) + require.Equal(t, "world", v1) + require.Equal(t, -7, v2) + require.Equal(t, uint(7), v3) + require.Equal(t, float32(1.23), v4) +} + +func TestC5(t *testing.T) { + run := func() (uint8, uint16, uint32, uint64, string, error) { + return 8, 16, 32, 64, "test", nil + } + + v1, v2, v3, v4, v5 := done.C5(run()) + require.Equal(t, uint8(8), v1) + require.Equal(t, uint16(16), v2) + require.Equal(t, uint32(32), v3) + require.Equal(t, uint64(64), v4) + require.Equal(t, "test", v5) +} + +func TestC6(t *testing.T) { + run := func() (float64, complex64, complex128, bool, string, int, error) { + return 6.28, complex(1, 2), complex(3, 4), true, "check", -1, nil + } + + v1, v2, v3, v4, v5, v6 := done.C6(run()) + require.Equal(t, 6.28, v1) + require.Equal(t, complex64(complex(1, 2)), v2) + require.Equal(t, complex(3, 4), v3) + require.True(t, v4) + require.Equal(t, "check", v5) + require.Equal(t, -1, v6) +} + +func TestC7(t *testing.T) { + run := func() (uint, uint8, uint16, uint32, uint64, int, int8, error) { + return 1, 2, 3, 4, 5, 6, 7, nil + } + + v1, v2, v3, v4, v5, v6, v7 := done.C7(run()) + require.Equal(t, uint(1), v1) + require.Equal(t, uint8(2), v2) + require.Equal(t, uint16(3), v3) + require.Equal(t, uint32(4), v4) + require.Equal(t, uint64(5), v5) + require.Equal(t, 6, v6) + require.Equal(t, int8(7), v7) +} + +func TestC8(t *testing.T) { + run := func() (string, rune, byte, bool, float32, float64, int32, int64, error) { + return "go", '中', 'B', true, 2.71, 1.618, -32, -64, nil + } + + v1, v2, v3, v4, v5, v6, v7, v8 := done.C8(run()) + require.Equal(t, "go", v1) + require.Equal(t, '中', v2) + require.Equal(t, byte('B'), v3) + require.True(t, v4) + require.Equal(t, float32(2.71), v5) + require.Equal(t, 1.618, v6) + require.Equal(t, int32(-32), v7) + require.Equal(t, int64(-64), v8) +} + +func TestC9(t *testing.T) { + run := func() (string, uint8, int16, uint32, int64, float32, float64, complex64, complex128, error) { + return "done", 255, -16, 32, -64, 3.14, 2.71, complex(5, 6), complex(7, 8), nil + } + + v1, v2, v3, v4, v5, v6, v7, v8, v9 := done.C9(run()) + require.Equal(t, "done", v1) + require.Equal(t, uint8(255), v2) + require.Equal(t, int16(-16), v3) + require.Equal(t, uint32(32), v4) + require.Equal(t, int64(-64), v5) + require.Equal(t, float32(3.14), v6) + require.Equal(t, 2.71, v7) + require.Equal(t, complex64(complex(5, 6)), v8) + require.Equal(t, complex(7, 8), v9) +} diff --git a/np.go b/np.go new file mode 100644 index 0000000..5a18ce5 --- /dev/null +++ b/np.go @@ -0,0 +1,95 @@ +package done + +func P0(err error) { + Must(err) +} + +func P1[T1 any](v1 *T1, err error) *T1 { + Must(err) + Full(v1) + return v1 +} + +func P2[T1, T2 any](v1 *T1, v2 *T2, err error) (*T1, *T2) { + Must(err) + Full(v1) + Full(v2) + return v1, v2 +} + +func P3[T1, T2, T3 any](v1 *T1, v2 *T2, v3 *T3, err error) (*T1, *T2, *T3) { + Must(err) + Full(v1) + Full(v2) + Full(v3) + return v1, v2, v3 +} + +func P4[T1, T2, T3, T4 any](v1 *T1, v2 *T2, v3 *T3, v4 *T4, err error) (*T1, *T2, *T3, *T4) { + Must(err) + Full(v1) + Full(v2) + Full(v3) + Full(v4) + return v1, v2, v3, v4 +} + +func P5[T1, T2, T3, T4, T5 any](v1 *T1, v2 *T2, v3 *T3, v4 *T4, v5 *T5, err error) (*T1, *T2, *T3, *T4, *T5) { + Must(err) + Full(v1) + Full(v2) + Full(v3) + Full(v4) + Full(v5) + return v1, v2, v3, v4, v5 +} + +func P6[T1, T2, T3, T4, T5, T6 any](v1 *T1, v2 *T2, v3 *T3, v4 *T4, v5 *T5, v6 *T6, err error) (*T1, *T2, *T3, *T4, *T5, *T6) { + Must(err) + Full(v1) + Full(v2) + Full(v3) + Full(v4) + Full(v5) + Full(v6) + return v1, v2, v3, v4, v5, v6 +} + +func P7[T1, T2, T3, T4, T5, T6, T7 any](v1 *T1, v2 *T2, v3 *T3, v4 *T4, v5 *T5, v6 *T6, v7 *T7, err error) (*T1, *T2, *T3, *T4, *T5, *T6, *T7) { + Must(err) + Full(v1) + Full(v2) + Full(v3) + Full(v4) + Full(v5) + Full(v6) + Full(v7) + return v1, v2, v3, v4, v5, v6, v7 +} + +func P8[T1, T2, T3, T4, T5, T6, T7, T8 any](v1 *T1, v2 *T2, v3 *T3, v4 *T4, v5 *T5, v6 *T6, v7 *T7, v8 *T8, err error) (*T1, *T2, *T3, *T4, *T5, *T6, *T7, *T8) { + Must(err) + Full(v1) + Full(v2) + Full(v3) + Full(v4) + Full(v5) + Full(v6) + Full(v7) + Full(v8) + return v1, v2, v3, v4, v5, v6, v7, v8 +} + +func P9[T1, T2, T3, T4, T5, T6, T7, T8, T9 any](v1 *T1, v2 *T2, v3 *T3, v4 *T4, v5 *T5, v6 *T6, v7 *T7, v8 *T8, v9 *T9, err error) (*T1, *T2, *T3, *T4, *T5, *T6, *T7, *T8, *T9) { + Must(err) + Full(v1) + Full(v2) + Full(v3) + Full(v4) + Full(v5) + Full(v6) + Full(v7) + Full(v8) + Full(v9) + return v1, v2, v3, v4, v5, v6, v7, v8, v9 +} diff --git a/np_test.go b/np_test.go new file mode 100644 index 0000000..ec30765 --- /dev/null +++ b/np_test.go @@ -0,0 +1,174 @@ +package done_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + "github.com/yyle88/done" +) + +func TestP0(t *testing.T) { + done.P0(error(nil)) +} + +func TestP1(t *testing.T) { + run := func() (*string, error) { + v1 := "hello" + return &v1, nil + } + + p1 := done.P1(run()) + require.Equal(t, "hello", *p1) +} + +func TestP2(t *testing.T) { + run := func() (*int, *float64, error) { + v1 := 42 + v2 := 3.14 + return &v1, &v2, nil + } + + v1, v2 := done.P2(run()) + require.Equal(t, 42, *v1) + require.Equal(t, 3.14, *v2) +} + +func TestP3(t *testing.T) { + run := func() (*string, *int, *bool, error) { + v1 := "test" + v2 := 100 + v3 := true + return &v1, &v2, &v3, nil + } + + v1, v2, v3 := done.P3(run()) + require.Equal(t, "test", *v1) + require.Equal(t, 100, *v2) + require.Equal(t, true, *v3) +} + +func TestP4(t *testing.T) { + run := func() (*int, *float64, *bool, *string, error) { + v1 := 42 + v2 := 3.14 + v3 := false + v4 := "world" + return &v1, &v2, &v3, &v4, nil + } + + v1, v2, v3, v4 := done.P4(run()) + require.Equal(t, 42, *v1) + require.Equal(t, 3.14, *v2) + require.Equal(t, false, *v3) + require.Equal(t, "world", *v4) +} + +func TestP5(t *testing.T) { + run := func() (*string, *int, *bool, *float64, *rune, error) { + v1 := "test" + v2 := 123 + v3 := true + v4 := 2.718 + v5 := 'a' + return &v1, &v2, &v3, &v4, &v5, nil + } + + v1, v2, v3, v4, v5 := done.P5(run()) + require.Equal(t, "test", *v1) + require.Equal(t, 123, *v2) + require.Equal(t, true, *v3) + require.Equal(t, 2.718, *v4) + require.Equal(t, 'a', *v5) +} + +func TestP6(t *testing.T) { + run := func() (*float64, *int, *string, *bool, *rune, *int64, error) { + v1 := 3.14 + v2 := 42 + v3 := "hello" + v4 := true + v5 := 'x' + v6 := int64(100000) + return &v1, &v2, &v3, &v4, &v5, &v6, nil + } + + v1, v2, v3, v4, v5, v6 := done.P6(run()) + require.Equal(t, 3.14, *v1) + require.Equal(t, 42, *v2) + require.Equal(t, "hello", *v3) + require.Equal(t, true, *v4) + require.Equal(t, 'x', *v5) + require.Equal(t, int64(100000), *v6) +} + +func TestP7(t *testing.T) { + run := func() (*bool, *string, *int, *float64, *rune, *int64, *uint32, error) { + v1 := true + v2 := "world" + v3 := 50 + v4 := 1.618 + v5 := 'y' + v6 := int64(200000) + v7 := uint32(300) + return &v1, &v2, &v3, &v4, &v5, &v6, &v7, nil + } + + v1, v2, v3, v4, v5, v6, v7 := done.P7(run()) + require.Equal(t, true, *v1) + require.Equal(t, "world", *v2) + require.Equal(t, 50, *v3) + require.Equal(t, 1.618, *v4) + require.Equal(t, 'y', *v5) + require.Equal(t, int64(200000), *v6) + require.Equal(t, uint32(300), *v7) +} + +func TestP8(t *testing.T) { + run := func() (*string, *int, *bool, *float64, *rune, *int64, *uint32, *uint8, error) { + v1 := "abc" + v2 := 999 + v3 := false + v4 := 0.577 + v5 := 'z' + v6 := int64(500000) + v7 := uint32(400) + v8 := uint8(255) + return &v1, &v2, &v3, &v4, &v5, &v6, &v7, &v8, nil + } + + v1, v2, v3, v4, v5, v6, v7, v8 := done.P8(run()) + require.Equal(t, "abc", *v1) + require.Equal(t, 999, *v2) + require.Equal(t, false, *v3) + require.Equal(t, 0.577, *v4) + require.Equal(t, 'z', *v5) + require.Equal(t, int64(500000), *v6) + require.Equal(t, uint32(400), *v7) + require.Equal(t, uint8(255), *v8) +} + +func TestP9(t *testing.T) { + run := func() (*string, *int, *bool, *float64, *rune, *int64, *uint32, *uint8, *uint64, error) { + v1 := "final" + v2 := 888 + v3 := true + v4 := 2.718 + v5 := 'p' + v6 := int64(900000) + v7 := uint32(500) + v8 := uint8(100) + v9 := uint64(1000000) + return &v1, &v2, &v3, &v4, &v5, &v6, &v7, &v8, &v9, nil + } + + v1, v2, v3, v4, v5, v6, v7, v8, v9 := done.P9(run()) + require.Equal(t, "final", *v1) + require.Equal(t, 888, *v2) + require.Equal(t, true, *v3) + require.Equal(t, 2.718, *v4) + require.Equal(t, 'p', *v5) + require.Equal(t, int64(900000), *v6) + require.Equal(t, uint32(500), *v7) + require.Equal(t, uint8(100), *v8) + require.Equal(t, uint64(1000000), *v9) +} diff --git a/nv.go b/nv.go new file mode 100644 index 0000000..2029dbe --- /dev/null +++ b/nv.go @@ -0,0 +1,50 @@ +package done + +func V0(err error) { + Must(err) +} + +func V1[T1 any](v1 T1, err error) T1 { + Must(err) + return v1 +} + +func V2[T1, T2 any](v1 T1, v2 T2, err error) (T1, T2) { + Must(err) + return v1, v2 +} + +func V3[T1, T2, T3 any](v1 T1, v2 T2, v3 T3, err error) (T1, T2, T3) { + Must(err) + return v1, v2, v3 +} + +func V4[T1, T2, T3, T4 any](v1 T1, v2 T2, v3 T3, v4 T4, err error) (T1, T2, T3, T4) { + Must(err) + return v1, v2, v3, v4 +} + +func V5[T1, T2, T3, T4, T5 any](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, err error) (T1, T2, T3, T4, T5) { + Must(err) + return v1, v2, v3, v4, v5 +} + +func V6[T1, T2, T3, T4, T5, T6 any](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, v6 T6, err error) (T1, T2, T3, T4, T5, T6) { + Must(err) + return v1, v2, v3, v4, v5, v6 +} + +func V7[T1, T2, T3, T4, T5, T6, T7 any](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, v6 T6, v7 T7, err error) (T1, T2, T3, T4, T5, T6, T7) { + Must(err) + return v1, v2, v3, v4, v5, v6, v7 +} + +func V8[T1, T2, T3, T4, T5, T6, T7, T8 any](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, v6 T6, v7 T7, v8 T8, err error) (T1, T2, T3, T4, T5, T6, T7, T8) { + Must(err) + return v1, v2, v3, v4, v5, v6, v7, v8 +} + +func V9[T1, T2, T3, T4, T5, T6, T7, T8, T9 any](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, v6 T6, v7 T7, v8 T8, v9 T9, err error) (T1, T2, T3, T4, T5, T6, T7, T8, T9) { + Must(err) + return v1, v2, v3, v4, v5, v6, v7, v8, v9 +} diff --git a/nv_test.go b/nv_test.go new file mode 100644 index 0000000..2114623 --- /dev/null +++ b/nv_test.go @@ -0,0 +1,129 @@ +package done_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + "github.com/yyle88/done" +) + +func TestV0(t *testing.T) { + done.V0(error(nil)) +} + +func TestV1(t *testing.T) { + run := func() (string, error) { + return "a", nil + } + + v1 := done.V1(run()) + require.Equal(t, "a", v1) +} + +func TestV2(t *testing.T) { + run := func() (string, uint64, error) { + return "a", 2, nil + } + + v1, v2 := done.V2(run()) + require.Equal(t, "a", v1) + require.Equal(t, uint64(2), v2) +} + +func TestV3(t *testing.T) { + run := func() (string, uint64, rune, error) { + return "a", 2, 'x', nil + } + + v1, v2, v3 := done.V3(run()) + require.Equal(t, "a", v1) + require.Equal(t, uint64(2), v2) + require.Equal(t, 'x', v3) +} + +func TestV4(t *testing.T) { + run := func() (string, uint64, rune, int32, error) { + return "a", 2, 'x', 42, nil + } + + v1, v2, v3, v4 := done.V4(run()) + require.Equal(t, "a", v1) + require.Equal(t, uint64(2), v2) + require.Equal(t, 'x', v3) + require.Equal(t, int32(42), v4) +} + +func TestV5(t *testing.T) { + run := func() (string, uint64, rune, int32, float64, error) { + return "a", 2, 'x', 42, 3.14, nil + } + + v1, v2, v3, v4, v5 := done.V5(run()) + require.Equal(t, "a", v1) + require.Equal(t, uint64(2), v2) + require.Equal(t, 'x', v3) + require.Equal(t, int32(42), v4) + require.Equal(t, 3.14, v5) +} + +func TestV6(t *testing.T) { + run := func() (string, uint64, rune, int32, float64, bool, error) { + return "a", 2, 'x', 42, 3.14, true, nil + } + + v1, v2, v3, v4, v5, v6 := done.V6(run()) + require.Equal(t, "a", v1) + require.Equal(t, uint64(2), v2) + require.Equal(t, 'x', v3) + require.Equal(t, int32(42), v4) + require.Equal(t, 3.14, v5) + require.Equal(t, true, v6) +} + +func TestV7(t *testing.T) { + run := func() (string, uint64, rune, int32, float64, bool, byte, error) { + return "a", 2, 'x', 42, 3.14, true, byte(255), nil + } + + v1, v2, v3, v4, v5, v6, v7 := done.V7(run()) + require.Equal(t, "a", v1) + require.Equal(t, uint64(2), v2) + require.Equal(t, 'x', v3) + require.Equal(t, int32(42), v4) + require.Equal(t, 3.14, v5) + require.Equal(t, true, v6) + require.Equal(t, byte(255), v7) +} + +func TestV8(t *testing.T) { + run := func() (string, uint64, rune, int32, float64, bool, byte, int64, error) { + return "a", 2, 'x', 42, 3.14, true, byte(255), int64(123456789), nil + } + + v1, v2, v3, v4, v5, v6, v7, v8 := done.V8(run()) + require.Equal(t, "a", v1) + require.Equal(t, uint64(2), v2) + require.Equal(t, 'x', v3) + require.Equal(t, int32(42), v4) + require.Equal(t, 3.14, v5) + require.Equal(t, true, v6) + require.Equal(t, byte(255), v7) + require.Equal(t, int64(123456789), v8) +} + +func TestV9(t *testing.T) { + run := func() (string, uint64, rune, int32, float64, bool, byte, int64, uint32, error) { + return "a", 2, 'x', 42, 3.14, true, byte(255), int64(123456789), uint32(987654321), nil + } + + v1, v2, v3, v4, v5, v6, v7, v8, v9 := done.V9(run()) + require.Equal(t, "a", v1) + require.Equal(t, uint64(2), v2) + require.Equal(t, 'x', v3) + require.Equal(t, int32(42), v4) + require.Equal(t, 3.14, v5) + require.Equal(t, true, v6) + require.Equal(t, byte(255), v7) + require.Equal(t, int64(123456789), v8) + require.Equal(t, uint32(987654321), v9) +} diff --git a/utils_check.go b/utils_check.go index 540f214..308079e 100644 --- a/utils_check.go +++ b/utils_check.go @@ -8,32 +8,36 @@ import ( "go.uber.org/zap" ) +// Done 当err非空是就panic且打印调用栈信息 func Done(err error) { if err != nil { zaplog.LOGS.P1.Panic("ERROR", zap.Error(err)) } } +// Must 当err非空是就panic且打印调用栈信息 func Must(err error) { if err != nil { zaplog.LOGS.P1.Panic("ERROR", zap.Error(err)) } } +// Soft 当err非空是就打印warning日志且打印调用栈信息 func Soft(err error) { if err != nil { zaplog.LOGS.P1.Warn("WARN", zap.Error(err)) } } -// Fata FaFaFaFaFa 哈哈哈哈哈 +// Fata 当出错时就是调用 log.Fatal 退出系统,当不出错时就不做任何事情 +// FaFaFaFaFa 哈哈哈哈哈 func Fata(err error) { if err != nil { zaplog.LOGS.P1.Fatal("ERROR", zap.Error(err)) } } -// EExt if err != nil os.Exit(code) +// EExt 逻辑相当于 if err != nil { os.Exit(code) } 也是出错时打印红色错误信息,接着退出系统 func EExt(err error, code int) { if err != nil { var msg = fmt.Sprintf("\x1b[31;1m%s\x1b[0m", err) //红色打印