Skip to content

Commit

Permalink
👔 update: update some goutil package import, replace pkg name
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 18, 2023
1 parent 9e581e1 commit 2040798
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 94 deletions.
7 changes: 7 additions & 0 deletions basefn/basefunc.go → basefn/basefn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions basefn/basefunc_test.go → basefn/basefn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions cflag/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down
15 changes: 8 additions & 7 deletions cflag/cflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]
}
Expand Down
75 changes: 6 additions & 69 deletions comdef/comdef.go
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
70 changes: 70 additions & 0 deletions comdef/interface.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion envutil/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Env Util

Provide some commonly ENV util functions.
Provide some commonly system or go ENV util functions.

## Install

Expand Down
7 changes: 0 additions & 7 deletions func.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
14 changes: 10 additions & 4 deletions goutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -45,14 +46,19 @@ 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:
//
// 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
Expand Down
8 changes: 4 additions & 4 deletions basefn/extfunc_test.go → mathutil/format_test.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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))
}
}

Expand All @@ -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))
}
}

0 comments on commit 2040798

Please sign in to comment.