From 3915bee7b837fe08e0f4b447ef7c8c45b6d04f82 Mon Sep 17 00:00:00 2001 From: what Date: Mon, 12 Jan 2026 15:04:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20Slice=E6=96=B9=E6=B3=95=E6=94=AF?= =?UTF-8?q?=E6=8C=81JSON=E5=AD=97=E7=AC=A6=E4=B8=B2=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 当Slice()方法遇到字符串类型且内容以'['开头时,会自动尝试JSON解析为切片。 这使得处理JSON格式的数组字符串更加便捷。 --- rfx.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/rfx.go b/rfx.go index 4e980f8..12d579a 100644 --- a/rfx.go +++ b/rfx.go @@ -885,7 +885,7 @@ func (r *rfx) StringMap() map[string]any { } // Slice 将当前值转换为 []any 切片 -func (r *rfx) Slice() []any { +func (r *rfx) Slice() (result []any) { v := r.value for v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface { if v.IsNil() { @@ -893,11 +893,20 @@ func (r *rfx) Slice() []any { } v = v.Elem() } - if !v.IsValid() || (v.Kind() != reflect.Slice && v.Kind() != reflect.Array) { + + if !v.IsValid() { + return nil + } else if v.Kind() == reflect.String { + if str := r.String(); len(str) >= 2 && str[0] == '[' { + if err := json.Unmarshal([]byte(str), &result); err == nil { + return result + } + } + } else if v.Kind() != reflect.Slice && v.Kind() != reflect.Array { return nil } - result := make([]any, v.Len()) + result = make([]any, v.Len()) for i := 0; i < v.Len(); i++ { elem := v.Index(i) for elem.Kind() == reflect.Ptr || elem.Kind() == reflect.Interface {