From 03b9b822f02c82281cf5b307a6b2d9841e14f1d4 Mon Sep 17 00:00:00 2001 From: yangyile Date: Wed, 18 Dec 2024 01:57:03 +0700 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=A9=BA=E5=80=BC=E6=AF=94?= =?UTF-8?q?=E8=BE=83=E5=92=8C=E9=87=8D=E5=91=BD=E5=90=8D=E9=9B=B6=E5=80=BC?= =?UTF-8?q?=E8=B5=8B=E9=BB=98=E8=AE=A4=E5=80=BC=E7=9A=84=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/release.yml | 6 +++-- README.md | 10 +++---- README.zh.md | 22 +++++++-------- nulltern/porx.go | 15 +++++++++++ nulltern/porx_test.go | 40 +++++++++++++++++++++++++++ tern_test.go | 51 ++++++++++++++++++----------------- zero_test.go | 11 +++++--- zerotern/zset.go | 4 +-- zerotern/zset_test.go | 20 +++++++------- 9 files changed, 121 insertions(+), 58 deletions(-) create mode 100644 nulltern/porx.go create mode 100644 nulltern/porx_test.go diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 7af6886..b66f75d 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -42,6 +42,7 @@ jobs: path-to-profile: /tmp/coverage/combined.txt flag-name: Go-${{ matrix.go }} parallel: true + if: ${{ github.event.repository.fork == false }} # 仅在非 fork 时上传覆盖率 check-coverage: name: Check coverage @@ -51,14 +52,15 @@ jobs: - uses: shogo82148/actions-goveralls@v1 with: parallel-finished: true + if: ${{ github.event.repository.fork == false }} # 仅在非 fork 时检查覆盖率 # 发布 Release release: name: Release a new version needs: [ lint, test ] runs-on: ubuntu-latest - # 仅在推送标签时执行 - if: ${{ success() && startsWith(github.ref, 'refs/tags/v') }} + # 仅在推送标签时执行 - && - 仅在非 fork 时执行发布 + if: ${{ github.event.repository.fork == false && success() && startsWith(github.ref, 'refs/tags/v') }} steps: # 1. 检出代码 - name: Checkout code diff --git a/README.md b/README.md index 1eb2b8c..5798abe 100644 --- a/README.md +++ b/README.md @@ -153,10 +153,10 @@ func main() { | **Function** | **Pointer Handling** | **Fallback Value** | |--------------|-------------------------|--------------------------| -| `PV` | Pointer to direct value | Direct value | -| `PF` | Pointer to direct value | Function returning value | +| `SetPV` | Pointer to direct value | Direct value | +| `SetPF` | Pointer to direct value | Function returning value | -#### Example: Using `PV` and `PF` +#### Example: Using `SetPV` and `SetPF` ```go package main @@ -168,11 +168,11 @@ import ( func main() { var value int - zerotern.PV(&value, 42) + zerotern.SetPV(&value, 42) fmt.Println(value) // Output: 42 value = 7 - zerotern.PF(&value, func() int { return 99 }) + zerotern.SetPF(&value, func() int { return 99 }) fmt.Println(value) // Output: 7 } ``` diff --git a/README.zh.md b/README.zh.md index 9f17712..d67fe9d 100644 --- a/README.zh.md +++ b/README.zh.md @@ -201,16 +201,16 @@ func main() { `zerotern` 子包提供了一组基于指针的赋值工具函数,用于检查指针内容是否为零值,并根据需要更新其内容。这些函数可以简化对指针类型值的默认处理逻辑。 -| **函数** | **参数类型** | **主值** | **备用值** | **描述** | -|--------|----------|--------|---------|-------------------------------| -| `PV` | 指针及备用值 | 指针指向的值 | 直接值 | 如果指针内容为零值,则将指针内容更新为备用值。 | -| `PF` | 指针及备用值 | 指针指向的值 | 函数返回值 | 如果指针内容为零值,则通过函数计算备用值,再更新指针内容。 | +| **函数** | **参数类型** | **主值** | **备用值** | **描述** | +|---------|----------|--------|---------|-------------------------------| +| `SetPV` | 指针及备用值 | 指针指向的值 | 直接值 | 如果指针内容为零值,则将指针内容更新为备用值。 | +| `SetPF` | 指针及备用值 | 指针指向的值 | 函数返回值 | 如果指针内容为零值,则通过函数计算备用值,再更新指针内容。 | --- ### 示例 -#### 使用 `PV` +#### 使用 `SetPV` ```go package main @@ -224,15 +224,15 @@ func main() { value := "" ptr := &value - zerotern.PV(ptr, "备用值") + zerotern.SetPV(ptr, "备用值") fmt.Println(*ptr) // 输出: "备用值" - zerotern.PV(ptr, "新值") + zerotern.SetPV(ptr, "新值") fmt.Println(*ptr) // 输出: "备用值"(因为内容已经非零值) } ``` -#### 使用 `PF` +#### 使用 `SetPF` ```go package main @@ -248,17 +248,17 @@ func main() { fallbackFunc := func() string { return "函数计算的备用值" } - zerotern.PF(ptr, fallbackFunc) + zerotern.SetPF(ptr, fallbackFunc) fmt.Println(*ptr) // 输出: "函数计算的备用值" - zerotern.PF(ptr, func() string { return "新计算值" }) + zerotern.SetPF(ptr, func() string { return "新计算值" }) fmt.Println(*ptr) // 输出: "函数计算的备用值"(因为内容已经非零值) } ``` --- -通过 `PV` 和 `PF`,`zerotern` 子包可以有效地处理指针内容的零值逻辑,提供了一种简洁而高效的方式来实现默认值赋值。 +通过 `SetPV` 和 `SetPF`,`zerotern` 子包可以有效地处理指针内容的零值逻辑,提供了一种简洁而高效的方式来实现默认值赋值。 --- diff --git a/nulltern/porx.go b/nulltern/porx.go new file mode 100644 index 0000000..8da1896 --- /dev/null +++ b/nulltern/porx.go @@ -0,0 +1,15 @@ +package nulltern + +func PP[T any](a *T, b *T) *T { + if a != nil { + return a + } + return b +} + +func PF[T any](a *T, b func() *T) *T { + if a != nil { + return a + } + return b() +} diff --git a/nulltern/porx_test.go b/nulltern/porx_test.go new file mode 100644 index 0000000..f25c59b --- /dev/null +++ b/nulltern/porx_test.go @@ -0,0 +1,40 @@ +package nulltern_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + "github.com/yyle88/tern/nulltern" +) + +type Example[T comparable] struct{ a T } + +func newExample[T comparable](a T) *Example[T] { + return &Example[T]{a: a} +} + +func compare[T comparable](t *testing.T, value T, example *Example[T]) { + require.Equal(t, value, example.a) +} + +func TestPP(t *testing.T) { + compare(t, 1, nulltern.PP(nil, newExample(1))) + compare(t, 1, nulltern.PP(newExample(1), newExample(2))) + compare(t, "a", nulltern.PP(nil, newExample("a"))) + compare(t, "a", nulltern.PP(newExample("a"), newExample("b"))) +} + +func TestPF(t *testing.T) { + compare(t, 1, nulltern.PF(nil, func() *Example[int] { + return newExample(1) + })) + compare(t, 1, nulltern.PF(newExample(1), func() *Example[int] { + return newExample(2) + })) + compare(t, "a", nulltern.PF(nil, func() *Example[string] { + return newExample("a") + })) + compare(t, "a", nulltern.PF(newExample("a"), func() *Example[string] { + return newExample("b") + })) +} diff --git a/tern_test.go b/tern_test.go index dc7d586..bd40ceb 100644 --- a/tern_test.go +++ b/tern_test.go @@ -1,30 +1,31 @@ -package tern +package tern_test import ( "testing" "github.com/stretchr/testify/require" + "github.com/yyle88/tern" ) func TestBVV(t *testing.T) { - require.Equal(t, 1, BVV(true, 1, 2)) - require.Equal(t, 2, BVV(false, 1, 2)) + require.Equal(t, 1, tern.BVV(true, 1, 2)) + require.Equal(t, 2, tern.BVV(false, 1, 2)) } func TestBVF(t *testing.T) { run := func() string { return "b" } - require.Equal(t, "a", BVF(true, "a", run)) - require.Equal(t, "b", BVF(false, "a", run)) + require.Equal(t, "a", tern.BVF(true, "a", run)) + require.Equal(t, "b", tern.BVF(false, "a", run)) } func TestBFV(t *testing.T) { run := func() float64 { return 1.0 } - require.Equal(t, 1.0, BFV(true, run, 2.0)) - require.Equal(t, 2.0, BFV(false, run, 2.0)) + require.Equal(t, 1.0, tern.BFV(true, run, 2.0)) + require.Equal(t, 2.0, tern.BFV(false, run, 2.0)) } func TestBFF(t *testing.T) { @@ -34,15 +35,15 @@ func TestBFF(t *testing.T) { run2 := func() float64 { return 2.0 } - require.Equal(t, 1.0, BFF(true, run1, run2)) - require.Equal(t, 2.0, BFF(false, run1, run2)) + require.Equal(t, 1.0, tern.BFF(true, run1, run2)) + require.Equal(t, 2.0, tern.BFF(false, run1, run2)) } func TestFVV(t *testing.T) { - require.Equal(t, 1, FVV(func() bool { + require.Equal(t, 1, tern.FVV(func() bool { return true }, 1, 2)) - require.Equal(t, 2, FVV(func() bool { + require.Equal(t, 2, tern.FVV(func() bool { return false }, 1, 2)) } @@ -51,10 +52,10 @@ func TestFVF(t *testing.T) { run := func() string { return "b" } - require.Equal(t, "a", FVF(func() bool { + require.Equal(t, "a", tern.FVF(func() bool { return true }, "a", run)) - require.Equal(t, "b", FVF(func() bool { + require.Equal(t, "b", tern.FVF(func() bool { return false }, "a", run)) } @@ -63,10 +64,10 @@ func TestFFV(t *testing.T) { run := func() float64 { return 1.0 } - require.Equal(t, 1.0, FFV(func() bool { + require.Equal(t, 1.0, tern.FFV(func() bool { return true }, run, 2.0)) - require.Equal(t, 2.0, FFV(func() bool { + require.Equal(t, 2.0, tern.FFV(func() bool { return false }, run, 2.0)) } @@ -78,32 +79,32 @@ func TestFFF(t *testing.T) { run2 := func() float64 { return 2.0 } - require.Equal(t, 1.0, FFF(func() bool { + require.Equal(t, 1.0, tern.FFF(func() bool { return true }, run1, run2)) - require.Equal(t, 2.0, FFF(func() bool { + require.Equal(t, 2.0, tern.FFF(func() bool { return false }, run1, run2)) } func TestBV(t *testing.T) { - require.Equal(t, 1, BV(true, 1)) - require.Equal(t, 0, BV(false, 1)) + require.Equal(t, 1, tern.BV(true, 1)) + require.Equal(t, 0, tern.BV(false, 1)) } func TestBF(t *testing.T) { run := func() string { return "a" } - require.Equal(t, "a", BF(true, run)) - require.Equal(t, "", BF(false, run)) + require.Equal(t, "a", tern.BF(true, run)) + require.Equal(t, "", tern.BF(false, run)) } func TestFV(t *testing.T) { - require.Equal(t, 1, FV(func() bool { + require.Equal(t, 1, tern.FV(func() bool { return true }, 1)) - require.Equal(t, 0, FV(func() bool { + require.Equal(t, 0, tern.FV(func() bool { return false }, 1)) } @@ -112,10 +113,10 @@ func TestFF(t *testing.T) { run := func() string { return "a" } - require.Equal(t, "a", FF(func() bool { + require.Equal(t, "a", tern.FF(func() bool { return true }, run)) - require.Equal(t, "", FF(func() bool { + require.Equal(t, "", tern.FF(func() bool { return false }, run)) } diff --git a/zero_test.go b/zero_test.go index 5a6d449..39adc4b 100644 --- a/zero_test.go +++ b/zero_test.go @@ -1,10 +1,15 @@ -package tern +package tern_test import ( "testing" + + "github.com/stretchr/testify/require" + "github.com/yyle88/tern" ) func TestZero(t *testing.T) { - res := Zero[int]() - t.Log(res) + require.Equal(t, 0, tern.Zero[int]()) + require.Equal(t, "", tern.Zero[string]()) + require.Equal(t, 0.0, tern.Zero[float64]()) + require.Equal(t, rune(0), tern.Zero[rune]()) } diff --git a/zerotern/zset.go b/zerotern/zset.go index ed85b31..2ff6a7c 100644 --- a/zerotern/zset.go +++ b/zerotern/zset.go @@ -2,13 +2,13 @@ package zerotern import "github.com/yyle88/tern" -func PV[T comparable](p *T, b T) { +func SetPV[T comparable](p *T, b T) { if *p == tern.Zero[T]() { *p = b } } -func PF[T comparable](p *T, b func() T) { +func SetPF[T comparable](p *T, b func() T) { if *p == tern.Zero[T]() { *p = b() } diff --git a/zerotern/zset_test.go b/zerotern/zset_test.go index 039ffd0..e4e7208 100644 --- a/zerotern/zset_test.go +++ b/zerotern/zset_test.go @@ -7,45 +7,45 @@ import ( "github.com/yyle88/tern/zerotern" ) -func TestPV(t *testing.T) { +func TestSetPV(t *testing.T) { a := 0 - zerotern.PV(&a, 1) + zerotern.SetPV(&a, 1) require.Equal(t, 1, a) b := 1 - zerotern.PV(&b, 2) + zerotern.SetPV(&b, 2) require.Equal(t, 1, b) c := "" - zerotern.PV(&c, "a") + zerotern.SetPV(&c, "a") require.Equal(t, "a", c) s := "a" - zerotern.PV(&s, "b") + zerotern.SetPV(&s, "b") require.Equal(t, "a", s) } -func TestPF(t *testing.T) { +func TestSetPF(t *testing.T) { a := 0 - zerotern.PF(&a, func() int { + zerotern.SetPF(&a, func() int { return 1 }) require.Equal(t, 1, a) b := 1 - zerotern.PF(&b, func() int { + zerotern.SetPF(&b, func() int { return 2 }) require.Equal(t, 1, b) c := "" - zerotern.PF(&c, func() string { + zerotern.SetPF(&c, func() string { return "a" }) require.Equal(t, "a", c) s := "a" - zerotern.PF(&s, func() string { + zerotern.SetPF(&s, func() string { return "b" }) require.Equal(t, "a", s)