Skip to content

Commit f14bb21

Browse files
committed
👔 up: optimize common ToStringWith func logic
1 parent 9174185 commit f14bb21

File tree

2 files changed

+23
-40
lines changed

2 files changed

+23
-40
lines changed

internal/comfunc/convert.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,26 @@ func StrToBool(s string) (bool, error) {
4343
}
4444

4545
// FormatWithArgs format message with args
46+
//
47+
// - only one element, format to string
48+
// - first is format: fmt.Sprintf(firstElem, fmtAndArgs[1:]...)
49+
// - all is args: return fmt.Sprint(fmtAndArgs...)
4650
func FormatWithArgs(fmtAndArgs []any) string {
4751
ln := len(fmtAndArgs)
4852
if ln == 0 {
4953
return ""
5054
}
5155

5256
first := fmtAndArgs[0]
53-
5457
if ln == 1 {
55-
if msgAsStr, ok := first.(string); ok {
56-
return msgAsStr
58+
if str, ok := first.(string); ok {
59+
return str
5760
}
5861
return fmt.Sprintf("%+v", first)
5962
}
6063

6164
// is template string.
62-
if tplStr, ok := first.(string); ok {
65+
if tplStr, ok := first.(string); ok && strings.IndexByte(tplStr, '%') >= 0 {
6366
return fmt.Sprintf(tplStr, fmtAndArgs[1:]...)
6467
}
6568
return fmt.Sprint(fmtAndArgs...)
@@ -86,11 +89,6 @@ var StrBySprintFn = func(v any) (string, error) {
8689
return fmt.Sprint(v), nil
8790
}
8891

89-
// WithHandlePtr set ConvOption.HandlePtr option
90-
func WithHandlePtr(opt *ConvOption) {
91-
opt.HandlePtr = true
92-
}
93-
9492
// WithUserConvFn set ConvOption.UserConvFn option
9593
func WithUserConvFn(fn comdef.ToStringFunc) ConvOptionFn {
9694
return func(opt *ConvOption) {
@@ -116,11 +114,6 @@ func (opt *ConvOption) WithOption(optFns ...ConvOptionFn) {
116114

117115
// ToStringWith try to convert value to string. can with some option func, more see ConvOption.
118116
func ToStringWith(in any, optFns ...ConvOptionFn) (str string, err error) {
119-
opt := NewConvOption(optFns...)
120-
if !opt.NilAsFail && in == nil {
121-
return "", nil
122-
}
123-
124117
switch value := in.(type) {
125118
case int:
126119
str = strconv.Itoa(value)
@@ -161,6 +154,18 @@ func ToStringWith(in any, optFns ...ConvOptionFn) (str string, err error) {
161154
case error:
162155
str = value.Error()
163156
default:
157+
if len(optFns) == 0 && in == nil {
158+
return "", nil
159+
}
160+
161+
opt := NewConvOption(optFns...)
162+
if in == nil {
163+
if opt.NilAsFail {
164+
err = comdef.ErrConvType
165+
}
166+
return
167+
}
168+
164169
if opt.HandlePtr {
165170
if rv := reflect.ValueOf(in); rv.Kind() == reflect.Pointer {
166171
rv = rv.Elem()

x/basefn/basefn.go

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,18 @@ package basefn
44
import (
55
"errors"
66
"fmt"
7+
8+
"github.com/gookit/goutil/internal/comfunc"
79
)
810

911
// Panicf format panic message use fmt.Sprintf
10-
func Panicf(format string, v ...any) {
11-
panic(fmt.Sprintf(format, v...))
12-
}
12+
func Panicf(format string, v ...any) { panic(fmt.Sprintf(format, v...)) }
1313

1414
// PanicIf if cond = true, panics with an error message
1515
func PanicIf(cond bool, fmtAndArgs ...any) {
1616
if cond {
17-
panic(errors.New(formatWithArgs(fmtAndArgs)))
18-
}
19-
}
20-
21-
func formatWithArgs(fmtAndArgs []any) string {
22-
ln := len(fmtAndArgs)
23-
if ln == 0 {
24-
return ""
25-
}
26-
27-
first := fmtAndArgs[0]
28-
29-
if ln == 1 {
30-
if msgAsStr, ok := first.(string); ok {
31-
return msgAsStr
32-
}
33-
return fmt.Sprintf("%+v", first)
34-
}
35-
36-
// is template string.
37-
if tplStr, ok := first.(string); ok {
38-
return fmt.Sprintf(tplStr, fmtAndArgs[1:]...)
17+
panic(errors.New(comfunc.FormatWithArgs(fmtAndArgs)))
3918
}
40-
return fmt.Sprint(fmtAndArgs...)
4119
}
4220

4321
// PanicErr panics if error is not empty

0 commit comments

Comments
 (0)