数组支持提取指定路径字段, 转换成list
This commit is contained in:
parent
3533617196
commit
50aa5ae7f8
2
any.go
2
any.go
@ -97,7 +97,7 @@ func (at *AnyType) ToString() String {
|
||||
case consts.DataTypeSliceAny:
|
||||
var val []any
|
||||
_ = serialize.JSON.Transition(at.data, &val)
|
||||
return String(ArrayType[any](val).ToString().Value)
|
||||
return String(ArrayType[any, any](val).ToString().Value)
|
||||
case consts.DataTypeMapAnyAny:
|
||||
return String(EasyMap(at.data).ToString())
|
||||
case consts.DataTypeInt:
|
||||
|
64
array.go
64
array.go
@ -9,7 +9,9 @@ package wrapper
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"git.zhangdeman.cn/zhangdeman/op_type"
|
||||
"github.com/tidwall/gjson"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
@ -19,8 +21,8 @@ import (
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:03 2023/6/11
|
||||
func ArrayType[Bt op_type.BaseType](value []Bt) *Array[Bt] {
|
||||
at := &Array[Bt]{
|
||||
func ArrayType[Bt op_type.BaseType, ExtractDataType op_type.BaseType](value []Bt) *Array[Bt, ExtractDataType] {
|
||||
at := &Array[Bt, ExtractDataType]{
|
||||
value: value,
|
||||
}
|
||||
return at
|
||||
@ -31,7 +33,7 @@ func ArrayType[Bt op_type.BaseType](value []Bt) *Array[Bt] {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:05 2023/6/11
|
||||
type Array[Bt op_type.BaseType] struct {
|
||||
type Array[Bt op_type.BaseType, ExtractDataType op_type.BaseType] struct {
|
||||
value []Bt
|
||||
convertErr error
|
||||
itemType reflect.Kind // 简单list场景下, 每一项的数据类型
|
||||
@ -42,7 +44,7 @@ type Array[Bt op_type.BaseType] struct {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:11 2023/6/11
|
||||
func (at *Array[Bt]) IsNil() bool {
|
||||
func (at *Array[Bt, ExtractDataType]) IsNil() bool {
|
||||
return at.value == nil
|
||||
}
|
||||
|
||||
@ -51,7 +53,7 @@ func (at *Array[Bt]) IsNil() bool {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 11:42 2024/4/22
|
||||
func (at *Array[Bt]) ToStringSlice() []string {
|
||||
func (at *Array[Bt, ExtractDataType]) ToStringSlice() []string {
|
||||
list := make([]string, 0)
|
||||
for _, item := range at.value {
|
||||
byteData, _ := json.Marshal(item)
|
||||
@ -65,7 +67,7 @@ func (at *Array[Bt]) ToStringSlice() []string {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:43 2023/6/12
|
||||
func (at *Array[Bt]) Unique() []Bt {
|
||||
func (at *Array[Bt, ExtractDataType]) Unique() []Bt {
|
||||
result := make([]Bt, 0)
|
||||
dataTable := make(map[string]bool)
|
||||
|
||||
@ -90,7 +92,7 @@ func (at *Array[Bt]) Unique() []Bt {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:28 2023/7/31
|
||||
func (at *Array[Bt]) Has(input Bt) int {
|
||||
func (at *Array[Bt, ExtractDataType]) Has(input Bt) int {
|
||||
for idx := 0; idx < len(at.value); idx++ {
|
||||
if reflect.TypeOf(at.value[idx]).String() != reflect.TypeOf(input).String() {
|
||||
// 类型不同
|
||||
@ -111,7 +113,7 @@ func (at *Array[Bt]) Has(input Bt) int {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:57 2023/9/28
|
||||
func (at *Array[Bt]) ToString() StringResult {
|
||||
func (at *Array[Bt, ExtractDataType]) ToString() StringResult {
|
||||
if at.IsNil() {
|
||||
return StringResult{
|
||||
Value: "",
|
||||
@ -130,7 +132,7 @@ func (at *Array[Bt]) ToString() StringResult {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:42 2023/10/25
|
||||
func (at *Array[Bt]) ToStringWithSplit(split string) StringResult {
|
||||
func (at *Array[Bt, ExtractDataType]) ToStringWithSplit(split string) StringResult {
|
||||
if at.IsNil() {
|
||||
return StringResult{
|
||||
Value: "",
|
||||
@ -142,3 +144,47 @@ func (at *Array[Bt]) ToStringWithSplit(split string) StringResult {
|
||||
Err: nil,
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
@ -13,6 +13,24 @@ import (
|
||||
)
|
||||
|
||||
func TestArray_Unique(t *testing.T) {
|
||||
fmt.Println(ArrayType([]any{"1", 1, 1, "1", 2, 3}).Unique())
|
||||
fmt.Println(ArrayType([]int{1, 1, 2, 3}).Unique())
|
||||
fmt.Println(ArrayType[any, any]([]any{"1", 1, 1, "1", 2, 3}).Unique())
|
||||
fmt.Println(ArrayType[int, any]([]int{1, 1, 2, 3}).Unique())
|
||||
}
|
||||
|
||||
func TestArray_ExtractField(t *testing.T) {
|
||||
testMap := []any{
|
||||
map[string]any{
|
||||
"age": 18,
|
||||
"name": "baicha",
|
||||
},
|
||||
map[string]any{
|
||||
"age": 20,
|
||||
"name": "qinghuan",
|
||||
},
|
||||
map[string]any{
|
||||
"foo": "bar",
|
||||
},
|
||||
}
|
||||
fmt.Println(ArrayType[any, int](testMap).ExtractField("age"))
|
||||
fmt.Println(ArrayType[any, string](testMap).ExtractField("name"))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user