From a90d655aec2aa7b8e44d3e6878291fec1658adbd Mon Sep 17 00:00:00 2001 From: Inhere Date: Thu, 17 Nov 2022 17:15:22 +0800 Subject: [PATCH] up: migrate interface{} to go1.18 any keywords --- arrutil/convert.go | 8 ++++---- cflag/cflag.go | 2 +- cflag/cflag_test.go | 2 +- cflag/optarg.go | 2 +- check.go | 12 ++++++------ comdef/comdef.go | 2 +- comdef/formatter.go | 2 +- conv.go | 18 ++++++++--------- dump/_examples/demo.go | 11 ++++++----- dump/_examples/demo1.go | 5 +++-- dump/_examples/demo_cyclic_ref.go | 13 +++++++------ dump/_examples/map.go | 7 ++++--- dump/_examples/refer_kr_pretty.go | 9 +++++---- dump/_examples/slice.go | 5 +++-- dump/_examples/struct.go | 9 +++++---- dump/dump.go | 16 ++++++++-------- dump/dump_test.go | 30 ++++++++++++++--------------- dump/dumper.go | 12 ++++++------ dump/dumper_test.go | 20 +++++++++---------- fsutil/finder/finder.go | 4 ++-- goutil.go | 4 ++-- maputil/convert.go | 2 +- reflects/util.go | 2 +- reflects/util_test.go | 10 +++++----- stdio/ioutil.go | 2 +- stdutil/stdutil.go | 2 +- structs/data.go | 18 ++++++++--------- strutil/strutil_test.go | 2 +- sysutil/clipboard/clipboard.go | 4 ++-- testutil/assert/util.go | 32 ++++++++++++++++++++----------- timex/timex.go | 4 ++++ 31 files changed, 146 insertions(+), 125 deletions(-) diff --git a/arrutil/convert.go b/arrutil/convert.go index b1844ab8b..6276ab1bb 100644 --- a/arrutil/convert.go +++ b/arrutil/convert.go @@ -40,15 +40,15 @@ func StringsToInts(ss []string) (ints []int, err error) { return } -// MustToStrings convert interface{}(allow: array,slice) to []string +// MustToStrings convert array or slice to []string func MustToStrings(arr any) []string { ret, _ := ToStrings(arr) return ret } -// StringsToSlice convert []string to []interface{} -func StringsToSlice(ss []string) []interface{} { - args := make([]interface{}, len(ss)) +// StringsToSlice convert []string to []any +func StringsToSlice(ss []string) []any { + args := make([]any, len(ss)) for i, s := range ss { args[i] = s } diff --git a/cflag/cflag.go b/cflag/cflag.go index 8150993f8..88ebaf961 100644 --- a/cflag/cflag.go +++ b/cflag/cflag.go @@ -171,7 +171,7 @@ func (c *CFlags) addShortcuts(name string, shorts []string) { } // AddArg binding for command -func (c *CFlags) AddArg(name, desc string, required bool, value interface{}) { +func (c *CFlags) AddArg(name, desc string, required bool, value any) { arg := &FlagArg{ Name: name, Desc: desc, diff --git a/cflag/cflag_test.go b/cflag/cflag_test.go index 1b6507d7c..40dbe4df6 100644 --- a/cflag/cflag_test.go +++ b/cflag/cflag_test.go @@ -70,7 +70,7 @@ func TestNew(t *testing.T) { c.IntVar(&opts.int, "int", 0, "this is a int option;true;i") c.StringVar(&opts.str, "str", "", "this is a string option;;s") c.StringVar(&opts.str1, "str1", "def-val", "this is a string option with default;;s1") - c.AddValidator("int", func(val interface{}) error { + c.AddValidator("int", func(val any) error { iv := val.(int) if iv < 10 { return errorx.Raw("value should >= 10") diff --git a/cflag/optarg.go b/cflag/optarg.go index d1f133cd1..9602e5083 100644 --- a/cflag/optarg.go +++ b/cflag/optarg.go @@ -7,7 +7,7 @@ import ( ) // OptCheckFn define -type OptCheckFn func(val interface{}) error +type OptCheckFn func(val any) error // FlagOpt struct type FlagOpt struct { diff --git a/check.go b/check.go index c0f6b8fc2..a0b0a0fc3 100644 --- a/check.go +++ b/check.go @@ -8,7 +8,7 @@ import ( ) // IsNil value check -func IsNil(v interface{}) bool { +func IsNil(v any) bool { if v == nil { return true } @@ -16,7 +16,7 @@ func IsNil(v interface{}) bool { } // IsEmpty value check -func IsEmpty(v interface{}) bool { +func IsEmpty(v any) bool { if v == nil { return true } @@ -24,7 +24,7 @@ func IsEmpty(v interface{}) bool { } // IsFunc value -func IsFunc(val interface{}) bool { +func IsFunc(val any) bool { if val == nil { return false } @@ -34,7 +34,7 @@ func IsFunc(val interface{}) bool { // IsEqual determines if two objects are considered equal. // // TIP: cannot compare function type -func IsEqual(src, dst interface{}) bool { +func IsEqual(src, dst any) bool { if src == nil || dst == nil { return src == dst } @@ -54,7 +54,7 @@ func IsEqual(src, dst interface{}) bool { // map - check key exists // string - check sub-string exists // array,slice - check sub-element exists -func Contains(data, elem interface{}) bool { +func Contains(data, elem any) bool { _, found := stdutil.CheckContains(data, elem) return found } @@ -66,7 +66,7 @@ func Contains(data, elem interface{}) bool { // map - check key exists // string - check sub-string exists // array,slice - check sub-element exists -func IsContains(data, elem interface{}) bool { +func IsContains(data, elem any) bool { _, found := stdutil.CheckContains(data, elem) return found } diff --git a/comdef/comdef.go b/comdef/comdef.go index ee8ae6b87..db183d25e 100644 --- a/comdef/comdef.go +++ b/comdef/comdef.go @@ -22,7 +22,7 @@ type StringWriteStringer interface { type ( // MarshalFunc define - MarshalFunc func(v interface{}) ([]byte, error) + MarshalFunc func(v any) ([]byte, error) // UnmarshalFunc define UnmarshalFunc func(bts []byte, ptr interface{}) error diff --git a/comdef/formatter.go b/comdef/formatter.go index 8d5640022..7bd713ad8 100644 --- a/comdef/formatter.go +++ b/comdef/formatter.go @@ -19,7 +19,7 @@ type BaseFormatter struct { // Out formatted to the writer Out io.Writer // Src data(array, map, struct) for format - Src interface{} + Src any // MaxDepth limit depth for array, map data TODO MaxDepth int // Prefix string for each element diff --git a/conv.go b/conv.go index 3892f0ca1..2f72a3f85 100644 --- a/conv.go +++ b/conv.go @@ -11,51 +11,51 @@ import ( ) // Bool convert value to bool -func Bool(v interface{}) bool { +func Bool(v any) bool { bl, _ := comfunc.ToBool(v) return bl } // ToBool try to convert type to bool -func ToBool(v interface{}) (bool, error) { +func ToBool(v any) (bool, error) { return comfunc.ToBool(v) } // String always convert value to string, will ignore error -func String(v interface{}) string { +func String(v any) string { s, _ := strutil.AnyToString(v, false) return s } // ToString convert value to string, will return error on fail. -func ToString(v interface{}) (string, error) { +func ToString(v any) (string, error) { return strutil.AnyToString(v, true) } // Int convert value to int -func Int(v interface{}) int { +func Int(v any) int { iv, _ := mathutil.ToInt(v) return iv } // ToInt try to convert value to int -func ToInt(v interface{}) (int, error) { +func ToInt(v any) (int, error) { return mathutil.ToInt(v) } // Int64 convert value to int64 -func Int64(v interface{}) int64 { +func Int64(v any) int64 { iv, _ := mathutil.ToInt64(v) return iv } // ToInt64 try to convert value to int64 -func ToInt64(v interface{}) (int64, error) { +func ToInt64(v any) (int64, error) { return mathutil.ToInt64(v) } // Uint convert value to uint64 -func Uint(v interface{}) uint64 { +func Uint(v any) uint64 { iv, _ := mathutil.ToUint(v) return iv } diff --git a/dump/_examples/demo.go b/dump/_examples/demo.go index dba902021..bb79fb9ce 100644 --- a/dump/_examples/demo.go +++ b/dump/_examples/demo.go @@ -7,20 +7,21 @@ import ( ) // rum demo: -// go run ./dump/_examples/demo.go +// +// go run ./dump/_examples/demo.go func main() { - val := map[string]interface{}{ + val := map[string]any{ "bool": true, "number": 1 + 1i, "bytes": []byte{97, 98, 99}, "lines": "multiline string\nline two", - "slice": []interface{}{1, 2}, + "slice": []any{1, 2}, "time": time.Now(), "struct": struct{ test int32 }{ test: 13, }, } - val["slice"].([]interface{})[1] = val["slice"] + val["slice"].([]any)[1] = val["slice"] dump.P(val) return // dump.Config.ShowFile = true @@ -33,6 +34,6 @@ func otherFunc() { dump.P("abc", "def") dump.P([]string{"ab", "cd"}) dump.P( - []interface{}{"ab", 234, []int{1, 3}}, + []any{"ab", 234, []int{1, 3}}, ) } diff --git a/dump/_examples/demo1.go b/dump/_examples/demo1.go index 5986bb6d4..041d5ac8d 100644 --- a/dump/_examples/demo1.go +++ b/dump/_examples/demo1.go @@ -3,7 +3,8 @@ package main import "github.com/gookit/goutil/dump" // rum demo: -// go run ./dump/_examples/demo1.go +// +// go run ./dump/_examples/demo1.go func main() { otherFunc1() } @@ -13,7 +14,7 @@ func otherFunc1() { 23, []string{"ab", "cd"}, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, - map[string]interface{}{ + map[string]any{ "key": "val", "sub": map[string]string{"k": "v"}, }, struct { diff --git a/dump/_examples/demo_cyclic_ref.go b/dump/_examples/demo_cyclic_ref.go index da8ed4071..ad8a3f83d 100644 --- a/dump/_examples/demo_cyclic_ref.go +++ b/dump/_examples/demo_cyclic_ref.go @@ -7,10 +7,11 @@ import ( ) // rum demo: -// go run ./dump/_examples/demo_cyclic_ref.go +// +// go run ./dump/_examples/demo_cyclic_ref.go func main() { - a := map[string]interface{}{} - a["circular"] = map[string]interface{}{ + a := map[string]any{} + a["circular"] = map[string]any{ "a": a, } @@ -18,17 +19,17 @@ func main() { // fmt.Println(a) dump.V(a) - val := map[string]interface{}{ + val := map[string]any{ "bool": true, "number": 1 + 1i, "bytes": []byte{97, 98, 99}, "lines": "first line\nsecond line", - "slice": []interface{}{1, 2}, + "slice": []any{1, 2}, "time": time.Now(), "struct": struct{ test int32 }{ test: 13, }, } - val["slice"].([]interface{})[1] = val["slice"] + val["slice"].([]any)[1] = val["slice"] dump.P(val) } diff --git a/dump/_examples/map.go b/dump/_examples/map.go index fea263acc..07aaad4e9 100644 --- a/dump/_examples/map.go +++ b/dump/_examples/map.go @@ -3,11 +3,12 @@ package main import "github.com/gookit/goutil/dump" // rum demo: -// go run ./map.go -// go run ./dump/_examples/map.go +// +// go run ./map.go +// go run ./dump/_examples/map.go func main() { dump.P( - map[string]interface{}{ + map[string]any{ "key0": 123, "key1": "value1", "key2": []int{1, 2, 3}, diff --git a/dump/_examples/refer_kr_pretty.go b/dump/_examples/refer_kr_pretty.go index 1fe4cee62..ac821cbe8 100644 --- a/dump/_examples/refer_kr_pretty.go +++ b/dump/_examples/refer_kr_pretty.go @@ -6,14 +6,15 @@ import ( ) // rum demo: -// go run ./refer_kr_pretty.go -// go run ./dump/_examples/refer_kr_pretty.go +// +// go run ./refer_kr_pretty.go +// go run ./dump/_examples/refer_kr_pretty.go func main() { - vs := []interface{}{ + vs := []any{ 23, []string{"ab", "cd"}, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, // len > 10 - map[string]interface{}{ + map[string]any{ "key": "val", "sub": map[string]string{"k": "v"}, }, struct { diff --git a/dump/_examples/slice.go b/dump/_examples/slice.go index 6a92ce2f2..870f144e5 100644 --- a/dump/_examples/slice.go +++ b/dump/_examples/slice.go @@ -3,13 +3,14 @@ package main import "github.com/gookit/goutil/dump" // rum demo: -// go run ./dump/_examples/slice.go +// +// go run ./dump/_examples/slice.go func main() { dump.P( []byte("abc"), []int{1, 2, 3}, []string{"ab", "cd"}, - []interface{}{ + []any{ "ab", 234, []int{1, 3}, diff --git a/dump/_examples/struct.go b/dump/_examples/struct.go index fbe3bfff9..744ddaf72 100644 --- a/dump/_examples/struct.go +++ b/dump/_examples/struct.go @@ -8,13 +8,14 @@ import ( ) // rum demo: -// go run ./struct.go -// go run ./dump/_examples/struct.go +// +// go run ./struct.go +// go run ./dump/_examples/struct.go func main() { s1 := &struct { - cannotExport map[string]interface{} + cannotExport map[string]any }{ - cannotExport: map[string]interface{}{ + cannotExport: map[string]any{ "key1": 12, "key2": "abcd123", }, diff --git a/dump/dump.go b/dump/dump.go index d484e0cb1..07b3d8bd0 100644 --- a/dump/dump.go +++ b/dump/dump.go @@ -77,32 +77,32 @@ func Config(fn func(opts *Options)) { } // V like fmt.Println, but the output is clearer and more beautiful -func V(vs ...interface{}) { +func V(vs ...any) { std.Dump(vs...) } // P like fmt.Println, but the output is clearer and more beautiful -func P(vs ...interface{}) { +func P(vs ...any) { std.Print(vs...) } // Print like fmt.Println, but the output is clearer and more beautiful -func Print(vs ...interface{}) { +func Print(vs ...any) { std.Print(vs...) } // Println like fmt.Println, but the output is clearer and more beautiful -func Println(vs ...interface{}) { +func Println(vs ...any) { std.Println(vs...) } // Fprint like fmt.Println, but the output is clearer and more beautiful -func Fprint(w io.Writer, vs ...interface{}) { +func Fprint(w io.Writer, vs ...any) { std.Fprint(w, vs...) } // Format like fmt.Println, but the output is clearer and more beautiful -func Format(vs ...interface{}) string { +func Format(vs ...any) string { w := &bytes.Buffer{} std2.Fprint(w, vs...) @@ -110,11 +110,11 @@ func Format(vs ...interface{}) string { } // NoLoc dump vars data, without location. -func NoLoc(vs ...interface{}) { +func NoLoc(vs ...any) { std2.Println(vs...) } // Clear dump clear data, without location. -func Clear(vs ...interface{}) { +func Clear(vs ...any) { std2.Println(vs...) } diff --git a/dump/dump_test.go b/dump/dump_test.go index 360bb29f0..fd16903cb 100644 --- a/dump/dump_test.go +++ b/dump/dump_test.go @@ -21,7 +21,7 @@ func ExamplePrint() { []string{"ab", "cd"}, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, map[string]string{"key": "val"}, - map[string]interface{}{ + map[string]any{ "sub": map[string]string{"k": "v"}, }, struct { @@ -146,7 +146,7 @@ func TestPrint(t *testing.T) { `, buf.String()) buf.Reset() - Fprint(buf, map[string]interface{}{ + Fprint(buf, map[string]any{ "key": "val", "sub": map[string]string{"k": "v"}, }) @@ -177,7 +177,7 @@ func TestPrintNil(t *testing.T) { is.Eq("int(0),\n", buf.String()) buf.Reset() - var f interface{} + var f any V(f) is.Eq(",\n", buf.String()) buf.Reset() @@ -211,9 +211,9 @@ func TestStruct_CannotExportField(t *testing.T) { func TestStruct_InterfaceField(t *testing.T) { myS1 := struct { - // cannotExport interface{} // ok + // cannotExport any // ok cannotExport st1 // ok - // CanExport interface{} ok + // CanExport any ok CanExport st1 // ok }{ cannotExport: s1, @@ -227,9 +227,9 @@ func TestStruct_InterfaceField(t *testing.T) { func TestStruct_MapInterfacedValue(t *testing.T) { myS2 := &struct { - cannotExport map[string]interface{} + cannotExport map[string]any }{ - cannotExport: map[string]interface{}{ + cannotExport: map[string]any{ "key1": 12, "key2": "abcd123", }, @@ -243,9 +243,9 @@ func TestStruct_MapInterfacedValue(t *testing.T) { type st2 struct { st1 Github string - Face1 interface{} - face2 interface{} - faces map[string]interface{} + Face1 any + face2 any + faces map[string]any } s2 := st2{ @@ -253,7 +253,7 @@ func TestStruct_MapInterfacedValue(t *testing.T) { Github: "https://github.com/inhere", Face1: s1, face2: s1, - faces: map[string]interface{}{ + faces: map[string]any{ "key1": 12, "key2": "abc2344", }, @@ -285,7 +285,7 @@ func TestStruct_ptrField(t *testing.T) { } func TestFormat(t *testing.T) { - s := Format(23, "abc", map[string]interface{}{ + s := Format(23, "abc", map[string]any{ "key1": 12, "key2": "abc2344", }) @@ -295,8 +295,8 @@ func TestFormat(t *testing.T) { } func TestPrint_over_max_depth(t *testing.T) { - a := map[string]interface{}{} - a["circular"] = map[string]interface{}{ + a := map[string]any{} + a["circular"] = map[string]any{ "a": a, } @@ -310,7 +310,7 @@ func TestPrint_over_max_depth(t *testing.T) { } func TestPrint_cyclic_slice(t *testing.T) { - a := map[string]interface{}{ + a := map[string]any{ "bool": true, "number": 1 + 1i, "bytes": []byte{97, 98, 99}, diff --git a/dump/dumper.go b/dump/dumper.go index 19fc2dd12..52ecba32f 100644 --- a/dump/dumper.go +++ b/dump/dumper.go @@ -125,22 +125,22 @@ func (d *Dumper) ResetOptions() { } // Dump vars -func (d *Dumper) Dump(vs ...interface{}) { +func (d *Dumper) Dump(vs ...any) { d.dump(vs...) } // Print vars. alias of Dump() -func (d *Dumper) Print(vs ...interface{}) { +func (d *Dumper) Print(vs ...any) { d.dump(vs...) } // Println vars. alias of Dump() -func (d *Dumper) Println(vs ...interface{}) { +func (d *Dumper) Println(vs ...any) { d.dump(vs...) } // Fprint print vars to io.Writer -func (d *Dumper) Fprint(w io.Writer, vs ...interface{}) { +func (d *Dumper) Fprint(w io.Writer, vs ...any) { backup := d.Output // backup d.Output = w @@ -149,7 +149,7 @@ func (d *Dumper) Fprint(w io.Writer, vs ...interface{}) { } // dump go vars -func (d *Dumper) dump(vs ...interface{}) { +func (d *Dumper) dump(vs ...any) { // reset some settings. d.curDepth = 0 d.visited = make(map[visit]int) @@ -222,7 +222,7 @@ func (d *Dumper) advance(step int) { d.indentBytes = strutil.RepeatBytes(d.IndentChar, d.IndentLen*d.curDepth) } -func (d *Dumper) printOne(v interface{}) { +func (d *Dumper) printOne(v any) { if v == nil { d.indentPrint(",\n") return diff --git a/dump/dumper_test.go b/dump/dumper_test.go index 506b73c25..25719c102 100644 --- a/dump/dumper_test.go +++ b/dump/dumper_test.go @@ -154,7 +154,7 @@ func TestDump_Ptr(t *testing.T) { func TestDumper_AccessCantExportedField(t *testing.T) { type MyStruct struct { // id string - id interface{} + id any } myStruct := MyStruct{ @@ -192,9 +192,9 @@ func TestDumper_AccessCantExportedField1(t *testing.T) { // init an nested struct s1 := st1{st0{2}, 23, "inhere"} myS1 := struct { - // cannotExport interface{} // ok + // cannotExport any // ok cannotExport st1 // ok - // CanExport interface{} ok + // CanExport any ok CanExport st1 // ok }{ cannotExport: s1, @@ -207,7 +207,7 @@ func TestDumper_AccessCantExportedField1(t *testing.T) { // ------------------------- map ------------------------- func TestDump_Map(t *testing.T) { - m4 := map[string]interface{}{ + m4 := map[string]any{ "key1": 12, "key2": "val1", "key3": [][]int{ @@ -218,7 +218,7 @@ func TestDump_Map(t *testing.T) { "key5": -34, "key6": nil, "key7": []int{23, 34}, - "key8": map[string]interface{}{ + "key8": map[string]any{ "key8sub1": []int{23, 34}, "key8sub2": []string{"a", "b"}, }, @@ -260,7 +260,7 @@ func TestMap_Simpled(t *testing.T) { */ - m4 := map[string]interface{}{ + m4 := map[string]any{ "key1": 12, "key2": "val1", "key3": 34, @@ -284,7 +284,7 @@ func TestMap_Simpled(t *testing.T) { func TestMap_InterfaceNested(t *testing.T) { s1 := st1{st0{2}, 23, "inhere"} - m1 := map[string]interface{}{ + m1 := map[string]any{ "key1": 112, "key2": uint(112), "key3": int64(112), @@ -298,7 +298,7 @@ func TestMap_InterfaceNested(t *testing.T) { "key1": 12, "key2": 13, }, - "submap2": map[string]interface{}{ + "submap2": map[string]any{ "key1": 12, "key2": "abc", "submap21": map[string]string{ @@ -306,10 +306,10 @@ func TestMap_InterfaceNested(t *testing.T) { "key2": "val2", }, }, - "submap3": map[string]interface{}{ + "submap3": map[string]any{ "key1": 12, "key2": "abc", - "submap31": map[string]interface{}{ + "submap31": map[string]any{ "key31": 12, "key32": 13, "user": user, diff --git a/fsutil/finder/finder.go b/fsutil/finder/finder.go index db8db8fbd..dd32c4533 100644 --- a/fsutil/finder/finder.go +++ b/fsutil/finder/finder.go @@ -126,12 +126,12 @@ func (f *FileFinder) ExcludeName(files ...string) *FileFinder { } // AddFilter for filter filepath or dirpath -func (f *FileFinder) AddFilter(filterFuncs ...interface{}) *FileFinder { +func (f *FileFinder) AddFilter(filterFuncs ...any) *FileFinder { return f.WithFilter(filterFuncs...) } // WithFilter add filter func for filtering filepath or dirpath -func (f *FileFinder) WithFilter(filterFuncs ...interface{}) *FileFinder { +func (f *FileFinder) WithFilter(filterFuncs ...any) *FileFinder { for _, filterFunc := range filterFuncs { if fileFilter, ok := filterFunc.(FileFilter); ok { f.fileFilters = append(f.fileFilters, fileFilter) diff --git a/goutil.go b/goutil.go index aefd8c165..97a1fe90e 100644 --- a/goutil.go +++ b/goutil.go @@ -43,12 +43,12 @@ func MustOK(err error) { } // Panicf format panic message use fmt.Sprintf -func Panicf(format string, v ...interface{}) { +func Panicf(format string, v ...any) { panic(fmt.Sprintf(format, v...)) } // FuncName get func name -func FuncName(f interface{}) string { +func FuncName(f any) string { return stdutil.FuncName(f) } diff --git a/maputil/convert.go b/maputil/convert.go index 141b6eb2a..da77b89f2 100644 --- a/maputil/convert.go +++ b/maputil/convert.go @@ -86,7 +86,7 @@ func FormatIndent(mp any, indent string) string { // {"top": {"sub": "value", "sub2": "value2"} } // -> // {"top.sub": "value", "top.sub2": "value2" } -func Flatten(mp map[string]any) map[string]interface{} { +func Flatten(mp map[string]any) map[string]any { if mp == nil { return nil } diff --git a/reflects/util.go b/reflects/util.go index e44595700..41b21e81b 100644 --- a/reflects/util.go +++ b/reflects/util.go @@ -62,7 +62,7 @@ func SliceSubKind(typ reflect.Type) reflect.Kind { } // SetValue to a reflect.Value -func SetValue(rv reflect.Value, val interface{}) error { +func SetValue(rv reflect.Value, val any) error { // get real type of the ptr value if rv.Kind() == reflect.Ptr { // init if is nil diff --git a/reflects/util_test.go b/reflects/util_test.go index 1b429c797..e28fe52dc 100644 --- a/reflects/util_test.go +++ b/reflects/util_test.go @@ -11,7 +11,7 @@ import ( func TestLen(t *testing.T) { is := assert.New(t) - tests := []interface{}{ + tests := []any{ "abc", 123, int8(123), int16(123), int32(123), int64(123), @@ -33,7 +33,7 @@ func TestLen(t *testing.T) { func TestSliceSubKind(t *testing.T) { noErrTests := []struct { - val interface{} + val any want reflect.Kind }{ {"invalid", reflect.Invalid}, @@ -48,7 +48,7 @@ func TestSliceSubKind(t *testing.T) { {[]uint32{1, 2}, reflect.Uint32}, {[]uint64{1, 2}, reflect.Uint64}, {[]string{"a", "b"}, reflect.String}, - {[]interface{}{"a", "b"}, reflect.Interface}, + {[]any{"a", "b"}, reflect.Interface}, } for _, item := range noErrTests { @@ -108,7 +108,7 @@ func TestSliceAddItem_ok(t *testing.T) { } func TestSlice_subMap_addItem(t *testing.T) { - d := []interface{}{ + d := []any{ map[string]string{ "k3051": "v3051", }, @@ -123,7 +123,7 @@ func TestSlice_subMap_addItem(t *testing.T) { } func TestMap_subSlice_addItem(t *testing.T) { - mp := map[string]interface{}{ + mp := map[string]any{ "sl": []string{"abc"}, } diff --git a/stdio/ioutil.go b/stdio/ioutil.go index 7da504bfe..d2942d45c 100644 --- a/stdio/ioutil.go +++ b/stdio/ioutil.go @@ -13,7 +13,7 @@ func QuietFprint(w io.Writer, ss ...string) { } // QuietFprintf to writer, will ignore error -func QuietFprintf(w io.Writer, tpl string, vs ...interface{}) { +func QuietFprintf(w io.Writer, tpl string, vs ...any) { _, _ = fmt.Fprintf(w, tpl, vs...) } diff --git a/stdutil/stdutil.go b/stdutil/stdutil.go index f56583bc4..a0382afe0 100644 --- a/stdutil/stdutil.go +++ b/stdutil/stdutil.go @@ -24,7 +24,7 @@ func PanicIf(err error) { } // Panicf format panic message use fmt.Sprintf -func Panicf(format string, v ...interface{}) { +func Panicf(format string, v ...any) { panic(fmt.Sprintf(format, v...)) } diff --git a/structs/data.go b/structs/data.go index 5595c4ad4..63702110b 100644 --- a/structs/data.go +++ b/structs/data.go @@ -25,12 +25,12 @@ func (d *LiteData) SetData(data map[string]any) { } // Value get from data -func (d *LiteData) Value(key string) interface{} { +func (d *LiteData) Value(key string) any { return d.data[key] } // GetVal get from data -func (d *LiteData) GetVal(key string) interface{} { +func (d *LiteData) GetVal(key string) any { return d.data[key] } @@ -83,7 +83,7 @@ func (d *Data) EnableLock() *Data { } // Data get all -func (d *Data) Data() map[string]interface{} { +func (d *Data) Data() map[string]any { return d.data } @@ -106,16 +106,16 @@ func (d *Data) DataLen() int { // ResetData all data func (d *Data) ResetData() { - d.data = make(map[string]interface{}) + d.data = make(map[string]any) } // Set value to data -func (d *Data) Set(key string, val interface{}) { +func (d *Data) Set(key string, val any) { d.SetValue(key, val) } // SetValue to data -func (d *Data) SetValue(key string, val interface{}) { +func (d *Data) SetValue(key string, val any) { if d.enableLock { d.Lock() defer d.Unlock() @@ -125,7 +125,7 @@ func (d *Data) SetValue(key string, val interface{}) { } // Value get from data -func (d *Data) Value(key string) (val interface{}, ok bool) { +func (d *Data) Value(key string) (val any, ok bool) { if d.enableLock { d.RLock() defer d.RUnlock() @@ -136,12 +136,12 @@ func (d *Data) Value(key string) (val interface{}, ok bool) { } // Get val from data -func (d *Data) Get(key string) interface{} { +func (d *Data) Get(key string) any { return d.GetVal(key) } // GetVal get from data -func (d *Data) GetVal(key string) interface{} { +func (d *Data) GetVal(key string) any { if d.enableLock { d.RLock() defer d.RUnlock() diff --git a/strutil/strutil_test.go b/strutil/strutil_test.go index 6afd1e1d5..0dd7ecc27 100644 --- a/strutil/strutil_test.go +++ b/strutil/strutil_test.go @@ -62,7 +62,7 @@ func TestPadding(t *testing.T) { tests := []struct { want, give, pad string len int - pos uint8 + pos strutil.PosFlag }{ {"ab000", "ab", "0", 5, strutil.PosRight}, {"000ab", "ab", "0", 5, strutil.PosLeft}, diff --git a/sysutil/clipboard/clipboard.go b/sysutil/clipboard/clipboard.go index 2e79e93dd..34ba56ea7 100644 --- a/sysutil/clipboard/clipboard.go +++ b/sysutil/clipboard/clipboard.go @@ -3,10 +3,10 @@ package clipboard import ( "bytes" + "errors" "os/exec" "strings" - "github.com/gookit/goutil/errorx" "github.com/gookit/goutil/fsutil" "github.com/gookit/goutil/sysutil" ) @@ -83,7 +83,7 @@ func (c *Clipboard) WriteString(s string) (int, error) { // Flush buffer contents to clipboard func (c *Clipboard) Flush() error { if c.buf == nil || c.buf.Len() == 0 { - return errorx.Raw("not write contents") + return errors.New("not write contents") } // linux: diff --git a/testutil/assert/util.go b/testutil/assert/util.go index 243a0f283..d6c81ecc8 100644 --- a/testutil/assert/util.go +++ b/testutil/assert/util.go @@ -15,7 +15,6 @@ import ( "github.com/gookit/goutil/mathutil" "github.com/gookit/goutil/reflects" "github.com/gookit/goutil/strutil" - "github.com/gookit/goutil/sysutil" ) func checkEqualArgs(expected, actual any) error { @@ -92,34 +91,45 @@ func formatTplAndArgs(fmtAndArgs ...any) string { func callerInfos() []string { num := 3 + skip := 4 ss := make([]string, 0, num) - cs := sysutil.CallersInfos(4, num, func(file string, fc *runtime.Func) bool { + + for i := skip; i < skip+num; i++ { + pc, file, line, ok := runtime.Caller(i) + if !ok { + // The breaks below failed to terminate the loop, and we ran off the + // end of the call stack. + break + } + + fc := runtime.FuncForPC(pc) + if fc == nil { + continue + } + // This is a huge edge case, but it will panic if this is the case if file == "" { - return false + continue } fcName := fc.Name() if fcName == "testing.tRunner" || strings.Contains(fcName, "goutil/testutil/assert") { - return false + continue } // eg: runtime.goexit if strings.HasPrefix(fcName, "runtime.") { - return false + continue } - return true - }) - // cwd := sysutil.Workdir() - for _, info := range cs { - filePath := info.File + filePath := file if !ShowFullPath { filePath = fsutil.Name(filePath) } - ss = append(ss, fmt.Sprintf("%s:%d", filePath, info.Line)) + ss = append(ss, fmt.Sprintf("%s:%d", filePath, line)) } + return ss } diff --git a/timex/timex.go b/timex/timex.go index c81d946a2..50a1c0876 100644 --- a/timex/timex.go +++ b/timex/timex.go @@ -24,6 +24,10 @@ const ( OneHour = time.Hour OneDay = 24 * time.Hour OneWeek = 7 * 24 * time.Hour + + DatetimeLayout = "2006-01-02 15:04:05" + DateOnlyLayout = "2006-01-02" + TimeOnlyLayout = "15:04:05" ) var (