Skip to content

Commit

Permalink
✨ feat: add new pkg: syncs and internal pkg: checkfn
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 17, 2023
1 parent c553460 commit 0155b13
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 37 deletions.
6 changes: 3 additions & 3 deletions check.go
Expand Up @@ -3,8 +3,8 @@ package goutil
import (
"reflect"

"github.com/gookit/goutil/internal/checkfn"
"github.com/gookit/goutil/reflects"
"github.com/gookit/goutil/stdutil"
)

// IsNil value check
Expand Down Expand Up @@ -58,7 +58,7 @@ func IsEqual(src, dst any) bool {
// string - check sub-string exists
// array,slice - check sub-element exists
func Contains(data, elem any) bool {
_, found := stdutil.CheckContains(data, elem)
_, found := checkfn.Contains(data, elem)
return found
}

Expand All @@ -70,6 +70,6 @@ func Contains(data, elem any) bool {
// string - check sub-string exists
// array,slice - check sub-element exists
func IsContains(data, elem any) bool {
_, found := stdutil.CheckContains(data, elem)
_, found := checkfn.Contains(data, elem)
return found
}
6 changes: 3 additions & 3 deletions errorx/panics/assert.go
@@ -1,8 +1,8 @@
package panics

import (
"github.com/gookit/goutil/internal/checkfn"
"github.com/gookit/goutil/internal/comfunc"
"github.com/gookit/goutil/stdutil"
)

// IsTrue assert result is true, otherwise will panic
Expand Down Expand Up @@ -35,14 +35,14 @@ func NotNil(result any, fmtAndArgs ...any) {

// IsEmpty assert result is empty, otherwise will panic
func IsEmpty(result any, fmtAndArgs ...any) {
if !stdutil.IsEmpty(result) {
if !checkfn.IsEmpty(result) {
panicWithMsg("result should be empty", fmtAndArgs)
}
}

// NotEmpty assert result is empty, otherwise will panic
func NotEmpty(result any, fmtAndArgs ...any) {
if stdutil.IsEmpty(result) {
if checkfn.IsEmpty(result) {
panicWithMsg("result should not be empty", fmtAndArgs)
}
}
Expand Down
6 changes: 6 additions & 0 deletions internal/TODO.md
@@ -0,0 +1,6 @@
# TODO

## new package

- misc package 杂项
- syncs package
73 changes: 73 additions & 0 deletions internal/checkfn/check.go
@@ -0,0 +1,73 @@
package checkfn

import (
"reflect"
"strings"

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

// IsNil value check
func IsNil(v any) bool {
if v == nil {
return true
}
return reflects.IsNil(reflect.ValueOf(v))
}

// IsEmpty value check
func IsEmpty(v any) bool {
if v == nil {
return true
}
return reflects.IsEmpty(reflect.ValueOf(v))
}

// Contains try loop over the data check if the data includes the element.
//
// TIP: only support types: string, map, array, slice
//
// map - check key exists
// string - check sub-string exists
// array,slice - check sub-element exists
//
// return (false, false) if impossible.
// return (true, false) if element was not found.
// return (true, true) if element was found.
func Contains(data, elem any) (valid, found bool) {
dataRv := reflect.ValueOf(data)
dataRt := reflect.TypeOf(data)
if dataRt == nil {
return false, false
}

dataKind := dataRt.Kind()

// string
if dataKind == reflect.String {
return true, strings.Contains(dataRv.String(), reflect.ValueOf(elem).String())
}

// map
if dataKind == reflect.Map {
mapKeys := dataRv.MapKeys()
for i := 0; i < len(mapKeys); i++ {
if reflects.IsEqual(mapKeys[i].Interface(), elem) {
return true, true
}
}
return true, false
}

// array, slice - other return false
if dataKind != reflect.Slice && dataKind != reflect.Array {
return false, false
}

for i := 0; i < dataRv.Len(); i++ {
if reflects.IsEqual(dataRv.Index(i).Interface(), elem) {
return true, true
}
}
return true, false
}
3 changes: 3 additions & 0 deletions internal/comfunc/README.md
@@ -0,0 +1,3 @@
# common func for internal use

- don't depend on other external packages
35 changes: 35 additions & 0 deletions syncs/chan.go
@@ -0,0 +1,35 @@
package syncs

import (
"os"
"os/signal"
"syscall"
)

// WaitCloseSignals for some huang program.
//
// Usage:
//
// // do something. eg: start a http server
//
// syncs.WaitCloseSignals(func(sig os.Signal) {
// // do something
// })
func WaitCloseSignals(onClose func(sig os.Signal)) {
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)

// block until a signal is received.
onClose(<-signals)
}

// 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 {
ch := make(chan error)
go func() {
ch <- f()
}()

return <-ch
}
6 changes: 3 additions & 3 deletions stdutil/chan_test.go → syncs/chan_test.go
@@ -1,14 +1,14 @@
package stdutil_test
package syncs_test

import (
"testing"

"github.com/gookit/goutil/stdutil"
"github.com/gookit/goutil/syncs"
"github.com/gookit/goutil/testutil/assert"
)

func TestGo(t *testing.T) {
err := stdutil.Go(func() error {
err := syncs.Go(func() error {
return nil
})
assert.NoErr(t, err)
Expand Down
24 changes: 1 addition & 23 deletions stdutil/chan.go → syncs/signal.go
@@ -1,34 +1,12 @@
package stdutil
package syncs

import (
"context"
"fmt"
"io"
"os"
"os/signal"
"syscall"
)

// WaitCloseSignals for some huang program.
func WaitCloseSignals(closer io.Closer) error {
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
<-signals

return closer.Close()
}

// 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 {
ch := make(chan error)
go func() {
ch <- f()
}()

return <-ch
}

// SignalHandler returns an actor, i.e. an execute and interrupt func, that
// terminates with SignalError when the process receives one of the provided
// signals, or the parent context is canceled.
Expand Down
10 changes: 5 additions & 5 deletions testutil/assert/asserts.go
Expand Up @@ -9,15 +9,15 @@ import (

"github.com/gookit/color"
"github.com/gookit/goutil/arrutil"
"github.com/gookit/goutil/internal/checkfn"
"github.com/gookit/goutil/maputil"
"github.com/gookit/goutil/mathutil"
"github.com/gookit/goutil/reflects"
"github.com/gookit/goutil/stdutil"
)

// Nil asserts that the given is a nil value
func Nil(t TestingT, give any, fmtAndArgs ...any) bool {
if stdutil.IsNil(give) {
if checkfn.IsNil(give) {
return true
}

Expand All @@ -27,7 +27,7 @@ func Nil(t TestingT, give any, fmtAndArgs ...any) bool {

// NotNil asserts that the given is a not nil value
func NotNil(t TestingT, give any, fmtAndArgs ...any) bool {
if !stdutil.IsNil(give) {
if !checkfn.IsNil(give) {
return true
}

Expand Down Expand Up @@ -175,7 +175,7 @@ func PanicsErrMsg(t TestingT, fn PanicRunFunc, errMsg string, fmtAndArgs ...any)
// string - check sub-string exists
// array,slice - check sub-element exists
func Contains(t TestingT, src, elem any, fmtAndArgs ...any) bool {
valid, found := stdutil.CheckContains(src, elem)
valid, found := checkfn.Contains(src, elem)
if valid && found {
return true
}
Expand All @@ -199,7 +199,7 @@ func Contains(t TestingT, src, elem any, fmtAndArgs ...any) bool {
// string - check sub-string exists
// array,slice - check sub-element exists
func NotContains(t TestingT, src, elem any, fmtAndArgs ...any) bool {
valid, found := stdutil.CheckContains(src, elem)
valid, found := checkfn.Contains(src, elem)
if valid && !found {
return true
}
Expand Down

0 comments on commit 0155b13

Please sign in to comment.