in_array优化类型检测

This commit is contained in:
白茶清欢 2023-03-30 16:22:56 +08:00
parent faba0a5a9e
commit 8aee2fb907
4 changed files with 34 additions and 17 deletions

View File

@ -8,8 +8,10 @@
package util package util
import ( import (
"encoding/json"
"fmt" "fmt"
"reflect" "reflect"
"strings"
) )
type array struct { type array struct {
@ -24,12 +26,20 @@ func (a *array) In(search interface{}, source interface{}) int {
if nil == source { if nil == source {
return -1 return -1
} }
list, ok := source.([]interface{}) sourceType := reflect.TypeOf(source).Kind()
if !ok { if sourceType != reflect.Array && sourceType != reflect.Slice {
return -1 return -1
} }
byteData, _ := json.Marshal(source)
var (
formatSourceData []interface{}
)
_ = JSON.UnmarshalWithNumber(byteData, &formatSourceData)
if nil == search { if nil == search {
for idx, item := range list { for idx, item := range formatSourceData {
if item == nil { if item == nil {
return idx return idx
} }
@ -37,9 +47,18 @@ func (a *array) In(search interface{}, source interface{}) int {
return -1 return -1
} }
searchType := reflect.TypeOf(search).Kind() searchType := reflect.TypeOf(search).Kind()
for idx, item := range list { for idx, item := range formatSourceData {
if searchType != reflect.TypeOf(item).Kind() { 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 continue
} }
if fmt.Sprintf("%v", search) == fmt.Sprintf("%v", item) { if fmt.Sprintf("%v", search) == fmt.Sprintf("%v", item) {

View File

@ -8,13 +8,14 @@
package util package util
import ( import (
"fmt"
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
func Test_array_In(t *testing.T) { func Test_array_In(t *testing.T) {
fmt.Println(Array.In(1234, []string{"1234", "123", "1"})) //assert.Equal(t, -1, Array.In(1234, []string{"1234", "123", "1"}))
fmt.Println(Array.In(1234, []interface{}{1234, "123", "1"})) assert.Equal(t, 0, Array.In(1234, []interface{}{1234, "123", "1"}))
fmt.Println(Array.In(nil, []interface{}{1234, "123", "1"})) //assert.Equal(t, -1, Array.In(nil, []interface{}{1234, "123", "1"}))
fmt.Println(Array.In(nil, []interface{}{1234, "123", "1", nil})) //assert.Equal(t, 3, Array.In(nil, []interface{}{1234, "123", "1", nil}))
} }

4
go.mod
View File

@ -9,12 +9,14 @@ require (
github.com/go-ini/ini v1.67.0 github.com/go-ini/ini v1.67.0
github.com/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1
github.com/spaolacci/murmur3 v1.1.0 github.com/spaolacci/murmur3 v1.1.0
github.com/stretchr/testify v1.8.0
github.com/tidwall/gjson v1.14.4 github.com/tidwall/gjson v1.14.4
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1
) )
require ( 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/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect
) )

5
go.sum
View File

@ -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/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 h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= 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 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= 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.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 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= 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 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM=
github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= 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 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= 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.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=