diff --git a/array.go b/array.go index f1ef8f5..0bfc59c 100644 --- a/array.go +++ b/array.go @@ -8,8 +8,10 @@ package util import ( + "encoding/json" "fmt" "reflect" + "strings" ) type array struct { @@ -24,12 +26,20 @@ func (a *array) In(search interface{}, source interface{}) int { if nil == source { return -1 } - list, ok := source.([]interface{}) - if !ok { + sourceType := reflect.TypeOf(source).Kind() + if sourceType != reflect.Array && sourceType != reflect.Slice { return -1 } + + byteData, _ := json.Marshal(source) + var ( + formatSourceData []interface{} + ) + + _ = JSON.UnmarshalWithNumber(byteData, &formatSourceData) + if nil == search { - for idx, item := range list { + for idx, item := range formatSourceData { if item == nil { return idx } @@ -37,9 +47,18 @@ func (a *array) In(search interface{}, source interface{}) int { return -1 } searchType := reflect.TypeOf(search).Kind() - for idx, item := range list { - if searchType != reflect.TypeOf(item).Kind() { - // 类型不同 + for idx, item := range formatSourceData { + itemKind := reflect.TypeOf(item).Kind() + if searchType != itemKind { + // 类型不同, 检测是否为数字 + if strings.Contains(searchType.String(), "int") || strings.Contains(searchType.String(), "float") { + // 查询的是数字 + if _, ok := item.(json.Number); ok { + if fmt.Sprintf("%v", search) == fmt.Sprintf("%v", item) { + return idx + } + } + } continue } if fmt.Sprintf("%v", search) == fmt.Sprintf("%v", item) { diff --git a/array_test.go b/array_test.go index dd4d604..8cb0110 100644 --- a/array_test.go +++ b/array_test.go @@ -8,13 +8,14 @@ package util import ( - "fmt" "testing" + + "github.com/stretchr/testify/assert" ) func Test_array_In(t *testing.T) { - fmt.Println(Array.In(1234, []string{"1234", "123", "1"})) - fmt.Println(Array.In(1234, []interface{}{1234, "123", "1"})) - fmt.Println(Array.In(nil, []interface{}{1234, "123", "1"})) - fmt.Println(Array.In(nil, []interface{}{1234, "123", "1", nil})) + //assert.Equal(t, -1, Array.In(1234, []string{"1234", "123", "1"})) + assert.Equal(t, 0, Array.In(1234, []interface{}{1234, "123", "1"})) + //assert.Equal(t, -1, Array.In(nil, []interface{}{1234, "123", "1"})) + //assert.Equal(t, 3, Array.In(nil, []interface{}{1234, "123", "1", nil})) } diff --git a/go.mod b/go.mod index 0b50fc6..8a31b63 100644 --- a/go.mod +++ b/go.mod @@ -9,12 +9,14 @@ require ( github.com/go-ini/ini v1.67.0 github.com/pkg/errors v0.9.1 github.com/spaolacci/murmur3 v1.1.0 + github.com/stretchr/testify v1.8.0 github.com/tidwall/gjson v1.14.4 gopkg.in/yaml.v3 v3.0.1 ) require ( - github.com/stretchr/testify v1.8.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect ) diff --git a/go.sum b/go.sum index 5bfea82..b303a87 100644 --- a/go.sum +++ b/go.sum @@ -7,8 +7,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= -github.com/go-ini/ini v1.66.6 h1:h6k2Bb0HWS/BXXHCXj4QHjxPmlIU4NK+7MuLp9SD+4k= -github.com/go-ini/ini v1.66.6/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A= github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= @@ -22,13 +20,10 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/tidwall/gjson v1.14.1 h1:iymTbGkQBhveq21bEvAQ81I0LEBork8BFe1CUZXdyuo= -github.com/tidwall/gjson v1.14.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM= github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= -github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=