Skip to content

Commit

Permalink
增加空值比较和重命名零值赋默认值的函数名
Browse files Browse the repository at this point in the history
  • Loading branch information
yangyile committed Dec 17, 2024
1 parent 078ff96 commit 03b9b82
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 58 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
```
Expand Down
22 changes: 11 additions & 11 deletions README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,16 +201,16 @@ func main() {

`zerotern` 子包提供了一组基于指针的赋值工具函数,用于检查指针内容是否为零值,并根据需要更新其内容。这些函数可以简化对指针类型值的默认处理逻辑。

| **函数** | **参数类型** | **主值** | **备用值** | **描述** |
|--------|----------|--------|---------|-------------------------------|
| `PV` | 指针及备用值 | 指针指向的值 | 直接值 | 如果指针内容为零值,则将指针内容更新为备用值。 |
| `PF` | 指针及备用值 | 指针指向的值 | 函数返回值 | 如果指针内容为零值,则通过函数计算备用值,再更新指针内容。 |
| **函数** | **参数类型** | **主值** | **备用值** | **描述** |
|---------|----------|--------|---------|-------------------------------|
| `SetPV` | 指针及备用值 | 指针指向的值 | 直接值 | 如果指针内容为零值,则将指针内容更新为备用值。 |
| `SetPF` | 指针及备用值 | 指针指向的值 | 函数返回值 | 如果指针内容为零值,则通过函数计算备用值,再更新指针内容。 |

---

### 示例

#### 使用 `PV`
#### 使用 `SetPV`

```go
package main
Expand All @@ -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
Expand All @@ -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` 子包可以有效地处理指针内容的零值逻辑,提供了一种简洁而高效的方式来实现默认值赋值。

---

Expand Down
15 changes: 15 additions & 0 deletions nulltern/porx.go
Original file line number Diff line number Diff line change
@@ -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()
}
40 changes: 40 additions & 0 deletions nulltern/porx_test.go
Original file line number Diff line number Diff line change
@@ -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")
}))
}
51 changes: 26 additions & 25 deletions tern_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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))
}
Expand All @@ -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))
}
Expand All @@ -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))
}
Expand All @@ -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))
}
Expand All @@ -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))
}
11 changes: 8 additions & 3 deletions zero_test.go
Original file line number Diff line number Diff line change
@@ -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]())
}
4 changes: 2 additions & 2 deletions zerotern/zset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Loading

0 comments on commit 03b9b82

Please sign in to comment.