Skip to content

Commit 54bd373

Browse files
committed
fix: barry quick fix, 2025-09-12 23:58:36
1 parent 7c0a437 commit 54bd373

File tree

15 files changed

+225
-141
lines changed

15 files changed

+225
-141
lines changed

assert/assert.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,25 @@ package assert
22

33
import (
44
"fmt"
5-
)
65

7-
var EnablePrintStack bool
6+
"github.com/google/go-cmp/cmp"
7+
"github.com/k0kubun/pp/v3"
8+
)
89

910
func Assert(b bool, format string, a ...interface{}) {
1011
if b {
1112
must(fmt.Errorf(format, a...))
1213
}
1314
}
1415

16+
func MustEqual[T any](a, b T) {
17+
if !cmp.Equal(a, b) {
18+
pp.Println("a: ", a)
19+
pp.Println("b: ", b)
20+
must(fmt.Errorf("a,b not equal"))
21+
}
22+
}
23+
1524
func If(b bool, format string, a ...interface{}) {
1625
if b {
1726
must(fmt.Errorf(format, a...))

assert/assert_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ import (
77
"github.com/pubgo/funk/recovery"
88
)
99

10-
func init() {
11-
assert.EnablePrintStack = true
12-
}
13-
1410
func TestCheckNil(t *testing.T) {
1511
var a *int
1612

assert/util.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package assert
33
import (
44
"fmt"
55
"log/slog"
6+
"reflect"
67
"runtime/debug"
78

8-
"github.com/pubgo/funk/stack"
9+
"github.com/k0kubun/pp/v3"
910
)
1011

1112
func messageFromMsgAndArgs(msgAndArgs ...any) string {
@@ -17,10 +18,9 @@ func messageFromMsgAndArgs(msgAndArgs ...any) string {
1718
if msgAsStr, ok := msgAndArgs[0].(string); ok {
1819
return msgAsStr
1920
}
20-
return fmt.Sprintf("%+v", msgAndArgs[0])
2121
}
2222

23-
return fmt.Sprintf(msgAndArgs[0].(string), msgAndArgs[1:]...)
23+
return pp.Sprint(msgAndArgs...)
2424
}
2525

2626
func must(err error, messageArgs ...any) {
@@ -32,15 +32,12 @@ func must(err error, messageArgs ...any) {
3232
if message == "" {
3333
message = err.Error()
3434
} else {
35-
message = fmt.Sprintf("msg:%s err:%s", message, err.Error())
35+
message = fmt.Sprintf("msg:%v err:%s", message, err.Error())
3636
}
3737

38-
if EnablePrintStack {
39-
slog.Error(message)
40-
debug.PrintStack()
41-
}
42-
43-
panic(message)
38+
slog.Error(message)
39+
debug.PrintStack()
40+
panic(err)
4441
}
4542

4643
func try(fn func() error) (gErr error) {
@@ -51,7 +48,7 @@ func try(fn func() error) (gErr error) {
5148

5249
defer func() {
5350
if gErr != nil {
54-
gErr = fmt.Errorf("stack:%s, err:%w", stack.CallerWithFunc(fn).String(), gErr)
51+
gErr = fmt.Errorf("stack:%s, err:%w", reflect.TypeOf(fn).String(), gErr)
5552
}
5653
}()
5754

config/config.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ var (
3636
)
3737

3838
func init() {
39-
vars.RegisterValue("config", map[string]any{
40-
"config_type": defaultConfigType,
41-
"config_name": defaultConfigName,
42-
"config_path": configPath,
43-
"config_dir": configDir,
39+
vars.Register("config", func() any {
40+
return map[string]any{
41+
"config_type": defaultConfigType,
42+
"config_name": defaultConfigName,
43+
"config_path": configPath,
44+
"config_dir": configDir,
45+
}
4446
})
4547
}
4648

errors/errinter/color_name.go

Lines changed: 0 additions & 36 deletions
This file was deleted.

errors/errinter/colorfield.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package errinter
2+
3+
import (
4+
"strings"
5+
6+
"github.com/pubgo/funk/internal/color"
7+
)
8+
9+
var width = 11
10+
11+
func getColorErrField(name string) string {
12+
if width-len(name) < 0 {
13+
return name
14+
}
15+
return strings.Repeat(" ", width-len(name)) + name
16+
}
17+
18+
var (
19+
ColorKind = color.Green.Str(getColorErrField("kind"))
20+
ColorMsg = color.Green.Str(getColorErrField("msg"))
21+
ColorService = color.Green.Str(getColorErrField("service"))
22+
ColorOperation = color.Green.Str(getColorErrField("operation"))
23+
ColorId = color.Green.Str(getColorErrField("id"))
24+
ColorDetail = color.Green.Str(getColorErrField("detail"))
25+
ColorTags = color.Green.Str(getColorErrField("tags"))
26+
ColorErrMsg = color.Red.Str(getColorErrField("err_msg"))
27+
ColorErrDetail = color.Red.Str(getColorErrField("err_detail"))
28+
ColorCaller = color.Green.Str(getColorErrField("caller"))
29+
ColorCode = color.Green.Str(getColorErrField("code"))
30+
ColorMessage = color.Green.Str(getColorErrField("message"))
31+
ColorBiz = color.Green.Str(getColorErrField("biz_code"))
32+
ColorStatusCode = color.Green.Str(getColorErrField("status_code"))
33+
ColorName = color.Green.Str(getColorErrField("name"))
34+
ColorStack = color.Green.Str(getColorErrField("stack"))
35+
ColorVersion = color.Green.Str(getColorErrField("version"))
36+
)

errors/errinter/utils.go

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
"errors"
66
"fmt"
77
"os"
8-
"strings"
98
"sync"
109

1110
"github.com/k0kubun/pp/v3"
11+
"github.com/rs/xid"
1212
"github.com/rs/zerolog/log"
1313
"github.com/samber/lo"
1414
"google.golang.org/protobuf/encoding/prototext"
@@ -18,7 +18,6 @@ import (
1818

1919
"github.com/pubgo/funk"
2020
"github.com/pubgo/funk/log/logutil"
21-
"github.com/pubgo/funk/pretty"
2221
"github.com/pubgo/funk/proto/errorpb"
2322
)
2423

@@ -36,38 +35,19 @@ func ParseError(val interface{}) error {
3635
return errors.New(v)
3736
case []byte:
3837
return errors.New(string(v))
38+
case proto.Message:
39+
return errors.New(prototext.Format(v))
3940
default:
40-
return errors.New(SimplePrint(v))
41+
return errors.New(errPretty().Sprint(v))
4142
}
4243
}
4344

44-
var Simple = sync.OnceValue(func() *pp.PrettyPrinter {
45-
printer := pp.New()
46-
printer.SetColoringEnabled(false)
47-
printer.SetExportedOnly(false)
48-
printer.SetOmitEmpty(true)
49-
printer.SetMaxDepth(3)
50-
return printer
51-
})
52-
53-
func SimplePrint(v interface{}) string {
54-
return strings.ReplaceAll(Simple().Sprint(v), "\n", "")
55-
}
56-
5745
func GetErrorId(err error) string {
58-
if err == nil {
59-
return ""
60-
}
61-
62-
for err != nil {
63-
if v, ok := err.(Error); ok {
64-
return v.ID()
65-
}
66-
67-
err = Unwrap(err)
46+
if v, ok := lo.ErrorsAs[Error](err); ok && v != nil {
47+
return v.ID()
6848
}
6949

70-
return ""
50+
return xid.New().String()
7151
}
7252

7353
func Unwrap(err error) error {
@@ -103,8 +83,7 @@ func Debug(err error) {
10383
return
10484
}
10585

106-
pretty.SetDefaultMaxDepth(20)
107-
pretty.Println(err)
86+
Console().Println(err)
10887
}
10988

11089
func MustTagsToAny(tags ...*errorpb.Tag) []*anypb.Any {
@@ -171,3 +150,21 @@ func MustProtoToAny(p proto.Message) *anypb.Any {
171150

172151
return pb
173152
}
153+
154+
var errPretty = sync.OnceValue(func() *pp.PrettyPrinter {
155+
printer := pp.New()
156+
printer.SetColoringEnabled(false)
157+
printer.SetExportedOnly(false)
158+
printer.SetOmitEmpty(true)
159+
printer.SetMaxDepth(3)
160+
return printer
161+
})
162+
163+
var Console = sync.OnceValue(func() *pp.PrettyPrinter {
164+
printer := pp.New()
165+
printer.SetColoringEnabled(true)
166+
printer.SetExportedOnly(false)
167+
printer.SetOmitEmpty(true)
168+
printer.SetMaxDepth(5)
169+
return printer
170+
})

errors/errinter/z_util_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package errinter_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
"google.golang.org/protobuf/encoding/prototext"
11+
12+
"github.com/pubgo/funk/errors"
13+
"github.com/pubgo/funk/errors/errinter"
14+
"github.com/pubgo/funk/proto/errorpb"
15+
)
16+
17+
func TestGetErrorId(t *testing.T) {
18+
err := errors.New("test error id")
19+
pbTxt := prototext.Format(errinter.ParseErrToPb(err))
20+
errId := errinter.GetErrorId(err)
21+
assert.NotEmpty(t, errId)
22+
assert.Contains(t, pbTxt, errId)
23+
}
24+
25+
func TestParseError(t *testing.T) {
26+
assert.Equal(t, errinter.ParseError(nil), nil)
27+
assert.Equal(t, errinter.ParseError("hello").Error(), "hello")
28+
assert.Equal(t, errinter.ParseError(fmt.Errorf("err test")).Error(), "err test")
29+
30+
errMsg := errinter.ParseError(prototext.Format(errinter.MustProtoToAny(&errorpb.ErrMsg{Msg: "test msg error"}))).Error()
31+
assert.Equal(t,
32+
strings.ReplaceAll(errMsg, " ", " "),
33+
"[type.googleapis.com/errors.ErrMsg]: {\n msg: \"test msg error\"\n}\n",
34+
)
35+
36+
ctx := context.WithValue(context.Background(), "hello", "world")
37+
assert.Equal(t,
38+
errinter.ParseError(ctx).Error(),
39+
"&context.valueCtx{\n Context: context.backgroundCtx{},\n key: \"hello\",\n val: \"world\",\n}",
40+
)
41+
}

errors/errors.go

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,26 +73,7 @@ func UnwrapEach(err error, call func(e error) bool) {
7373

7474
func AsA[T any](err error) (*T, bool) {
7575
var target T
76-
val := reflect.ValueOf(&target)
77-
typ := val.Type()
78-
if typ.Kind() != reflect.Ptr || val.IsNil() {
79-
panic("errors: target must be a non-nil pointer")
80-
}
81-
82-
targetType := typ.Elem()
83-
for err != nil {
84-
if reflect.TypeOf(err).AssignableTo(targetType) {
85-
val.Elem().Set(reflect.ValueOf(err))
86-
return &target, true
87-
}
88-
89-
if x, ok := err.(ErrAs); ok && x.As(&target) {
90-
return &target, true
91-
}
92-
93-
err = Unwrap(err)
94-
}
95-
return &target, false
76+
return &target, As(err, &target)
9677
}
9778

9879
func As(err error, target any) bool {

funk.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,6 @@ func IsNil(err interface{}) bool {
165165
}
166166

167167
v := reflect.ValueOf(err)
168-
if !v.IsValid() {
169-
return true
170-
}
171-
172168
switch v.Kind() {
173169
case reflect.Chan, reflect.Func, reflect.Map, reflect.Pointer, reflect.UnsafePointer, reflect.Slice, reflect.Interface:
174170
return v.IsNil()

0 commit comments

Comments
 (0)