From 19c740e77f22426872d56bf57aa9dfe75b921e9a Mon Sep 17 00:00:00 2001 From: bigdavidwong <63399445+bigdavidwong@users.noreply.github.com> Date: Sat, 9 Nov 2024 11:16:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20`IfThenElse`=20=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E8=BF=94=E5=9B=9E=E6=B3=9B=E5=9E=8B=E7=BB=93=E6=9E=9C?= =?UTF-8?q?=20(#259)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * update 1. 增加了一个条件判断返回泛型结果的函数; 1. 增加了一个条件判断执行对应方法的函数; 2. 调整和增加单元测试和Example; * IfThenElseFunc固定增加错误返回,泛型T作为用户可以自定义返回的结果 --------- Co-authored-by: huangdawei --- condition.go | 32 +++++++++++++++++++++++ condition_test.go | 65 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 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..eb7f9f7 --- /dev/null +++ b/condition.go @@ -0,0 +1,32 @@ +// 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 根据条件返回对应的泛型结果 +// 注意避免结果的空指针问题 +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, error)) (T, error) { + if condition { + return trueFunc() + } + return falseFunc() +} diff --git a/condition_test.go b/condition_test.go new file mode 100644 index 0000000..272c226 --- /dev/null +++ b/condition_test.go @@ -0,0 +1,65 @@ +// 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 ( + "errors" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestIfThenElse(t *testing.T) { + i := 7 + 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) { + 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() { + code, err := IfThenElseFunc(false, func() (code int, err error) { + // do something when condition is true + // ... + return 0, nil + }, func() (code int, err error) { + // do something when condition is false + // ... + return 1, errors.New("some error when execute func2") + }) + fmt.Println(code) + fmt.Println(err) + + // Output: + // 1 + // some error when execute func2 +}