From 204079858272355e2f931f3e52c4444ecf301669 Mon Sep 17 00:00:00 2001 From: Inhere Date: Tue, 18 Jul 2023 11:44:53 +0800 Subject: [PATCH] :necktie: update: update some goutil package import, replace pkg name --- basefn/{basefunc.go => basefn.go} | 7 ++ basefn/{basefunc_test.go => basefn_test.go} | 8 ++ cflag/app.go | 4 +- cflag/cflag.go | 15 ++-- comdef/comdef.go | 75 ++----------------- comdef/interface.go | 70 +++++++++++++++++ envutil/README.md | 2 +- func.go | 7 -- goutil.go | 14 +++- .../format_test.go | 8 +- 10 files changed, 116 insertions(+), 94 deletions(-) rename basefn/{basefunc.go => basefn.go} (94%) rename basefn/{basefunc_test.go => basefn_test.go} (92%) create mode 100644 comdef/interface.go rename basefn/extfunc_test.go => mathutil/format_test.go (77%) diff --git a/basefn/basefunc.go b/basefn/basefn.go similarity index 94% rename from basefn/basefunc.go rename to basefn/basefn.go index decc3187d..7b95737ed 100644 --- a/basefn/basefunc.go +++ b/basefn/basefn.go @@ -8,6 +8,13 @@ func Panicf(format string, v ...any) { panic(fmt.Sprintf(format, v...)) } +// PanicIf if error is not empty +func PanicIf(err error) { + if err != nil { + panic(err) + } +} + // MustOK if error is not empty, will panic func MustOK(err error) { if err != nil { diff --git a/basefn/basefunc_test.go b/basefn/basefn_test.go similarity index 92% rename from basefn/basefunc_test.go rename to basefn/basefn_test.go index 4b3ad0c48..11a3c6d14 100644 --- a/basefn/basefunc_test.go +++ b/basefn/basefn_test.go @@ -8,6 +8,14 @@ import ( "github.com/gookit/goutil/testutil/assert" ) +func TestPanicIf(t *testing.T) { + basefn.PanicIf(nil) + + assert.Panics(t, func() { + basefn.PanicIf(errors.New("a error")) + }) +} + func TestPanicf(t *testing.T) { basefn.MustOK(nil) diff --git a/cflag/app.go b/cflag/app.go index 7133b1e38..fdda68530 100644 --- a/cflag/app.go +++ b/cflag/app.go @@ -9,9 +9,9 @@ import ( "sort" "github.com/gookit/color" + "github.com/gookit/goutil" "github.com/gookit/goutil/cliutil" "github.com/gookit/goutil/mathutil" - "github.com/gookit/goutil/stdutil" "github.com/gookit/goutil/strutil" ) @@ -63,7 +63,7 @@ func (a *App) addCmd(c *Cmd) { } if _, ok := a.cmds[c.Name]; ok { - stdutil.Panicf("command name %s has been exists", c.Name) + goutil.Panicf("command name %s has been exists", c.Name) } a.names = append(a.names, c.Name) diff --git a/cflag/cflag.go b/cflag/cflag.go index 945580110..bde9ff07e 100644 --- a/cflag/cflag.go +++ b/cflag/cflag.go @@ -19,12 +19,13 @@ import ( "strings" "github.com/gookit/color" + "github.com/gookit/goutil" + "github.com/gookit/goutil/basefn" "github.com/gookit/goutil/cliutil" "github.com/gookit/goutil/envutil" "github.com/gookit/goutil/errorx" "github.com/gookit/goutil/mathutil" "github.com/gookit/goutil/stdio" - "github.com/gookit/goutil/stdutil" "github.com/gookit/goutil/structs" "github.com/gookit/goutil/strutil" ) @@ -140,7 +141,7 @@ func (c *CFlags) AddValidator(name string, fn OptCheckFn) { // ConfigOpt for a flag option func (c *CFlags) ConfigOpt(name string, fn func(opt *FlagOpt)) { if c.Lookup(name) == nil { - stdutil.Panicf("cflag: option '%s' is not registered", name) + goutil.Panicf("cflag: option '%s' is not registered", name) } // init on not exist @@ -163,7 +164,7 @@ func (c *CFlags) AddShortcuts(name string, shorts ...string) { func (c *CFlags) addShortcuts(name string, shorts []string) { for _, short := range shorts { if regName, ok := c.shortcuts[short]; ok { - stdutil.Panicf("cflag: shortcut '%s' has been used by option '%s'", short, regName) + goutil.Panicf("cflag: shortcut '%s' has been used by option '%s'", short, regName) } c.shortcuts[short] = name @@ -188,10 +189,10 @@ func (c *CFlags) BindArg(arg *FlagArg) { arg.Index = len(c.bindArgs) // check arg info - stdutil.PanicIf(arg.check()) + basefn.PanicIf(arg.check()) if _, ok := c.argNames[arg.Name]; ok { - stdutil.Panicf("cflag: arg '%s' have been registered", arg.Name) + basefn.Panicf("cflag: arg '%s' have been registered", arg.Name) } // register @@ -266,7 +267,7 @@ func (c *CFlags) prepare() error { // parse flag usage string c.VisitAll(func(f *flag.Flag) { if regName, ok := c.shortcuts[f.Name]; ok { - stdutil.Panicf("cflag: name '%s' has been as shortcut by '%s'", f.Name, regName) + goutil.Panicf("cflag: name '%s' has been as shortcut by '%s'", f.Name, regName) } f.Usage = c.parseFlagUsage(f.Name, f.Usage) @@ -388,7 +389,7 @@ func (c *CFlags) bindParsedArgs() error { func (c *CFlags) Arg(name string) *FlagArg { idx, ok := c.argNames[name] if !ok { - stdutil.Panicf("cflag: get not binding arg '%s'", name) + goutil.Panicf("cflag: get not binding arg '%s'", name) } return c.bindArgs[idx] } diff --git a/comdef/comdef.go b/comdef/comdef.go index 24a5275af..54a16687a 100644 --- a/comdef/comdef.go +++ b/comdef/comdef.go @@ -1,25 +1,6 @@ // Package comdef provide some common type or constant definitions package comdef -import ( - "fmt" - "io" -) - -// ByteStringWriter interface -type ByteStringWriter interface { - io.Writer - io.ByteWriter - io.StringWriter - fmt.Stringer -} - -// StringWriteStringer interface -type StringWriteStringer interface { - io.StringWriter - fmt.Stringer -} - type ( // MarshalFunc define MarshalFunc func(v any) ([]byte, error) @@ -28,58 +9,14 @@ type ( UnmarshalFunc func(bts []byte, ptr any) error ) -// Int64able interface -type Int64able interface { - Int64() (int64, error) -} - -// -// -// Matcher type -// -// - -// Matcher interface -type Matcher[T any] interface { - Match(s T) bool -} - -// MatchFunc definition. implements Matcher interface -type MatchFunc[T any] func(v T) bool - -// Match satisfies the Matcher interface -func (fn MatchFunc[T]) Match(v T) bool { - return fn(v) -} - -// StringMatcher interface -type StringMatcher interface { - Match(s string) bool -} - -// StringMatchFunc definition -type StringMatchFunc func(s string) bool - -// Match satisfies the StringMatcher interface -func (fn StringMatchFunc) Match(s string) bool { - return fn(s) -} - -// StringHandler interface -type StringHandler interface { - Handle(s string) string -} - -// StringHandleFunc definition -type StringHandleFunc func(s string) string - -// Handle satisfies the StringHandler interface -func (fn StringHandleFunc) Handle(s string) string { - return fn(s) -} - // IntCheckFunc check func type IntCheckFunc func(val int) error // StrCheckFunc check func type StrCheckFunc func(val string) error + +// ToStringFunc try to convert value to string, return error on fail +type ToStringFunc func(v any) (string, error) + +// SafeStringFunc safe convert value to string +type SafeStringFunc func(v any) string diff --git a/comdef/interface.go b/comdef/interface.go new file mode 100644 index 000000000..7718f4263 --- /dev/null +++ b/comdef/interface.go @@ -0,0 +1,70 @@ +package comdef + +import ( + "fmt" + "io" +) + +// ByteStringWriter interface +type ByteStringWriter interface { + io.Writer + io.ByteWriter + io.StringWriter + fmt.Stringer +} + +// StringWriteStringer interface +type StringWriteStringer interface { + io.StringWriter + fmt.Stringer +} + +// Int64able interface +type Int64able interface { + Int64() (int64, error) +} + +// +// +// Matcher type +// +// + +// Matcher interface +type Matcher[T any] interface { + Match(s T) bool +} + +// MatchFunc definition. implements Matcher interface +type MatchFunc[T any] func(v T) bool + +// Match satisfies the Matcher interface +func (fn MatchFunc[T]) Match(v T) bool { + return fn(v) +} + +// StringMatcher interface +type StringMatcher interface { + Match(s string) bool +} + +// StringMatchFunc definition +type StringMatchFunc func(s string) bool + +// Match satisfies the StringMatcher interface +func (fn StringMatchFunc) Match(s string) bool { + return fn(s) +} + +// StringHandler interface +type StringHandler interface { + Handle(s string) string +} + +// StringHandleFunc definition +type StringHandleFunc func(s string) string + +// Handle satisfies the StringHandler interface +func (fn StringHandleFunc) Handle(s string) string { + return fn(s) +} diff --git a/envutil/README.md b/envutil/README.md index 85d18c90b..48901adf1 100644 --- a/envutil/README.md +++ b/envutil/README.md @@ -1,6 +1,6 @@ # Env Util -Provide some commonly ENV util functions. +Provide some commonly system or go ENV util functions. ## Install diff --git a/func.go b/func.go index 0573f1d10..49764d32d 100644 --- a/func.go +++ b/func.go @@ -1,12 +1,5 @@ package goutil -import "github.com/gookit/goutil/stdutil" - -// FuncName get func name -func FuncName(f any) string { - return stdutil.FuncName(f) -} - // Go is a basic promise implementation: it wraps calls a function in a goroutine // and returns a channel which will later return the function's return value. func Go(f func() error) error { diff --git a/goutil.go b/goutil.go index 6298c9f74..2e915e51f 100644 --- a/goutil.go +++ b/goutil.go @@ -5,11 +5,12 @@ package goutil import ( "fmt" - "github.com/gookit/goutil/stdutil" + "github.com/gookit/goutil/goinfo" + "github.com/gookit/goutil/structs" ) -// Value alias of stdutil.Value -type Value = stdutil.Value +// Value alias of structs.Value +type Value = structs.Value // Panicf format panic message use fmt.Sprintf func Panicf(format string, v ...any) { @@ -45,6 +46,11 @@ func Must[T any](v T, err error) T { return v } +// FuncName get func name +func FuncName(f any) string { + return goinfo.FuncName(f) +} + // PkgName get current package name. alias of stdutil.PkgName() // // Usage: @@ -52,7 +58,7 @@ func Must[T any](v T, err error) T { // funcName := goutil.FuncName(fn) // pgkName := goutil.PkgName(funcName) func PkgName(funcName string) string { - return stdutil.PkgName(funcName) + return goinfo.PkgName(funcName) } // ErrOnFail return input error on cond is false, otherwise return nil diff --git a/basefn/extfunc_test.go b/mathutil/format_test.go similarity index 77% rename from basefn/extfunc_test.go rename to mathutil/format_test.go index 771e67862..0ade78ae3 100644 --- a/basefn/extfunc_test.go +++ b/mathutil/format_test.go @@ -1,9 +1,9 @@ -package basefn_test +package mathutil_test import ( "testing" - "github.com/gookit/goutil/basefn" + "github.com/gookit/goutil/mathutil" "github.com/gookit/goutil/testutil/assert" ) @@ -20,7 +20,7 @@ func TestDataSize(t *testing.T) { } for _, tt := range tests { - assert.Eq(t, tt.want, basefn.DataSize(tt.args)) + assert.Eq(t, tt.want, mathutil.DataSize(tt.args)) } } @@ -39,6 +39,6 @@ func TestHowLongAgo(t *testing.T) { } for _, tt := range tests { - assert.Eq(t, tt.want, basefn.HowLongAgo(tt.args)) + assert.Eq(t, tt.want, mathutil.HowLongAgo(tt.args)) } }