From c3f5086cbae508d94bd20b0c19a4253aabd15821 Mon Sep 17 00:00:00 2001 From: Inhere Date: Fri, 9 Dec 2022 10:30:59 +0800 Subject: [PATCH] chore: update some readme and re-generate readme docs --- README.md | 37 ++++++++++++++++++++++++--------- README.zh-CN.md | 37 +++++++++++++++++++++++++-------- byteutil/README.md | 46 +++++++++++++++++++++++++++++++++++++++++ goutil.go | 8 +++++++ internal/gendoc/main.go | 9 ++++---- 5 files changed, 114 insertions(+), 23 deletions(-) create mode 100644 byteutil/README.md diff --git a/README.md b/README.md index 565f799ef..ad285e5ae 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ u64Val = goutil.Uint("2") // 2 ## Packages -### Array/Slice +### Array and Slice > Package `github.com/gookit/goutil/arrutil` @@ -153,6 +153,21 @@ ss, err := arrutil.ToStrings([]int{1, 2}) // ss: []string{"1", "2"} ``` +### Bytes Utils + +> Package `github.com/gookit/goutil/byteutil` + +```go +// source at byteutil/buffer.go +func NewBuffer() *Buffer +// source at byteutil/bytex.go +func Md5(src any) []byte +// source at byteutil/encoder.go +func NewStdEncoder(encFn func(src []byte) []byte, decFn func(src []byte) ([]byte, error)) *StdEncoder +// source at byteutil/pool.go +func NewChanPool(maxSize int, width int, capWidth int) *ChanPool +``` + ### Cflag > Package `github.com/gookit/goutil/cflag` @@ -549,7 +564,7 @@ runtime.goexit() ``` -### Formatting +### Format Utils > Package `github.com/gookit/goutil/fmtutil` @@ -906,7 +921,7 @@ func NewValue(val any) *Value ```go // source at strutil/bytes.go func NewBuffer() *Buffer -func NewByteChanPool(maxSize int, width int, capWidth int) *ByteChanPool +func NewByteChanPool(maxSize, width, capWidth int) *ByteChanPool // source at strutil/check.go func NoCaseEq(s, t string) bool func IsNumChar(c byte) bool @@ -974,15 +989,18 @@ func ToOSArgs(s string) []string func MustToTime(s string, layouts ...string) time.Time func ToTime(s string, layouts ...string) (t time.Time, err error) func ToDuration(s string) (time.Duration, error) +// source at strutil/crypto.go +func Md5(src any) string +func MD5(src any) string { return Md5(src) } +func GenMd5(src any) string { return Md5(src) } +func Md5Bytes(src any) []byte +func HashPasswd(pwd, key string) string +func VerifyPasswd(pwdMAC, pwd, key string) bool // source at strutil/encode.go func EscapeJS(s string) string func EscapeHTML(s string) string func AddSlashes(s string) string func StripSlashes(s string) string -func Md5(src any) string -func MD5(src any) string { return Md5(src) } -func GenMd5(src any) string { return Md5(src) } -func Md5Bytes(src any) []byte func URLEncode(s string) string func URLDecode(s string) string func B32Encode(str string) string @@ -1081,7 +1099,7 @@ func RenderText(input string, data any, fns template.FuncMap, isFile ...bool) st func WrapTag(s, tag string) string ``` -### System +### System Utils > Package `github.com/gookit/goutil/sysutil` @@ -1142,7 +1160,7 @@ func ChangeUserByName(newUname string) (err error) func ChangeUserUidGid(newUid int, newGid int) (err error) ``` -### Testing +### Testing Utils > Package `github.com/gookit/goutil/testutil` @@ -1383,4 +1401,3 @@ root@xx:/go/work# go test ./... ## License [MIT](LICENSE) - diff --git a/README.zh-CN.md b/README.zh-CN.md index 829ffe39c..f4e7a724f 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -44,6 +44,7 @@ Go一些常用的string、number、slice、map、struct、env、system等工具 - [`testutil`](testutil) test help 相关操作的函数工具包. eg: http test, mock ENV value - [assert](testutil/assert) 用于帮助测试的断言函数工具包 - [`timex`](timex) 提供增强的 time.Time 实现。添加更多常用的功能方法 + - 提供类似 `Y-m-d H:i:s` 的日期时间格式解析处理 - 例如: DayStart(), DayAfter(), DayAgo(), DateFormat() 等等 ## GoDoc @@ -80,7 +81,7 @@ u64Val = goutil.Uint("2") // 2 ## Packages -### Array/Slice +### Array and Slice > Package `github.com/gookit/goutil/arrutil` @@ -151,6 +152,21 @@ ss, err := arrutil.ToStrings([]int{1, 2}) // ss: []string{"1", "2"} ``` +### Bytes Utils + +> Package `github.com/gookit/goutil/byteutil` + +```go +// source at byteutil/buffer.go +func NewBuffer() *Buffer +// source at byteutil/bytex.go +func Md5(src any) []byte +// source at byteutil/encoder.go +func NewStdEncoder(encFn func(src []byte) []byte, decFn func(src []byte) ([]byte, error)) *StdEncoder +// source at byteutil/pool.go +func NewChanPool(maxSize int, width int, capWidth int) *ChanPool +``` + ### Cflag > Package `github.com/gookit/goutil/cflag` @@ -549,7 +565,7 @@ runtime.goexit() ``` -### Formatting +### Format Utils > Package `github.com/gookit/goutil/fmtutil` @@ -906,7 +922,7 @@ func NewValue(val any) *Value ```go // source at strutil/bytes.go func NewBuffer() *Buffer -func NewByteChanPool(maxSize int, width int, capWidth int) *ByteChanPool +func NewByteChanPool(maxSize, width, capWidth int) *ByteChanPool // source at strutil/check.go func NoCaseEq(s, t string) bool func IsNumChar(c byte) bool @@ -974,15 +990,18 @@ func ToOSArgs(s string) []string func MustToTime(s string, layouts ...string) time.Time func ToTime(s string, layouts ...string) (t time.Time, err error) func ToDuration(s string) (time.Duration, error) +// source at strutil/crypto.go +func Md5(src any) string +func MD5(src any) string { return Md5(src) } +func GenMd5(src any) string { return Md5(src) } +func Md5Bytes(src any) []byte +func HashPasswd(pwd, key string) string +func VerifyPasswd(pwdMAC, pwd, key string) bool // source at strutil/encode.go func EscapeJS(s string) string func EscapeHTML(s string) string func AddSlashes(s string) string func StripSlashes(s string) string -func Md5(src any) string -func MD5(src any) string { return Md5(src) } -func GenMd5(src any) string { return Md5(src) } -func Md5Bytes(src any) []byte func URLEncode(s string) string func URLDecode(s string) string func B32Encode(str string) string @@ -1081,7 +1100,7 @@ func RenderText(input string, data any, fns template.FuncMap, isFile ...bool) st func WrapTag(s, tag string) string ``` -### System +### System Utils > Package `github.com/gookit/goutil/sysutil` @@ -1142,7 +1161,7 @@ func ChangeUserByName(newUname string) (err error) func ChangeUserUidGid(newUid int, newGid int) (err error) ``` -### Testing +### Testing Utils > Package `github.com/gookit/goutil/testutil` diff --git a/byteutil/README.md b/byteutil/README.md new file mode 100644 index 000000000..dfd758132 --- /dev/null +++ b/byteutil/README.md @@ -0,0 +1,46 @@ +# Bytes Util + +Provide some commonly bytes util functions. + +## Install + +```shell +go get github.com/gookit/goutil/byteutil +``` + +## Go docs + +- [Go docs](https://pkg.go.dev/github.com/gookit/goutil/byteutil) + +## Functions API + +```go +var HexEncoder = NewStdEncoder(func(src []byte) []byte { ... }, func(src []byte) ([]byte, error) { ... }) ... +func Md5(src any) []byte +type Buffer struct{ ... } +func NewBuffer() *Buffer +type BytesEncoder interface{ ... } +type ChanPool struct{ ... } +func NewChanPool(maxSize int, width int, capWidth int) *ChanPool +type StdEncoder struct{ ... } +func NewStdEncoder(encFn func(src []byte) []byte, decFn func(src []byte) ([]byte, error)) *StdEncoder +``` + +## Code Check & Testing + +```bash +gofmt -w -l ./ +golint ./... +``` + +**Testing**: + +```shell +go test -v ./byteutil/... +``` + +**Test limit by regexp**: + +```shell +go test -v -run ^TestSetByKeys ./byteutil/... +``` diff --git a/goutil.go b/goutil.go index 97a1fe90e..89d0507e5 100644 --- a/goutil.go +++ b/goutil.go @@ -61,3 +61,11 @@ func FuncName(f any) string { func PkgName(funcName string) string { return stdutil.PkgName(funcName) } + +// ErrOnFail return input error on cond is false, otherwise return nil +func ErrOnFail(cond bool, err error) error { + if !cond { + return err + } + return nil +} diff --git a/internal/gendoc/main.go b/internal/gendoc/main.go index 6da18dbd1..b80b0fa64 100644 --- a/internal/gendoc/main.go +++ b/internal/gendoc/main.go @@ -29,13 +29,14 @@ var ( "internal", } nameMap = map[string]string{ - "arr": "array/Slice", + "arr": "array and Slice", "str": "strings", - "sys": "system", + "byte": "Bytes Utils", + "sys": "system Utils", "math": "math/Number", "fs": "file System", - "fmt": "formatting", - "test": "testing", + "fmt": "format Utils", + "test": "testing Utils", "dump": "dumper", "structs": "structs", "json": "JSON Utils",