Skip to content

Commit

Permalink
👔 update: inner - rename internal func comfunc.FormatTplWithArgs to F…
Browse files Browse the repository at this point in the history
…ormatWithArgs
  • Loading branch information
inhere committed Jul 19, 2023
1 parent 8aa12c0 commit 19943b5
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 51 deletions.
6 changes: 3 additions & 3 deletions errorx/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func IsIn[T comdef.ScalarType](value T, list []T, fmtAndArgs ...any) error {
if arrutil.NotIn(value, list) {
var errMsg string
if len(fmtAndArgs) > 0 {
errMsg = comfunc.FormatTplAndArgs(fmtAndArgs)
errMsg = comfunc.FormatWithArgs(fmtAndArgs)
} else {
errMsg = fmt.Sprintf("value should be in the %v", list)
}
Expand All @@ -44,7 +44,7 @@ func NotIn[T comdef.ScalarType](value T, list []T, fmtAndArgs ...any) error {
if arrutil.In(value, list) {
var errMsg string
if len(fmtAndArgs) > 0 {
errMsg = comfunc.FormatTplAndArgs(fmtAndArgs)
errMsg = comfunc.FormatWithArgs(fmtAndArgs)
} else {
errMsg = fmt.Sprintf("value should not be in the %v", list)
}
Expand All @@ -55,7 +55,7 @@ func NotIn[T comdef.ScalarType](value T, list []T, fmtAndArgs ...any) error {

func formatErrMsg(errMsg string, fmtAndArgs []any) string {
if len(fmtAndArgs) > 0 {
errMsg = comfunc.FormatTplAndArgs(fmtAndArgs)
errMsg = comfunc.FormatWithArgs(fmtAndArgs)
}
return errMsg
}
2 changes: 1 addition & 1 deletion errorx/panics/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func NotEmpty(result any, fmtAndArgs ...any) {

func panicWithMsg(errMsg string, fmtAndArgs []any) {
if len(fmtAndArgs) > 0 {
errMsg = comfunc.FormatTplAndArgs(fmtAndArgs)
errMsg = comfunc.FormatWithArgs(fmtAndArgs)
}
panic(errMsg)
}
23 changes: 0 additions & 23 deletions internal/comfunc/comfunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,29 +80,6 @@ func ParseEnvVar(val string, getFn func(string) string) (newVal string) {
})
}

// FormatTplAndArgs message
func FormatTplAndArgs(fmtAndArgs []any) string {
if len(fmtAndArgs) == 0 || fmtAndArgs == nil {
return ""
}

ln := len(fmtAndArgs)
first := fmtAndArgs[0]

if ln == 1 {
if msgAsStr, ok := first.(string); ok {
return msgAsStr
}
return fmt.Sprintf("%+v", first)
}

// is template string.
if tplStr, ok := first.(string); ok {
return fmt.Sprintf(tplStr, fmtAndArgs[1:]...)
}
return fmt.Sprint(fmtAndArgs...)
}

var (
// TIP: extend unit d,w
// time.ParseDuration() is not supported. eg: "1d", "2w"
Expand Down
23 changes: 23 additions & 0 deletions internal/comfunc/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,26 @@ func StrToBool(s string) (bool, error) {

return false, fmt.Errorf("'%s' cannot convert to bool", s)
}

// FormatWithArgs format message with args
func FormatWithArgs(fmtAndArgs []any) string {
ln := len(fmtAndArgs)
if ln == 0 {
return ""
}

first := fmtAndArgs[0]

if ln == 1 {
if msgAsStr, ok := first.(string); ok {
return msgAsStr
}
return fmt.Sprintf("%+v", first)
}

// is template string.
if tplStr, ok := first.(string); ok {
return fmt.Sprintf(tplStr, fmtAndArgs[1:]...)
}
return fmt.Sprint(fmtAndArgs...)
}
3 changes: 2 additions & 1 deletion testutil/assert/asserts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/gookit/color"
"github.com/gookit/goutil/arrutil"
"github.com/gookit/goutil/internal/checkfn"
"github.com/gookit/goutil/internal/comfunc"
"github.com/gookit/goutil/maputil"
"github.com/gookit/goutil/mathutil"
"github.com/gookit/goutil/reflects"
Expand Down Expand Up @@ -703,7 +704,7 @@ func fail(t TestingT, failMsg string, fmtAndArgs []any) bool {
}

// user custom message
if userMsg := formatTplAndArgs(fmtAndArgs...); len(userMsg) > 0 {
if userMsg := comfunc.FormatWithArgs(fmtAndArgs); len(userMsg) > 0 {
labeledTexts = append(labeledTexts, labeledText{"User Msg", userMsg})
}

Expand Down
24 changes: 1 addition & 23 deletions testutil/assert/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,35 +75,13 @@ func truncatingFormat(data any) string {
}

// Give us some space the type info too if needed.
max := bufio.MaxScanTokenSize - 100
max := bufio.MaxScanTokenSize - 1000
if len(value) > max {
value = value[0:max] + "<... truncated>"
}
return value
}

func formatTplAndArgs(fmtAndArgs ...any) string {
if len(fmtAndArgs) == 0 || fmtAndArgs == nil {
return ""
}

ln := len(fmtAndArgs)
first := fmtAndArgs[0]

if ln == 1 {
if msgAsStr, ok := first.(string); ok {
return msgAsStr
}
return fmt.Sprintf("%+v", first)
}

// is template string.
if tplStr, ok := first.(string); ok {
return fmt.Sprintf(tplStr, fmtAndArgs[1:]...)
}
return fmt.Sprint(fmtAndArgs...)
}

func callerInfos() []string {
num := 3
skip := 2
Expand Down

0 comments on commit 19943b5

Please sign in to comment.