File tree 3 files changed +81
-5
lines changed
3 files changed +81
-5
lines changed Original file line number Diff line number Diff line change @@ -13,11 +13,6 @@ func VE[V any](val V, err error) *Ve[V] {
13
13
}
14
14
}
15
15
16
- func (a * Ve [V ]) Value () V {
17
- Done (a .E ) //只会检查错误,而不检查结果
18
- return a .V //结果不做检查,允许返回零值,因为不是 comparable 的这里也没法比较是否为零值
19
- }
20
-
21
16
func (a * Ve [V ]) Done () V {
22
17
Done (a .E ) //只会检查错误,而不检查结果
23
18
return a .V //结果不做检查,允许返回零值,因为不是 comparable 的这里也没法比较是否为零值
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments