Skip to content

Commit

Permalink
chore: migrate more 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 b198b73 commit 7ba341d
Show file tree
Hide file tree
Showing 31 changed files with 98 additions and 98 deletions.
2 changes: 1 addition & 1 deletion arrutil/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func TestExceptWhileNotSliceShouldPanic(t *testing.T) {

func TestExceptWhileEmptyReturnsEmpty(t *testing.T) {
var data []string
result := arrutil.ExceptWhile(data, func(a interface{}) bool { return a == "b" || a == "c" }).([]string)
result := arrutil.ExceptWhile(data, func(a any) bool { return a == "b" || a == "c" }).([]string)

assert.Eq(t, []string{}, result)
assert.NotSame(t, &data, &result, "should always returns new slice")
Expand Down
40 changes: 20 additions & 20 deletions cliutil/color_print.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,65 +46,65 @@ func Grayln(a ...any) { color.Gray.Println(a...) }
func Greenp(a ...any) { color.Green.Print(a...) }

// Greenf print message with green color
func Greenf(format string, a ...interface{}) { color.Green.Printf(format, a...) }
func Greenf(format string, a ...any) { color.Green.Printf(format, a...) }

// Greenln print message line with green color
func Greenln(a ...interface{}) { color.Green.Println(a...) }
func Greenln(a ...any) { color.Green.Println(a...) }

// Yellowp print message with yellow color
func Yellowp(a ...interface{}) { color.Yellow.Print(a...) }
func Yellowp(a ...any) { color.Yellow.Print(a...) }

// Yellowf print message with yellow color
func Yellowf(format string, a ...interface{}) { color.Yellow.Printf(format, a...) }
func Yellowf(format string, a ...any) { color.Yellow.Printf(format, a...) }

// Yellowln print message line with yellow color
func Yellowln(a ...interface{}) { color.Yellow.Println(a...) }
func Yellowln(a ...any) { color.Yellow.Println(a...) }

// Magentap print message with magenta color
func Magentap(a ...interface{}) { color.Magenta.Print(a...) }
func Magentap(a ...any) { color.Magenta.Print(a...) }

// Magentaf print message with magenta color
func Magentaf(format string, a ...interface{}) { color.Magenta.Printf(format, a...) }
func Magentaf(format string, a ...any) { color.Magenta.Printf(format, a...) }

// Magentaln print message line with magenta color
func Magentaln(a ...interface{}) { color.Magenta.Println(a...) }
func Magentaln(a ...any) { color.Magenta.Println(a...) }

/*************************************************************
* quick use style print message
*************************************************************/

// Infop print message with info color
func Infop(a ...interface{}) { color.Info.Print(a...) }
func Infop(a ...any) { color.Info.Print(a...) }

// Infof print message with info style
func Infof(format string, a ...interface{}) { color.Info.Printf(format, a...) }
func Infof(format string, a ...any) { color.Info.Printf(format, a...) }

// Infoln print message with info style
func Infoln(a ...interface{}) { color.Info.Println(a...) }
func Infoln(a ...any) { color.Info.Println(a...) }

// Successp print message with success color
func Successp(a ...interface{}) { color.Success.Print(a...) }
func Successp(a ...any) { color.Success.Print(a...) }

// Successf print message with success style
func Successf(format string, a ...interface{}) { color.Success.Printf(format, a...) }
func Successf(format string, a ...any) { color.Success.Printf(format, a...) }

// Successln print message with success style
func Successln(a ...interface{}) { color.Success.Println(a...) }
func Successln(a ...any) { color.Success.Println(a...) }

// Errorp print message with error color
func Errorp(a ...interface{}) { color.Error.Print(a...) }
func Errorp(a ...any) { color.Error.Print(a...) }

// Errorf print message with error style
func Errorf(format string, a ...interface{}) { color.Error.Printf(format, a...) }
func Errorf(format string, a ...any) { color.Error.Printf(format, a...) }

// Errorln print message with error style
func Errorln(a ...interface{}) { color.Error.Println(a...) }
func Errorln(a ...any) { color.Error.Println(a...) }

// Warnp print message with warn color
func Warnp(a ...interface{}) { color.Warn.Print(a...) }
func Warnp(a ...any) { color.Warn.Print(a...) }

// Warnf print message with warn style
func Warnf(format string, a ...interface{}) { color.Warn.Printf(format, a...) }
func Warnf(format string, a ...any) { color.Warn.Printf(format, a...) }

// Warnln print message with warn style
func Warnln(a ...interface{}) { color.Warn.Println(a...) }
func Warnln(a ...any) { color.Warn.Println(a...) }
2 changes: 1 addition & 1 deletion maputil/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func HasKey(mp, key any) (ok bool) {
}

// HasAllKeys check of the given map.
func HasAllKeys(mp any, keys ...any) (ok bool, noKey interface{}) {
func HasAllKeys(mp any, keys ...any) (ok bool, noKey any) {
rftVal := reflect.Indirect(reflect.ValueOf(mp))
if rftVal.Kind() != reflect.Map {
return
Expand Down
4 changes: 2 additions & 2 deletions maputil/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestHasKey(t *testing.T) {
var mp interface{}
var mp any

mp = map[string]string{"key0": "val0"}
assert.True(t, maputil.HasKey(mp, "key0"))
Expand All @@ -17,7 +17,7 @@ func TestHasKey(t *testing.T) {
}

func TestHasAllKeys(t *testing.T) {
var mp interface{}
var mp any

mp = map[string]string{"key0": "val0", "key1": "def"}
ok, noKey := maputil.HasAllKeys(mp, "key0")
Expand Down
8 changes: 4 additions & 4 deletions maputil/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ func (d Data) IsEmtpy() bool {
}

// Value get from the data map
func (d Data) Value(key string) (interface{}, bool) {
func (d Data) Value(key string) (any, bool) {
val, ok := d.GetByPath(key)
return val, ok
}

// Get value from the data map.
// Supports dot syntax to get deep values. eg: top.sub
func (d Data) Get(key string) interface{} {
func (d Data) Get(key string) any {
if val, ok := d.GetByPath(key); ok {
return val
}
Expand All @@ -39,7 +39,7 @@ func (d Data) Get(key string) interface{} {

// GetByPath get value from the data map by path. eg: top.sub
// Supports dot syntax to get deep values.
func (d Data) GetByPath(path string) (interface{}, bool) {
func (d Data) GetByPath(path string) (any, bool) {
if val, ok := d[path]; ok {
return val, true
}
Expand Down Expand Up @@ -100,7 +100,7 @@ func (d Data) SetByKeys(keys []string, value any) error {
}

// Default get value from the data map with default value
func (d Data) Default(key string, def any) interface{} {
func (d Data) Default(key string, def any) any {
if val, ok := d.GetByPath(key); ok {
return val
}
Expand Down
4 changes: 2 additions & 2 deletions maputil/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func TestNewFormatter(t *testing.T) {
mp := map[string]interface{}{"a": "v0", "b": 23}
mp := map[string]any{"a": "v0", "b": 23}

mf := maputil.NewFormatter(mp)
assert.Contains(t, mf.String(), "b:23")
Expand All @@ -33,7 +33,7 @@ func TestNewFormatter(t *testing.T) {
}

func TestFormatIndent_mlevel(t *testing.T) {
mp := map[string]interface{}{"a": "v0", "b": 23}
mp := map[string]any{"a": "v0", "b": 23}

mp["subs"] = map[string]string{
"sub_k1": "sub val1",
Expand Down
12 changes: 6 additions & 6 deletions maputil/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import (
)

// DeepGet value by key path. eg "top" "top.sub"
func DeepGet(mp map[string]any, path string) (val interface{}) {
func DeepGet(mp map[string]any, path string) (val any) {
val, _ = GetByPath(path, mp)
return
}

// QuietGet value by key path. eg "top" "top.sub"
func QuietGet(mp map[string]any, path string) (val interface{}) {
func QuietGet(mp map[string]any, path string) (val any) {
val, _ = GetByPath(path, mp)
return
}

// GetByPath get value by key path from a map(map[string]any). eg "top" "top.sub"
func GetByPath(path string, mp map[string]any) (val interface{}, ok bool) {
func GetByPath(path string, mp map[string]any) (val any, ok bool) {
if val, ok := mp[path]; ok {
return val, true
}
Expand Down Expand Up @@ -59,7 +59,7 @@ func GetByPath(path string, mp map[string]any) (val interface{}, ok bool) {
}
case []string, []int, []float32, []float64, []bool, []rune:
slice := reflect.ValueOf(tData)
sData := make([]interface{}, slice.Len())
sData := make([]any, slice.Len())
for i := 0; i < slice.Len(); i++ {
sData[i] = slice.Index(i).Interface()
}
Expand All @@ -74,7 +74,7 @@ func GetByPath(path string, mp map[string]any) (val interface{}, ok bool) {
return item, true
}

func getBySlice(k string, slice []any) (val interface{}, ok bool) {
func getBySlice(k string, slice []any) (val any, ok bool) {
i, err := strconv.ParseInt(k, 10, 64)
if err != nil {
return nil, false
Expand All @@ -100,7 +100,7 @@ func Keys(mp any) (keys []string) {
}

// Values get all values from the given map.
func Values(mp any) (values []interface{}) {
func Values(mp any) (values []any) {
rftVal := reflect.Indirect(reflect.ValueOf(mp))
if rftVal.Kind() != reflect.Map {
return
Expand Down
10 changes: 5 additions & 5 deletions maputil/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
)

func TestGetByPath(t *testing.T) {
mp := map[string]interface{}{
mp := map[string]any{
"key0": "val0",
"key1": map[string]string{"sk0": "sv0"},
"key2": []string{"sv1", "sv2"},
"key3": map[string]interface{}{"sk1": "sv1"},
"key3": map[string]any{"sk1": "sv1"},
"key4": []int{1, 2},
"key5": []interface{}{1, "2", true},
"key5": []any{1, "2", true},
}

v, ok := maputil.GetByPath("key0", mp)
Expand Down Expand Up @@ -80,7 +80,7 @@ func TestGetByPath(t *testing.T) {
}

func TestKeys(t *testing.T) {
mp := map[string]interface{}{
mp := map[string]any{
"key0": "v0",
"key1": "v1",
"key2": 34,
Expand All @@ -106,7 +106,7 @@ func TestKeys(t *testing.T) {
}

func TestValues(t *testing.T) {
mp := map[string]interface{}{
mp := map[string]any{
"key0": "v0",
"key1": "v1",
"key2": 34,
Expand Down
12 changes: 6 additions & 6 deletions maputil/maputil.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ func MergeStringMap(src, dst map[string]string, ignoreCase bool) map[string]stri
//
// "site.info"
// ->
// map[string]interface{} {
// map[string]any {
// site: {info: val}
// }
//
// // case 2, last key is slice:
// "site.tags[1]"
// ->
// map[string]interface{} {
// map[string]any {
// site: {tags: [val]}
// }
func MakeByPath(path string, val interface{}) (mp map[string]interface{}) {
func MakeByPath(path string, val any) (mp map[string]any) {
return MakeByKeys(strings.Split(path, KeySepStr), val)
}

Expand All @@ -59,17 +59,17 @@ func MakeByPath(path string, val interface{}) (mp map[string]interface{}) {
// // case 1:
// []string{"site", "info"}
// ->
// map[string]interface{} {
// map[string]any {
// site: {info: val}
// }
//
// // case 2, last key is slice:
// []string{"site", "tags[1]"}
// ->
// map[string]interface{} {
// map[string]any {
// site: {tags: [val]}
// }
func MakeByKeys(keys []string, val any) (mp map[string]interface{}) {
func MakeByKeys(keys []string, val any) (mp map[string]any) {
size := len(keys)

// if last key contains slice index, make slice wrap the val
Expand Down
2 changes: 1 addition & 1 deletion maputil/maputil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// TODO remove
type any = interface{}
type any = any

func TestMergeStringMap(t *testing.T) {
ret := maputil.MergeSMap(map[string]string{"A": "v0"}, map[string]string{"A": "v1"}, false)
Expand Down
6 changes: 3 additions & 3 deletions maputil/setval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/gookit/goutil/testutil/assert"
)

func makMapForSetByPath() map[string]interface{} {
func makMapForSetByPath() map[string]any {
return map[string]any{
"key0": "v0",
"key2": 34,
Expand Down Expand Up @@ -150,7 +150,7 @@ func TestSetByKeys_slice_upAdd_method2(t *testing.T) {
}

func TestSetByPath(t *testing.T) {
mp := map[string]interface{}{
mp := map[string]any{
"key0": "v0",
"key1": "v1",
"key2": 34,
Expand All @@ -161,7 +161,7 @@ func TestSetByPath(t *testing.T) {
assert.ContainsKey(t, mp, "key0")
assert.Eq(t, "v00", mp["key0"])

err = maputil.SetByPath(&mp, "key3", map[string]interface{}{
err = maputil.SetByPath(&mp, "key3", map[string]any{
"k301": "v301",
"k302": 234,
"k303": []string{"v303-1", "v303-2"},
Expand Down
2 changes: 1 addition & 1 deletion netutil/httpreq/httpreq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestHttpReq_Send(t *testing.T) {
assert.False(t, httpreq.IsClientError(sc))
assert.False(t, httpreq.IsServerError(sc))

retMp := make(map[string]interface{})
retMp := make(map[string]any)
err = jsonutil.DecodeReader(resp.Body, &retMp)
assert.NoErr(t, err)
dump.P(retMp)
Expand Down
2 changes: 1 addition & 1 deletion netutil/httpreq/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func AddHeaders(req *http.Request, header http.Header) {
}

// ToQueryValues convert string-map to url.Values
func ToQueryValues(data interface{}) url.Values {
func ToQueryValues(data any) url.Values {
// use url.Values directly if we have it
if uv, ok := data.(url.Values); ok {
return uv
Expand Down
Loading

0 comments on commit 7ba341d

Please sign in to comment.