From 8d4ee5952eda9e20519932df5b14418122e2c852 Mon Sep 17 00:00:00 2001 From: huangdawei Date: Mon, 26 Aug 2024 16:51:13 +0800 Subject: [PATCH 1/4] =?UTF-8?q?update=201.=20=E5=A2=9E=E5=8A=A0=E4=BA=86?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E6=9D=A1=E4=BB=B6=E5=88=A4=E6=96=AD=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E6=B3=9B=E5=9E=8B=E7=BB=93=E6=9E=9C=E7=9A=84=E5=87=BD?= =?UTF-8?q?=E6=95=B0=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- condition.go | 24 ++++++++++++++++++++++++ condition_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 condition.go create mode 100644 condition_test.go diff --git a/condition.go b/condition.go new file mode 100644 index 0000000..01197aa --- /dev/null +++ b/condition.go @@ -0,0 +1,24 @@ +// Copyright 2021 ecodeclub +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ekit + +// IfThenElse 根据条件返回对应的泛型结果,请注意,结果值在传入时就会访问,请务必确认其合法性 +// 例:传 trueValue 时传入了一个指针对象并调用其方法,需要先判空 +func IfThenElse[T any](condition bool, trueValue, falseValue T) T { + if condition { + return trueValue + } + return falseValue +} diff --git a/condition_test.go b/condition_test.go new file mode 100644 index 0000000..e449f19 --- /dev/null +++ b/condition_test.go @@ -0,0 +1,26 @@ +// Copyright 2021 ecodeclub +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ekit + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestIfThenElse(t *testing.T) { + i := 7 + i = IfThenElse(false, i, 0) + assert.Equal(t, i, 0) +} From d22ff4f75560d00f3ef839c7efe45b589fcc73d1 Mon Sep 17 00:00:00 2001 From: huangdawei Date: Tue, 27 Aug 2024 20:37:15 +0800 Subject: [PATCH 2/4] make check --- condition_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/condition_test.go b/condition_test.go index e449f19..71a4f07 100644 --- a/condition_test.go +++ b/condition_test.go @@ -15,8 +15,9 @@ package ekit import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestIfThenElse(t *testing.T) { From a35e6c732118eeded246669a6ab8ebeaec350ceb Mon Sep 17 00:00:00 2001 From: "david.wong" Date: Sat, 2 Nov 2024 22:22:13 +0800 Subject: [PATCH 3/4] =?UTF-8?q?update=201.=20=E5=A2=9E=E5=8A=A0=E4=BA=86?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E6=9D=A1=E4=BB=B6=E5=88=A4=E6=96=AD=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E5=AF=B9=E5=BA=94=E6=96=B9=E6=B3=95=E7=9A=84=E5=87=BD?= =?UTF-8?q?=E6=95=B0=EF=BC=9B=202.=20=E8=B0=83=E6=95=B4=E5=92=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95=E5=92=8CExample?= =?UTF-8?q?=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- condition.go | 12 ++++++++++-- condition_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/condition.go b/condition.go index 01197aa..e0181be 100644 --- a/condition.go +++ b/condition.go @@ -14,11 +14,19 @@ package ekit -// IfThenElse 根据条件返回对应的泛型结果,请注意,结果值在传入时就会访问,请务必确认其合法性 -// 例:传 trueValue 时传入了一个指针对象并调用其方法,需要先判空 +// IfThenElse 根据条件返回对应的泛型结果 +// 注意避免结果的空指针问题 func IfThenElse[T any](condition bool, trueValue, falseValue T) T { if condition { return trueValue } return falseValue } + +// IfThenElseFunc 根据条件执行对应的函数并返回泛型结果 +func IfThenElseFunc[T any](condition bool, trueFunc, falseFunc func() T) T { + if condition { + return trueFunc() + } + return falseFunc() +} diff --git a/condition_test.go b/condition_test.go index 71a4f07..7df6ab3 100644 --- a/condition_test.go +++ b/condition_test.go @@ -15,6 +15,8 @@ package ekit import ( + "errors" + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -25,3 +27,36 @@ func TestIfThenElse(t *testing.T) { i = IfThenElse(false, i, 0) assert.Equal(t, i, 0) } + +func ExampleIfThenElse() { + result := IfThenElse(true, "yes", "no") + fmt.Println(result) + + // Output: + // yes +} + +func TestIfThenElseFunc(t *testing.T) { + err := IfThenElseFunc(true, func() error { + return nil + }, func() error { + return errors.New("some error") + }) + assert.NoError(t, err) +} + +func ExampleIfThenElseFunc() { + err := IfThenElseFunc(false, func() error { + // do something when condition is true + // ... + return nil + }, func() error { + // do something when condition is false + // ... + return errors.New("some error when execute func2") + }) + fmt.Println(err) + + // Output: + // some error when execute func2 +} From 317856e841367bcd17b06674abf68dcb3a0bad68 Mon Sep 17 00:00:00 2001 From: "david.wong" Date: Thu, 7 Nov 2024 16:37:53 +0800 Subject: [PATCH 4/4] =?UTF-8?q?IfThenElseFunc=E5=9B=BA=E5=AE=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E9=94=99=E8=AF=AF=E8=BF=94=E5=9B=9E=EF=BC=8C=E6=B3=9B?= =?UTF-8?q?=E5=9E=8BT=E4=BD=9C=E4=B8=BA=E7=94=A8=E6=88=B7=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E8=87=AA=E5=AE=9A=E4=B9=89=E8=BF=94=E5=9B=9E=E7=9A=84?= =?UTF-8?q?=E7=BB=93=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- condition.go | 2 +- condition_test.go | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/condition.go b/condition.go index e0181be..eb7f9f7 100644 --- a/condition.go +++ b/condition.go @@ -24,7 +24,7 @@ func IfThenElse[T any](condition bool, trueValue, falseValue T) T { } // IfThenElseFunc 根据条件执行对应的函数并返回泛型结果 -func IfThenElseFunc[T any](condition bool, trueFunc, falseFunc func() T) T { +func IfThenElseFunc[T any](condition bool, trueFunc, falseFunc func() (T, error)) (T, error) { if condition { return trueFunc() } diff --git a/condition_test.go b/condition_test.go index 7df6ab3..272c226 100644 --- a/condition_test.go +++ b/condition_test.go @@ -37,26 +37,29 @@ func ExampleIfThenElse() { } func TestIfThenElseFunc(t *testing.T) { - err := IfThenElseFunc(true, func() error { - return nil - }, func() error { - return errors.New("some error") + resp, err := IfThenElseFunc(true, func() (int, error) { + return 0, nil + }, func() (int, error) { + return 1, errors.New("some error") }) assert.NoError(t, err) + assert.Equal(t, resp, 0) } func ExampleIfThenElseFunc() { - err := IfThenElseFunc(false, func() error { + code, err := IfThenElseFunc(false, func() (code int, err error) { // do something when condition is true // ... - return nil - }, func() error { + return 0, nil + }, func() (code int, err error) { // do something when condition is false // ... - return errors.New("some error when execute func2") + return 1, errors.New("some error when execute func2") }) + fmt.Println(code) fmt.Println(err) // Output: + // 1 // some error when execute func2 }