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-05-06 14:56:35 +08:00
|
|
|
"git.zhangdeman.cn/zhangdeman/op_type"
|
2024-10-13 19:23:20 +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-05-06 14:56:35 +08:00
|
|
|
func ArrayType[Bt op_type.BaseType](value []Bt) *Array[Bt] {
|
|
|
|
at := &Array[Bt]{
|
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-05-06 14:56:35 +08:00
|
|
|
type Array[Bt op_type.BaseType] struct {
|
|
|
|
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-05-06 14:56:35 +08:00
|
|
|
func (at *Array[Bt]) 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-05-06 14:56:35 +08:00
|
|
|
func (at *Array[Bt]) 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-05-06 15:00:32 +08:00
|
|
|
func (at *Array[Bt]) Unique() []Bt {
|
|
|
|
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-05-06 14:56:35 +08:00
|
|
|
func (at *Array[Bt]) Has(input Bt) int {
|
|
|
|
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-05-06 14:56:35 +08:00
|
|
|
func (at *Array[Bt]) 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-05-06 14:56:35 +08:00
|
|
|
func (at *Array[Bt]) 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-10-13 19:23:20 +08:00
|
|
|
|
|
|
|
// ExtraField 提取[]map/[]struct 中的指定字段, 并以list形式返回
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 19:00 2024/10/13
|
2024-10-13 19:28:16 +08:00
|
|
|
func (at *Array[Bt]) ExtraField(fieldName string) String {
|
2024-10-13 19:23:20 +08:00
|
|
|
if at.IsNil() {
|
2024-10-13 19:28:16 +08:00
|
|
|
return String("[]")
|
2024-10-13 19:23:20 +08:00
|
|
|
}
|
|
|
|
byteData, _ := json.Marshal(at.value)
|
|
|
|
res := make([]any, 0)
|
|
|
|
list := gjson.ParseBytes(byteData).Array()
|
|
|
|
for _, item := range list {
|
|
|
|
itemValue := item.Get(fieldName)
|
|
|
|
if itemValue.Exists() {
|
|
|
|
res = append(res, itemValue.Value())
|
|
|
|
}
|
|
|
|
}
|
2024-10-13 19:28:16 +08:00
|
|
|
return String(ArrayType(res).ToString().Value)
|
2024-10-13 19:23:20 +08:00
|
|
|
}
|