Skip to content

Commit 38d1a64

Browse files
author
yangyile
committed
添加map+err的返回值检查逻辑
1 parent cda6595 commit 38d1a64

6 files changed

+149
-17
lines changed

check_vae_test.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ package done
33
import "testing"
44

55
func TestVAE_Length(t *testing.T) {
6-
VAE(newExample1x([]int{1, 2, 3, 4, 5})).Length(5)
6+
run := func() ([]int, error) {
7+
return []int{1, 2, 3, 4, 5}, nil
8+
}
9+
10+
VAE(run()).Length(5)
711
}
812

913
func TestVAE_Len(t *testing.T) {
10-
VAE(newExample1x([]string{"a", "b", "c"})).Len(3)
11-
}
14+
run := func() ([]string, error) {
15+
return []string{"a", "b", "c"}, nil
16+
}
1217

13-
func newExample1x[T any](a T) (T, error) {
14-
return a, nil
18+
VAE(run()).Len(3)
1519
}

check_vbe_test.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@ package done
33
import "testing"
44

55
func TestVBE_OK(t *testing.T) {
6-
VBE(newExample1x(true)).OK()
6+
run := func() (bool, error) {
7+
return true, nil
8+
}
9+
10+
VBE(run()).OK()
711
}
812

913
func TestVBE_NO(t *testing.T) {
10-
VBE(newExample1x(false)).NO()
14+
run := func() (bool, error) {
15+
return false, nil
16+
}
17+
18+
VBE(run()).NO()
1119
}

check_vme.go

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package done
2+
3+
import "github.com/pkg/errors"
4+
5+
type Vme[K comparable, V any] struct {
6+
V map[K]V
7+
E error
8+
*Ve[map[K]V]
9+
}
10+
11+
// VME accept two params. one is map and one is error interface
12+
// So M means Map type.
13+
func VME[K comparable, V any](val map[K]V, err error) *Vme[K, V] {
14+
return &Vme[K, V]{
15+
V: val,
16+
E: err,
17+
Ve: VE[map[K]V](val, err),
18+
}
19+
}
20+
21+
func (a *Vme[K, V]) Sure() map[K]V {
22+
return a.sure1x1nice1x1some(a.Done())
23+
}
24+
25+
func (a *Vme[K, V]) Nice() map[K]V {
26+
return a.sure1x1nice1x1some(a.Done())
27+
}
28+
29+
func (a *Vme[K, V]) Some() map[K]V {
30+
return a.sure1x1nice1x1some(a.Done())
31+
}
32+
33+
func (a *Vme[K, V]) Good() {
34+
a.good1x1fine1x1safe1x1have(a.Done())
35+
}
36+
37+
func (a *Vme[K, V]) Fine() {
38+
a.good1x1fine1x1safe1x1have(a.Done())
39+
}
40+
41+
func (a *Vme[K, V]) Safe() {
42+
a.good1x1fine1x1safe1x1have(a.Done())
43+
}
44+
45+
func (a *Vme[K, V]) Have() {
46+
a.good1x1fine1x1safe1x1have(a.Done())
47+
}
48+
49+
func (a *Vme[K, V]) Zero() {
50+
a.none1x1zero1x1void(a.Done())
51+
}
52+
53+
func (a *Vme[K, V]) None() {
54+
a.none1x1zero1x1void(a.Done())
55+
}
56+
57+
func (a *Vme[K, V]) Void() {
58+
a.none1x1zero1x1void(a.Done())
59+
}
60+
61+
func (a *Vme[K, V]) Size(n int) {
62+
a.size1x1len1x1length(a.Done(), n)
63+
}
64+
65+
func (a *Vme[K, V]) Len(n int) {
66+
a.size1x1len1x1length(a.Done(), n)
67+
}
68+
69+
func (a *Vme[K, V]) Length(n int) {
70+
a.size1x1len1x1length(a.Done(), n)
71+
}
72+
73+
func (a *Vme[K, V]) sure1x1nice1x1some(v map[K]V) map[K]V {
74+
if len(v) == 0 {
75+
panic(errors.New("SHOULD HAVE SOME BUT IS NONE"))
76+
}
77+
return v
78+
}
79+
80+
func (a *Vme[K, V]) good1x1fine1x1safe1x1have(v map[K]V) {
81+
if len(v) == 0 {
82+
panic(errors.New("SHOULD HAVE SOME BUT IS NONE"))
83+
}
84+
}
85+
86+
func (a *Vme[K, V]) none1x1zero1x1void(v map[K]V) {
87+
if len(v) != 0 {
88+
panic(errors.New("SHOULD BE NONE BUT HAVE SOME"))
89+
}
90+
}
91+
92+
func (a *Vme[K, V]) size1x1len1x1length(v map[K]V, n int) {
93+
if len(v) != n {
94+
panic(errors.New("SIZE IS NOT SAME"))
95+
}
96+
}

check_vme_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package done
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestVme_Nice(t *testing.T) {
8+
run := func() (map[int64]string, error) {
9+
return map[int64]string{
10+
1: "a",
11+
}, nil
12+
}
13+
14+
res := VME(run()).Nice()
15+
t.Log(res)
16+
}
17+
18+
func TestVme_Size(t *testing.T) {
19+
run := func() (map[string]float64, error) {
20+
return map[string]float64{
21+
"a": 1.0,
22+
"b": 2.0,
23+
"c": 3.0,
24+
}, nil
25+
}
26+
27+
VME(run()).Size(3)
28+
}

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ go 1.22.6
55
require (
66
github.com/pkg/errors v0.9.1
77
github.com/stretchr/testify v1.9.0
8-
github.com/yyle88/zaplog v0.0.11
8+
github.com/yyle88/zaplog v0.0.13
99
go.uber.org/zap v1.27.0
1010
)
1111

1212
require (
1313
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
1414
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
15-
github.com/yyle88/mutexmap v1.0.5 // indirect
15+
github.com/yyle88/mutexmap v1.0.6 // indirect
1616
go.uber.org/multierr v1.11.0 // indirect
1717
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
1818
gopkg.in/yaml.v3 v3.0.1 // indirect

go.sum

+4-8
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,10 @@ github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU
1212
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
1313
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
1414
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
15-
github.com/yyle88/mutexmap v1.0.4 h1:Y4VfNE28HBYwpw/UaZyTUZAjYcob3qPMubQWxgdYl9s=
16-
github.com/yyle88/mutexmap v1.0.4/go.mod h1:lAEDkpXe9iSU0/8L/SaXAQ6TUa+4A9nnl9OQ56zHEQY=
17-
github.com/yyle88/mutexmap v1.0.5 h1:sJS5NRY5NbFgVTUGuL4wMZv5dgq1oiDBbGm7f/AdZu4=
18-
github.com/yyle88/mutexmap v1.0.5/go.mod h1:lAEDkpXe9iSU0/8L/SaXAQ6TUa+4A9nnl9OQ56zHEQY=
19-
github.com/yyle88/zaplog v0.0.10 h1:4jzeWB7h7IQ126hcE5mRMjVo99k8+xpLjQTtbie8JZ8=
20-
github.com/yyle88/zaplog v0.0.10/go.mod h1:dS72cgivcfuXM6ExW6+f/YiPVGOzQ7spQL5jECrEdcU=
21-
github.com/yyle88/zaplog v0.0.11 h1:g7IfF03vfJCgJrlr3qMeCbeRi3nxneA3n5W8lYSjZes=
22-
github.com/yyle88/zaplog v0.0.11/go.mod h1:RbgWRlved+hKCcpFd7UV4MTq9NdrqIIYumngKExIxkk=
15+
github.com/yyle88/mutexmap v1.0.6 h1:wIkEZmENn/+kgVt4uYNElMXUzyKKE784DSfVwnAUwaw=
16+
github.com/yyle88/mutexmap v1.0.6/go.mod h1:lAEDkpXe9iSU0/8L/SaXAQ6TUa+4A9nnl9OQ56zHEQY=
17+
github.com/yyle88/zaplog v0.0.13 h1:GI1ThMfiDsoMZ1MiBL87Bu4bhKQoj5fkdPhG+q1N8uY=
18+
github.com/yyle88/zaplog v0.0.13/go.mod h1:AlCwptNgHCLrKVgCzp64FDqPQn/xZux3yGVIYjxqr2A=
2319
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
2420
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
2521
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=

0 commit comments

Comments
 (0)