Skip to content

Commit

Permalink
👔 up: update some type name, up some comments and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 1, 2023
1 parent 4c2531f commit 8fea977
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 46 deletions.
15 changes: 9 additions & 6 deletions errorx/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,14 @@ func (e *errorR) GoString() string {
return e.String()
}

// ErrMap multi error map
type ErrMap map[string]error
// ErrorM multi error map
type ErrorM map[string]error

// ErrMap alias of ErrorM
type ErrMap = ErrorM

// Error string
func (e ErrMap) Error() string {
func (e ErrorM) Error() string {
var sb strings.Builder
for name, err := range e {
sb.WriteString(name)
Expand All @@ -93,20 +96,20 @@ func (e ErrMap) Error() string {
}

// ErrorOrNil error
func (e ErrMap) ErrorOrNil() error {
func (e ErrorM) ErrorOrNil() error {
if len(e) == 0 {
return nil
}
return e
}

// IsEmpty error
func (e ErrMap) IsEmpty() bool {
func (e ErrorM) IsEmpty() bool {
return len(e) == 0
}

// One error
func (e ErrMap) One() error {
func (e ErrorM) One() error {
for _, err := range e {
return err
}
Expand Down
28 changes: 23 additions & 5 deletions goinfo/gofunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import (
"reflect"
"runtime"
"strings"
"unicode"

"github.com/gookit/goutil/strutil"
)

// FullFcName struct.
type FullFcName struct {
// FullName eg: github.com/gookit/goutil/goinfo.PanicIf
// FullName eg: "github.com/gookit/goutil/goinfo.PanicIf"
FullName string
pkgPath string
pkgName string
funcName string
pkgPath string // "github.com/gookit/goutil/goinfo"
pkgName string // "goinfo"
funcName string // "PanicIf"
}

// Parse the full func name.
Expand All @@ -28,7 +29,6 @@ func (ffn *FullFcName) Parse() {
ffn.pkgPath = ffn.FullName[:i+1]
// spilt get pkg and func name
ffn.pkgName, ffn.funcName = strutil.MustCut(ffn.FullName[i+1:], ".")

ffn.pkgPath += ffn.pkgName
}

Expand Down Expand Up @@ -93,3 +93,21 @@ func PkgName(fullFcName string) string {
}
return fullFcName
}

// GoodFuncName reports whether the function name is a valid identifier.
func GoodFuncName(name string) bool {
if name == "" {
return false
}

for i, r := range name {
switch {
case r == '_':
case i == 0 && !unicode.IsLetter(r):
return false
case !unicode.IsLetter(r) && !unicode.IsDigit(r):
return false
}
}
return true
}
5 changes: 5 additions & 0 deletions goinfo/gofunc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (
func TestFuncName(t *testing.T) {
name := goinfo.FuncName(goinfo.PkgName)
assert.Eq(t, "github.com/gookit/goutil/goinfo.PkgName", name)

assert.True(t, goinfo.GoodFuncName("MyFunc"))
assert.False(t, goinfo.GoodFuncName(""))
assert.False(t, goinfo.GoodFuncName("+MyFunc"))
assert.False(t, goinfo.GoodFuncName("My+Func"))
}

func TestPkgName(t *testing.T) {
Expand Down
33 changes: 4 additions & 29 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,21 @@ import (
"context"

"github.com/gookit/goutil/structs"
"golang.org/x/sync/errgroup"
"github.com/gookit/goutil/syncs"
)

// ErrGroup is a collection of goroutines working on subtasks that
// are part of the same overall task.
//
// Refers:
//
// https://github.com/neilotoole/errgroup
// https://github.com/fatih/semgroup
type ErrGroup struct {
*errgroup.Group
}
type ErrGroup = syncs.ErrGroup

// NewCtxErrGroup instance
func NewCtxErrGroup(ctx context.Context, limit ...int) (*ErrGroup, context.Context) {
egg, ctx1 := errgroup.WithContext(ctx)
if len(limit) > 0 && limit[0] > 0 {
egg.SetLimit(limit[0])
}

eg := &ErrGroup{Group: egg}
return eg, ctx1
return syncs.NewCtxErrGroup(ctx, limit...)
}

// NewErrGroup instance
func NewErrGroup(limit ...int) *ErrGroup {
eg := &ErrGroup{Group: new(errgroup.Group)}

if len(limit) > 0 && limit[0] > 0 {
eg.SetLimit(limit[0])
}
return eg
}

// Add one or more handler at once
func (g *ErrGroup) Add(handlers ...func() error) {
for _, handler := range handlers {
g.Go(handler)
}
return syncs.NewErrGroup(limit...)
}

// RunFn func
Expand Down
9 changes: 4 additions & 5 deletions mathutil/convert.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package mathutil

import (
"encoding/json"
"fmt"
"math"
"strconv"
Expand Down Expand Up @@ -132,7 +131,7 @@ func ToIntWithFunc(in any, usrFn ToIntFunc) (iVal int, err error) {
}
case string:
iVal, err = strconv.Atoi(strings.TrimSpace(tVal))
case json.Number:
case interface{ Int64() (int64, error) }: // eg: json.Number
var i64 int64
if i64, err = tVal.Int64(); err == nil {
if i64 > math.MaxInt32 {
Expand Down Expand Up @@ -248,7 +247,7 @@ func ToUintWithFunc(in any, usrFn ToUintFunc) (u64 uint64, err error) {
u64 = uint64(tVal)
case time.Duration:
u64 = uint64(tVal)
case json.Number:
case interface{ Int64() (int64, error) }: // eg: json.Number
var i64 int64
i64, err = tVal.Int64()
u64 = uint64(i64)
Expand Down Expand Up @@ -348,7 +347,7 @@ func ToInt64WithFunc(in any, usrFn ToInt64Func) (i64 int64, err error) {
i64 = int64(tVal)
case time.Duration:
i64 = int64(tVal)
case json.Number:
case interface{ Int64() (int64, error) }: // eg: json.Number
i64, err = tVal.Int64()
default:
if usrFn != nil {
Expand Down Expand Up @@ -449,7 +448,7 @@ func ToFloatWithFunc(in any, usrFn ToFloatFunc) (f64 float64, err error) {
f64 = tVal
case time.Duration:
f64 = float64(tVal)
case json.Number:
case interface{ Float64() (float64, error) }: // eg: json.Number
f64, err = tVal.Float64()
default:
if usrFn != nil {
Expand Down
2 changes: 1 addition & 1 deletion strutil/textscan/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,6 @@ func (t *StringToken) ScanMore(_ *TextScanner) error {
}

// MergeSame implements
func (t *StringToken) MergeSame(tok Token) error {
func (t *StringToken) MergeSame(_ Token) error {
return errorx.Raw("cannot merge any token to Invalid token")
}

0 comments on commit 8fea977

Please sign in to comment.