Skip to content

Commit

Permalink
up: migrate interface{} to go1.18 any keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 17, 2022
1 parent 623fc20 commit a90d655
Show file tree
Hide file tree
Showing 31 changed files with 146 additions and 125 deletions.
8 changes: 4 additions & 4 deletions arrutil/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cflag/cflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion cflag/cflag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion cflag/optarg.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// OptCheckFn define
type OptCheckFn func(val interface{}) error
type OptCheckFn func(val any) error

// FlagOpt struct
type FlagOpt struct {
Expand Down
12 changes: 6 additions & 6 deletions check.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ import (
)

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

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

// IsFunc value
func IsFunc(val interface{}) bool {
func IsFunc(val any) bool {
if val == nil {
return false
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion comdef/comdef.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion comdef/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
11 changes: 6 additions & 5 deletions dump/_examples/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}},
)
}
5 changes: 3 additions & 2 deletions dump/_examples/demo1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -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 {
Expand Down
13 changes: 7 additions & 6 deletions dump/_examples/demo_cyclic_ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,29 @@ 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,
}

// TIP: will stack overflow
// 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)
}
7 changes: 4 additions & 3 deletions dump/_examples/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
9 changes: 5 additions & 4 deletions dump/_examples/refer_kr_pretty.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions dump/_examples/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
9 changes: 5 additions & 4 deletions dump/_examples/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand Down
16 changes: 8 additions & 8 deletions dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,44 +77,44 @@ 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...)
return w.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...)
}

0 comments on commit a90d655

Please sign in to comment.