From 50aa5ae7f805dda9b2c26264f974187aba013224 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 6 Aug 2024 16:03:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E7=BB=84=E6=94=AF=E6=8C=81=E6=8F=90?= =?UTF-8?q?=E5=8F=96=E6=8C=87=E5=AE=9A=E8=B7=AF=E5=BE=84=E5=AD=97=E6=AE=B5?= =?UTF-8?q?,=20=E8=BD=AC=E6=8D=A2=E6=88=90list?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- any.go | 2 +- array.go | 64 +++++++++++++++++++++++++++++++++++++++++++-------- array_test.go | 22 ++++++++++++++++-- 3 files changed, 76 insertions(+), 12 deletions(-) diff --git a/any.go b/any.go index d28e834..99fbe24 100644 --- a/any.go +++ b/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: diff --git a/array.go b/array.go index 9604988..fa1a8a2 100644 --- a/array.go +++ b/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 +} diff --git a/array_test.go b/array_test.go index 86d4258..763b800 100644 --- a/array_test.go +++ b/array_test.go @@ -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")) }