2023-06-11 21:17:27 +08:00
|
|
|
// Package wrapper ...
|
|
|
|
//
|
|
|
|
// Description : wrapper ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2023-06-11 21:02
|
|
|
|
package wrapper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2024-08-06 16:03:12 +08:00
|
|
|
"errors"
|
2024-05-06 14:56:35 +08:00
|
|
|
"git.zhangdeman.cn/zhangdeman/op_type"
|
2024-08-06 16:03:12 +08:00
|
|
|
"github.com/tidwall/gjson"
|
2023-06-12 18:18:16 +08:00
|
|
|
"reflect"
|
2023-06-11 21:17:27 +08:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-08-11 13:49:14 +08:00
|
|
|
// ArrayType 数组实例
|
2023-06-11 21:17:27 +08:00
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:03 2023/6/11
|
2024-08-06 16:03:12 +08:00
|
|
|
func ArrayType[Bt op_type.BaseType, ExtractDataType op_type.BaseType](value []Bt) *Array[Bt, ExtractDataType] {
|
|
|
|
at := &Array[Bt, ExtractDataType]{
|
2023-06-11 21:17:27 +08:00
|
|
|
value: value,
|
|
|
|
}
|
2023-06-12 18:18:16 +08:00
|
|
|
return at
|
2023-06-11 21:17:27 +08:00
|
|
|
}
|
|
|
|
|
2023-08-11 13:49:14 +08:00
|
|
|
// Array ...
|
2023-06-11 21:17:27 +08:00
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:05 2023/6/11
|
2024-08-06 16:03:12 +08:00
|
|
|
type Array[Bt op_type.BaseType, ExtractDataType op_type.BaseType] struct {
|
2024-05-06 14:56:35 +08:00
|
|
|
value []Bt
|
|
|
|
convertErr error
|
|
|
|
itemType reflect.Kind // 简单list场景下, 每一项的数据类型
|
2023-06-11 21:17:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsNil 输入是否为nil
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 21:11 2023/6/11
|
2024-08-06 16:03:12 +08:00
|
|
|
func (at *Array[Bt, ExtractDataType]) IsNil() bool {
|
2023-06-11 21:17:27 +08:00
|
|
|
return at.value == nil
|
|
|
|
}
|
|
|
|
|
2024-04-22 11:44:17 +08:00
|
|
|
// ToStringSlice ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 11:42 2024/4/22
|
2024-08-06 16:03:12 +08:00
|
|
|
func (at *Array[Bt, ExtractDataType]) ToStringSlice() []string {
|
2024-04-22 11:44:17 +08:00
|
|
|
list := make([]string, 0)
|
2024-05-06 14:56:35 +08:00
|
|
|
for _, item := range at.value {
|
|
|
|
byteData, _ := json.Marshal(item)
|
|
|
|
list = append(list, strings.Trim(string(byteData), "\""))
|
2024-04-22 11:44:17 +08:00
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2023-06-12 18:18:16 +08:00
|
|
|
// Unique 对数据结果进行去重
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 17:43 2023/6/12
|
2024-08-06 16:03:12 +08:00
|
|
|
func (at *Array[Bt, ExtractDataType]) Unique() []Bt {
|
2024-05-06 15:00:32 +08:00
|
|
|
result := make([]Bt, 0)
|
2024-05-06 14:29:33 +08:00
|
|
|
dataTable := make(map[string]bool)
|
|
|
|
|
2024-05-06 14:56:35 +08:00
|
|
|
for _, item := range at.value {
|
|
|
|
byteData, _ := json.Marshal(item)
|
|
|
|
k := string(byteData)
|
|
|
|
if strings.HasPrefix(k, "\"\"") && strings.HasSuffix(k, "\"\"") {
|
|
|
|
k = string(byteData[1 : len(byteData)-1])
|
2023-06-12 18:47:28 +08:00
|
|
|
}
|
2024-05-06 14:29:33 +08:00
|
|
|
if _, exist := dataTable[k]; exist {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dataTable[k] = true
|
|
|
|
result = append(result, item)
|
|
|
|
}
|
2024-05-06 15:00:32 +08:00
|
|
|
return result
|
2024-05-06 14:29:33 +08:00
|
|
|
|
2023-06-12 18:18:16 +08:00
|
|
|
}
|
2023-07-31 17:26:12 +08:00
|
|
|
|
2023-08-26 14:02:51 +08:00
|
|
|
// Has 查询一个值是否在列表里, 在的话, 返回首次出现的索引, 不在返回-1
|
2023-07-31 17:26:12 +08:00
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 16:28 2023/7/31
|
2024-08-06 16:03:12 +08:00
|
|
|
func (at *Array[Bt, ExtractDataType]) Has(input Bt) int {
|
2024-05-06 14:56:35 +08:00
|
|
|
for idx := 0; idx < len(at.value); idx++ {
|
|
|
|
if reflect.TypeOf(at.value[idx]).String() != reflect.TypeOf(input).String() {
|
2023-07-31 17:26:12 +08:00
|
|
|
// 类型不同
|
|
|
|
continue
|
|
|
|
}
|
2024-05-06 14:56:35 +08:00
|
|
|
sourceByte, _ := json.Marshal(at.value[idx])
|
2023-07-31 17:26:12 +08:00
|
|
|
inputByte, _ := json.Marshal(input)
|
|
|
|
if string(sourceByte) != string(inputByte) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return idx
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
2023-09-28 17:01:42 +08:00
|
|
|
|
|
|
|
// ToString ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 16:57 2023/9/28
|
2024-08-06 16:03:12 +08:00
|
|
|
func (at *Array[Bt, ExtractDataType]) ToString() StringResult {
|
2023-09-28 17:01:42 +08:00
|
|
|
if at.IsNil() {
|
2023-10-25 17:46:15 +08:00
|
|
|
return StringResult{
|
|
|
|
Value: "",
|
|
|
|
Err: nil,
|
|
|
|
}
|
|
|
|
}
|
2024-05-06 14:56:35 +08:00
|
|
|
byteData, err := json.Marshal(at.value)
|
2023-10-25 17:46:15 +08:00
|
|
|
return StringResult{
|
|
|
|
Value: string(byteData),
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToStringWithSplit 数组按照指定分隔符转为字符串
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 17:42 2023/10/25
|
2024-08-06 16:03:12 +08:00
|
|
|
func (at *Array[Bt, ExtractDataType]) ToStringWithSplit(split string) StringResult {
|
2023-10-25 17:46:15 +08:00
|
|
|
if at.IsNil() {
|
|
|
|
return StringResult{
|
|
|
|
Value: "",
|
|
|
|
Err: nil,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return StringResult{
|
2024-05-06 14:56:35 +08:00
|
|
|
Value: strings.Join(at.ToStringSlice(), split),
|
2023-10-25 17:46:15 +08:00
|
|
|
Err: nil,
|
2023-09-28 17:01:42 +08:00
|
|
|
}
|
|
|
|
}
|
2024-08-06 16:03:12 +08:00
|
|
|
|
|
|
|
// ExtractField 提取指定字段, 转换成数组
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 15:24 2024/8/6
|
|
|
|
func (at *Array[Bt, ExtractDataType]) ExtractField(fieldPath string) ([]ExtractDataType, error) {
|
|
|
|
strValResult := at.ToString()
|
|
|
|
if nil != strValResult.Err {
|
|
|
|
return make([]ExtractDataType, 0), nil
|
|
|
|
}
|
|
|
|
gjsonResult := gjson.Parse(strValResult.Value)
|
|
|
|
if !gjsonResult.IsArray() {
|
|
|
|
return make([]ExtractDataType, 0), errors.New("input value is not slice")
|
|
|
|
}
|
|
|
|
arrList := gjsonResult.Array()
|
|
|
|
if len(arrList) == 0 {
|
|
|
|
return make([]ExtractDataType, 0), nil
|
|
|
|
}
|
|
|
|
res := make([]ExtractDataType, 0)
|
|
|
|
for _, item := range arrList {
|
|
|
|
valueResult := item.Get(fieldPath)
|
|
|
|
if !valueResult.Exists() {
|
|
|
|
// 不存在
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var val ExtractDataType
|
|
|
|
if err := ConvertAssign(&val, valueResult.String()); nil != err {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res = append(res, val)
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExtractFieldIgnoreError 提取指定字段并忽略异常
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 15:28 2024/8/6
|
|
|
|
func (at *Array[Bt, ExtractDataType]) ExtractFieldIgnoreError(field string) []ExtractDataType {
|
|
|
|
res, _ := at.ExtractField(field)
|
|
|
|
return res
|
|
|
|
}
|