-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: add new pkg: syncs and internal pkg: checkfn
- Loading branch information
Showing
9 changed files
with
132 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# TODO | ||
|
||
## new package | ||
|
||
- misc package 杂项 | ||
- syncs package |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# common func for internal use | ||
|
||
- don't depend on other external packages |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters