Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/go_modules/github.com/stretchr/…
Browse files Browse the repository at this point in the history
…testify-1.6.1
  • Loading branch information
inhere committed Dec 26, 2020
2 parents ef28137 + 48c801f commit ebfa60e
Show file tree
Hide file tree
Showing 32 changed files with 774 additions and 94 deletions.
7 changes: 7 additions & 0 deletions .github/dependabot.yml
@@ -0,0 +1,7 @@
version: 2
updates:
- package-ecosystem: gomod
directory: "/"
schedule:
interval: daily
open-pull-requests-limit: 10
4 changes: 3 additions & 1 deletion .github/workflows/go.yml
Expand Up @@ -2,7 +2,9 @@ name: Unit-Tests
on:
push:
paths:
- 'go.mod'
- '**.go'
- '**.yml'

jobs:

Expand All @@ -11,7 +13,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
go_version: [1.11, 1.12, 1.13, 1.14]
go_version: [1.12, 1.13, 1.14, 1.15]
os: [ubuntu-latest, windows-latest, macOS-latest]

steps:
Expand Down
38 changes: 38 additions & 0 deletions .github/workflows/release.yml
@@ -0,0 +1,38 @@
name: Tag-release

on:
push:
tags:
- v*

jobs:
release:
name: Release new version
runs-on: ubuntu-latest
timeout-minutes: 10
strategy:
fail-fast: true
matrix:
go: [1.14]

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup ENV
# https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable
run: |
echo "RELEASE_TAG=${GITHUB_REF:10}" >> $GITHUB_ENV
echo "RELEASE_NAME=$GITHUB_WORKFLOW" >> $GITHUB_ENV
- name: Display Env
run: env

# https://github.com/actions/create-release
- uses: meeDamian/github-release@2.0
with:
gzip: false
token: ${{ secrets.GITHUB_TOKEN }}
tag: ${{ env.RELEASE_TAG }}
name: ${{ env.RELEASE_TAG }}
# files: kite-${{ env.RELEASE_TAG }}.phar
3 changes: 2 additions & 1 deletion .travis.yml
@@ -1,12 +1,13 @@
language: go
dist: bionic
env:
GO111MODULE: on
- GO111MODULE=on

go:
- '1.12'
- '1.13'
- '1.14'
- '1.15'

before_install:
- go get github.com/mattn/goveralls
Expand Down
11 changes: 11 additions & 0 deletions README.md
Expand Up @@ -39,6 +39,11 @@ func Reverse(ss []string)
func StringsRemove(ss []string, s string) []string
func StringsToInts(ss []string) (ints []int, err error)
func TrimStrings(ss []string, cutSet ...string) (ns []string)
func IntsHas(ints []int, val int) bool
func Int64sHas(ints []int64, val int64) bool
func StringsHas(ss []string, val string) bool
func Contains(arr, val interface{}) bool
func NotContains(arr, val interface{}) bool
```

### CLI Util
Expand Down Expand Up @@ -159,6 +164,7 @@ func ToInt(in interface{}) (iVal int, err error)
func ToInt64(in interface{}) (i64 int64, err error)
func ToUint(in interface{}) (u64 uint64, err error)
func Uint(in interface{}) (uint64, error)
func RandomInt(min, max int) int like php random_int() function
```

### String Util
Expand Down Expand Up @@ -208,6 +214,11 @@ func URLEncode(s string) string
func UpperFirst(s string) string
func UpperWord(s string) string
func Uppercase(s string) string
func RandomChars(ln int) string
func RandomCharsV2(ln int) string
func RandomCharsV3(ln int) string
func MicroTimeID() string
func MicroTimeHexID() string
```

### System Util
Expand Down
11 changes: 11 additions & 0 deletions README.zh-CN.md
Expand Up @@ -38,6 +38,11 @@ func Reverse(ss []string)
func StringsRemove(ss []string, s string) []string
func StringsToInts(ss []string) (ints []int, err error)
func TrimStrings(ss []string, cutSet ...string) (ns []string)
func IntsHas(ints []int, val int) bool
func Int64sHas(ints []int64, val int64) bool
func StringsHas(ss []string, val string) bool
func Contains(arr, val interface{}) bool
func NotContains(arr, val interface{}) bool
```

### CLI Util
Expand Down Expand Up @@ -158,6 +163,7 @@ func ToInt(in interface{}) (iVal int, err error)
func ToInt64(in interface{}) (i64 int64, err error)
func ToUint(in interface{}) (u64 uint64, err error)
func Uint(in interface{}) (uint64, error)
func RandomInt(min, max int) int like php random_int() function
```

### String Util
Expand Down Expand Up @@ -207,6 +213,11 @@ func URLEncode(s string) string
func UpperFirst(s string) string
func UpperWord(s string) string
func Uppercase(s string) string
func RandomChars(ln int) string
func RandomCharsV2(ln int) string
func RandomCharsV3(ln int) string
func MicroTimeID() string
func MicroTimeHexID() string
```

### System Util
Expand Down
84 changes: 83 additions & 1 deletion arrutil/slice.go
Expand Up @@ -2,8 +2,11 @@
package arrutil

import (
"reflect"
"strconv"
"strings"

"github.com/gookit/goutil/mathutil"
)

// Reverse string slice [site user info 0] -> [0 info user site]
Expand Down Expand Up @@ -39,7 +42,6 @@ func StringsToInts(ss []string) (ints []int, err error) {

ints = append(ints, iVal)
}

return
}

Expand All @@ -56,3 +58,83 @@ func TrimStrings(ss []string, cutSet ...string) (ns []string) {
}
return
}

// IntsHas check the []int contains the given value
func IntsHas(ints []int, val int) bool {
for _, ele := range ints {
if ele == val {
return true
}
}
return false
}

// Int64sHas check the []int64 contains the given value
func Int64sHas(ints []int64, val int64) bool {
for _, ele := range ints {
if ele == val {
return true
}
}
return false
}

// StringsHas check the []string contains the given element
func StringsHas(ss []string, val string) bool {
for _, ele := range ss {
if ele == val {
return true
}
}
return false
}

// Contains array(strings, ints, uints) should be contains the given value(int(X),string).
func Contains(arr, val interface{}) bool {
if val == nil || arr == nil {
return false
}

// if is string value
if strVal, ok := val.(string); ok {
if ss, ok := arr.([]string); ok {
return StringsHas(ss, strVal)
}
return false
}

// as int value
intVal, err := mathutil.Int64(val)
if err != nil {
return false
}

if int64s, ok := toInt64Slice(arr); ok {
return Int64sHas(int64s, intVal)
}
return false
}

// NotContains array(strings, ints, uints) should be not contains the given value.
func NotContains(arr, val interface{}) bool {
return false == Contains(arr, val)
}

func toInt64Slice(arr interface{}) (ret []int64, ok bool) {
rv := reflect.ValueOf(arr)
if rv.Kind() != reflect.Slice {
return
}

for i := 0; i < rv.Len(); i++ {
i64, err := mathutil.Int64(rv.Index(i).Interface())
if err != nil {
return []int64{}, false
}

ret = append(ret, i64)
}

ok = true
return
}
65 changes: 65 additions & 0 deletions arrutil/slice_test.go
Expand Up @@ -44,3 +44,68 @@ func TestStringsToInts(t *testing.T) {
_, err = arrutil.StringsToInts([]string{"a", "b"})
is.Error(err)
}

func TestIntsHas(t *testing.T) {
ints := []int{2, 4, 5}
assert.True(t, arrutil.IntsHas(ints, 2))
assert.True(t, arrutil.IntsHas(ints, 5))
assert.False(t, arrutil.IntsHas(ints, 3))
}

func TestInt64sHas(t *testing.T) {
ints := []int64{2, 4, 5}
assert.True(t, arrutil.Int64sHas(ints, 2))
assert.True(t, arrutil.Int64sHas(ints, 5))
assert.False(t, arrutil.Int64sHas(ints, 3))
}

func TestStringsHas(t *testing.T) {
ss := []string{"a", "b"}
assert.True(t, arrutil.StringsHas(ss, "a"))
assert.True(t, arrutil.StringsHas(ss, "b"))
assert.False(t, arrutil.StringsHas(ss, "c"))
}

func TestContains(t *testing.T) {
is := assert.New(t)
tests := map[interface{}]interface{}{
1: []int{1, 2, 3},
2: []int8{1, 2, 3},
3: []int16{1, 2, 3},
4: []int32{4, 2, 3},
5: []int64{5, 2, 3},
6: []uint{6, 2, 3},
7: []uint8{7, 2, 3},
8: []uint16{8, 2, 3},
9: []uint32{9, 2, 3},
10: []uint64{10, 3},
11: []string{"11", "3"},
'a': []int64{97},
'b': []rune{'a', 'b'},
'c': []byte{'a', 'b', 'c'}, // byte -> uint8
"a": []string{"a", "b", "c"},
}

for val, list := range tests {
is.True(arrutil.Contains(list, val))
is.False(arrutil.NotContains(list, val))
}

is.False(arrutil.Contains(nil, []int{}))
is.False(arrutil.Contains('a', []int{}))
//
is.False(arrutil.Contains([]int{2, 3}, []int{2}))
is.False(arrutil.Contains([]string{"a", "b"}, 12))
is.False(arrutil.Contains(nil, 12))
is.False(arrutil.Contains(map[int]int{2: 3}, 12))

tests1 := map[interface{}]interface{}{
2: []int{1, 3},
"a": []string{"b", "c"},
}

for val, list := range tests1 {
is.True(arrutil.NotContains(list, val))
is.False(arrutil.Contains(list, val))
}
}
37 changes: 24 additions & 13 deletions dump/dump_test.go
Expand Up @@ -207,39 +207,50 @@ func TestStruct_CannotExportField(t *testing.T) {
}

func TestStruct_InterfaceField(t *testing.T) {
s1 := st1{st0{2}, 23, "inhere"}

Println(struct {
cannotExport interface{}
myS1 := struct {
// cannotExport interface{} // ok
cannotExport st1 // ok
// CanExport interface{} ok
CanExport st1 // ok
}{
cannotExport: s1,
})
CanExport: s1,
}

Println(struct {
Println(myS1)
color.Infoln("\nUse fmt.Println:")
fmt.Println(myS1)
}

func TestStruct_MapInterfacedValue(t *testing.T) {
myS2 := struct {
cannotExport map[string]interface{}
}{
cannotExport: map[string]interface{}{
"key1": 12,
"key2": "abc",
"key2": "abcd123",
},
})
}
Println(myS2)
color.Infoln("\nUse fmt.Println:")
fmt.Println(myS2)

type st2 struct {
st1
Github string
Face1 interface{}
face2 interface{}
Face1 interface{}
face2 interface{}
faces map[string]interface{}
}

s2 := st2{
st1: s1,
Github: "https://github.com/inhere",
Face1: s1,
face2: s1,
Face1: s1,
face2: s1,
faces: map[string]interface{}{
"key1": 12,
"key2": "abc",
"key2": "abc2344",
},
}

Expand Down

0 comments on commit ebfa60e

Please sign in to comment.