Skip to content

Commit 4c78e9a

Browse files
author
chenjie
committed
update
1 parent 2bed942 commit 4c78e9a

28 files changed

+1367
-1502
lines changed

bufpool/bufpool.go

+439-1,182
Large diffs are not rendered by default.

bufpool/bufpool_test.go

+49-14
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,57 @@ import (
77

88
cerror "github.com/chenjie199234/Corelib/error"
99
ctime "github.com/chenjie199234/Corelib/util/time"
10-
//"time"
1110
)
1211

13-
type A struct {
14-
E1 *error
15-
E2 error
16-
E3 error
17-
E4 *cerror.Error
18-
E5 error
19-
T1 ctime.Time
20-
}
21-
2212
func Test_Bufpool(t *testing.T) {
2313
b := GetBuffer()
24-
e := errors.New("e")
25-
ee := cerror.Error{}
26-
b.Append(&A{E1: &e, E2: e, E3: ee, E4: &ee, E5: nil, T1: ctime.Time(time.Now())})
27-
t.Log(b.String())
14+
var s = time.Second
15+
b.AppendStdDuration(s)
16+
if b.String() != "1000000000" {
17+
panic("std duration error")
18+
}
19+
b.Reset()
20+
b.AppendStdDurations([]time.Duration{s})
21+
if b.String() != "[1000000000]" {
22+
panic("std durations error")
23+
}
24+
b.Reset()
25+
b.AppendStdDurationPointers([]*time.Duration{nil, &s})
26+
if b.String() != "[null,1000000000]" {
27+
panic("std durationpointers error")
28+
}
29+
b.Reset()
30+
var cs ctime.Duration = ctime.Duration(s)
31+
b.AppendDuration(cs)
32+
if b.String() != "1s" {
33+
panic("corelib duration error")
34+
}
35+
b.Reset()
36+
b.AppendDurations([]ctime.Duration{cs})
37+
if b.String() != "[\"1s\"]" {
38+
panic("corelib durations error")
39+
}
40+
b.Reset()
41+
b.AppendDurationPointers([]*ctime.Duration{nil, &cs})
42+
if b.String() != "[null,\"1s\"]" {
43+
panic("corelib durationpointers error")
44+
}
45+
b.Reset()
46+
stde := errors.New("stde")
47+
ce := cerror.MakeError(1, "ce")
48+
b.AppendError(stde)
49+
if b.String() != "stde" {
50+
panic("std error error")
51+
}
52+
b.Reset()
53+
b.AppendError(ce)
54+
if b.String() != "{\"code\":1,\"msg\":\"ce\"}" {
55+
panic("corelib error error")
56+
}
57+
b.Reset()
58+
b.AppendErrors([]error{nil, stde, ce})
59+
if b.String() != "[null,\"stde\",{\"code\":1,\"msg\":\"ce\"}]" {
60+
panic("corelib errors error")
61+
}
62+
b.Reset()
2863
}

codegen/tml/gomod/template_gomod.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ const text = `module {{.}}
1111
go 1.16
1212
1313
require (
14-
github.com/chenjie199234/Config v0.0.14
15-
github.com/chenjie199234/Corelib v0.0.28
14+
github.com/chenjie199234/Config v0.0.15
15+
github.com/chenjie199234/Corelib v0.0.29
1616
github.com/fsnotify/fsnotify v1.5.1
1717
github.com/go-sql-driver/mysql v1.6.0
1818
github.com/segmentio/kafka-go v0.4.18

error/const.go

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package error
22

3+
import "context"
4+
35
var (
46
ErrUnknown = &Error{Code: 10000, Msg: "unknown"}
57
ErrReq = &Error{Code: 10001, Msg: "request error"}
@@ -10,3 +12,9 @@ var (
1012
ErrBan = &Error{Code: 10006, Msg: "ban"}
1113
ErrNotExist = &Error{Code: 10007, Msg: "not exist"}
1214
)
15+
16+
//convert std error
17+
var (
18+
ErrDeadlineExceeded = &Error{Code: -1, Msg: context.DeadlineExceeded.Error()}
19+
ErrCanceled = &Error{Code: -1, Msg: context.Canceled.Error()}
20+
)

error/error.go

+37-55
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,92 @@
11
package error
22

33
import (
4+
"context"
45
"encoding/json"
6+
"strconv"
57

68
"github.com/chenjie199234/Corelib/util/common"
79
)
810

911
//if error was not in this error's format,code will return -1,msg will use the origin error.Error()
1012

11-
type Error struct {
12-
Code int32 `json:"code"`
13-
Msg string `json:"msg"`
14-
}
15-
1613
func MakeError(code int32, msg string) *Error {
1714
return &Error{Code: code, Msg: msg}
1815
}
1916
func GetCodeFromErrorstr(e string) int32 {
20-
if e == "" {
17+
ee := ConvertErrorstr(e)
18+
if ee == nil {
2119
return 0
2220
}
23-
tempe := &Error{}
24-
if ee := json.Unmarshal(common.Str2byte(e), tempe); ee != nil {
25-
return -1
26-
}
27-
return tempe.Code
21+
return ee.Code
2822
}
2923
func GetCodeFromStdError(e error) int32 {
30-
if e == nil {
24+
ee := ConvertStdError(e)
25+
if ee == nil {
3126
return 0
3227
}
33-
tempe, ok := e.(*Error)
34-
if ok {
35-
return tempe.Code
36-
}
37-
tempe = &Error{}
38-
if ee := json.Unmarshal(common.Str2byte(e.Error()), tempe); ee != nil {
39-
return -1
40-
}
41-
return tempe.Code
28+
return ee.Code
4229
}
4330
func GetMsgFromErrorstr(e string) string {
44-
if e == "" {
31+
ee := ConvertErrorstr(e)
32+
if ee == nil {
4533
return ""
4634
}
47-
tempe := &Error{}
48-
if ee := json.Unmarshal(common.Str2byte(e), tempe); ee != nil {
49-
return e
50-
}
51-
return tempe.Msg
35+
return ee.Msg
5236
}
5337
func GetMsgFromStdError(e error) string {
54-
if e == nil {
38+
ee := ConvertStdError(e)
39+
if ee == nil {
5540
return ""
5641
}
57-
tempe, ok := e.(*Error)
58-
if ok {
59-
return tempe.Msg
60-
}
61-
tempe = &Error{}
62-
if ee := json.Unmarshal(common.Str2byte(e.Error()), tempe); ee != nil {
63-
return e.Error()
64-
}
65-
return tempe.Msg
42+
return ee.Msg
6643
}
67-
func ErrorstrToError(e string) *Error {
44+
func ConvertErrorstr(e string) *Error {
6845
if e == "" {
6946
return nil
7047
}
7148
result := &Error{}
72-
if ee := json.Unmarshal(common.Str2byte(e), result); ee != nil {
49+
if e[0] == '{' {
50+
//json format
51+
if ee := json.Unmarshal(common.Str2byte(e), result); ee != nil {
52+
result.Code = -1
53+
result.Msg = e
54+
}
55+
} else {
56+
//text format
7357
result.Code = -1
7458
result.Msg = e
7559
}
7660
return result
7761
}
78-
func StdErrorToError(e error) *Error {
62+
func ConvertStdError(e error) *Error {
7963
if e == nil {
8064
return nil
8165
}
66+
if e == context.DeadlineExceeded {
67+
return ErrDeadlineExceeded
68+
} else if e == context.Canceled {
69+
return ErrCanceled
70+
}
8271
result, ok := e.(*Error)
8372
if ok {
8473
return result
8574
}
86-
result = &Error{}
87-
if ee := json.Unmarshal(common.Str2byte(e.Error()), result); ee != nil {
88-
result.Code = -1
89-
result.Msg = e.Error()
90-
}
91-
return result
75+
return ConvertErrorstr(e.Error())
9276
}
9377
func Equal(a, b error) bool {
94-
aa := StdErrorToError(a)
95-
bb := StdErrorToError(b)
78+
aa := ConvertStdError(a)
79+
bb := ConvertStdError(b)
9680
if aa == nil && bb == nil {
9781
return true
9882
} else if (aa == nil && bb != nil) || (aa != nil && bb == nil) {
9983
return false
10084
}
10185
return aa.Code == bb.Code && aa.Msg == bb.Msg
10286
}
103-
func (this Error) Error() string {
104-
d, _ := json.Marshal(this)
105-
return common.Byte2str(d)
106-
}
107-
func (this Error) String() string {
108-
d, _ := json.Marshal(this)
109-
return common.Byte2str(d)
87+
func (this *Error) Error() string {
88+
if this == nil {
89+
return ""
90+
}
91+
return "{\"code\":" + strconv.FormatInt(int64(this.Code), 10) + ",\"msg\":\"" + this.Msg + "\"}"
11092
}

0 commit comments

Comments
 (0)