Skip to content

Commit 44e1742

Browse files
author
yangyile
committed
增加比较指针的类型
1 parent 38d1a64 commit 44e1742

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed

check_ve.go

-5
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ func VE[V any](val V, err error) *Ve[V] {
1313
}
1414
}
1515

16-
func (a *Ve[V]) Value() V {
17-
Done(a.E) //只会检查错误,而不检查结果
18-
return a.V //结果不做检查,允许返回零值,因为不是 comparable 的这里也没法比较是否为零值
19-
}
20-
2116
func (a *Ve[V]) Done() V {
2217
Done(a.E) //只会检查错误,而不检查结果
2318
return a.V //结果不做检查,允许返回零值,因为不是 comparable 的这里也没法比较是否为零值

check_vpe.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package done
2+
3+
type Vpe[V any] struct {
4+
V *V
5+
E error
6+
*Ve[*V] //这里最好不要继承自 vce-comparable 这个类型,因为指针比较地址相等或者不想等的意义不太大,因此不要提供这些无意义的功能
7+
}
8+
9+
// VPE accept two params. one is POINTER and one is ERROR interface
10+
func VPE[V any](val *V, err error) *Vpe[V] {
11+
return &Vpe[V]{
12+
V: val,
13+
E: err,
14+
Ve: VE[*V](val, err),
15+
}
16+
}
17+
18+
func (a *Vpe[V]) Sure() *V {
19+
return Sure(a.Done())
20+
}
21+
22+
func (a *Vpe[V]) Nice() *V {
23+
return Nice(a.Done())
24+
}
25+
26+
func (a *Vpe[V]) Good() {
27+
Good(a.Done())
28+
}
29+
30+
func (a *Vpe[V]) Fine() {
31+
Fine(a.Done())
32+
}
33+
34+
func (a *Vpe[V]) Safe() {
35+
Safe(a.Done())
36+
}
37+
38+
func (a *Vpe[V]) Zero() {
39+
Zero(a.Done())
40+
}
41+
42+
func (a *Vpe[V]) None() {
43+
None(a.Done())
44+
}

check_vpe_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package done
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestVpe_Sure(t *testing.T) {
10+
run := func() (*int32, error) {
11+
num := int32(100)
12+
return &num, nil
13+
}
14+
p := VPE(run()).Sure()
15+
t.Log(*p)
16+
require.Equal(t, int32(100), *p)
17+
}
18+
19+
func TestVpe_Nice(t *testing.T) {
20+
type exampleType struct {
21+
num int64
22+
}
23+
24+
run := func() (*exampleType, error) {
25+
return &exampleType{num: 200}, nil
26+
}
27+
p := VPE(run()).Sure()
28+
t.Log(p)
29+
require.Equal(t, int64(200), p.num)
30+
}
31+
32+
func TestVpe_None(t *testing.T) {
33+
run := func() (*int64, error) {
34+
return nil, nil
35+
}
36+
VPE(run()).None()
37+
}

0 commit comments

Comments
 (0)