2023-03-30 14:49:38 +08:00
|
|
|
// Package util ...
|
|
|
|
//
|
|
|
|
// Description : util ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2023-03-30 14:35
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2023-03-30 16:22:56 +08:00
|
|
|
"encoding/json"
|
2023-03-30 14:49:38 +08:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2023-03-30 16:22:56 +08:00
|
|
|
"strings"
|
2023-03-30 14:49:38 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type array struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// In 判断指定数据是否在目标数据集中, 不存在返回 -1 , 存在时返回数据对应的索引
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 14:38 2023/3/30
|
|
|
|
func (a *array) In(search interface{}, source interface{}) int {
|
|
|
|
if nil == source {
|
|
|
|
return -1
|
|
|
|
}
|
2023-03-30 16:22:56 +08:00
|
|
|
sourceType := reflect.TypeOf(source).Kind()
|
|
|
|
if sourceType != reflect.Array && sourceType != reflect.Slice {
|
2023-03-30 14:49:38 +08:00
|
|
|
return -1
|
|
|
|
}
|
2023-03-30 16:22:56 +08:00
|
|
|
|
|
|
|
byteData, _ := json.Marshal(source)
|
|
|
|
var (
|
|
|
|
formatSourceData []interface{}
|
|
|
|
)
|
|
|
|
|
|
|
|
_ = JSON.UnmarshalWithNumber(byteData, &formatSourceData)
|
|
|
|
|
2023-03-30 14:49:38 +08:00
|
|
|
if nil == search {
|
2023-03-30 16:22:56 +08:00
|
|
|
for idx, item := range formatSourceData {
|
2023-03-30 14:49:38 +08:00
|
|
|
if item == nil {
|
|
|
|
return idx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
searchType := reflect.TypeOf(search).Kind()
|
2023-03-30 16:22:56 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-30 14:49:38 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if fmt.Sprintf("%v", search) == fmt.Sprintf("%v", item) {
|
|
|
|
return idx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|