Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions util/gconv/gconv_z_unit_scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"

"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
Expand Down Expand Up @@ -367,10 +368,10 @@ func TestScan(t *testing.T) {
t.Assert(test["Name"], scanExpects.structSub.Place)
t.Assert(test["Place"], scanExpects.structSub.Name)

//t.Logf("%#v", test)
// t.Logf("%#v", test)
err = gconv.Scan(test, &scanExpects.structSubPtr, mapParameter)
t.AssertNil(err)
//t.Logf("%#v", scanExpects.structSubPtr)
// t.Logf("%#v", scanExpects.structSubPtr)
t.Assert(test["Name"], scanExpects.structSubPtr.Place)
t.Assert(test["Place"], scanExpects.structSubPtr.Name)
}
Expand Down Expand Up @@ -421,3 +422,22 @@ func TestScanEmptyStringToCustomType(t *testing.T) {
t.Assert(len(req.Types), 0)
})
}

func TestScanDeepSlice(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
req [][]int
req2 [][][]int
data1 = gjson.New("[[1,2,3],[4,5,6]]")
data2 = gjson.New("[[[1,2,3]],[[4,5,6]]]")
)
err := data1.Scan(&req)
t.AssertNil(err)
err = gconv.Scan(data1.String(), &req)
t.AssertNil(err)
err = data2.Scan(&req2)
t.AssertNil(err)
t.Assert(len(req), 2)
t.Assert(len(req2), 2)
})
}
6 changes: 5 additions & 1 deletion util/gconv/internal/converter/converter_scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ func (c *Converter) Scan(srcValue any, dstPointer any, option ...ScanOption) (er
dstElemType = dstPointerReflectValueElem.Type().Elem()
dstElemKind = dstElemType.Kind()
)

// The slice element might be a pointer type
if dstElemKind == reflect.Ptr {
dstElemType = dstElemType.Elem()
Expand Down Expand Up @@ -209,9 +210,12 @@ func (c *Converter) Scan(srcValue any, dstPointer any, option ...ScanOption) (er
}
newSlice.Index(i).SetBool(v)
default:
return c.Scan(
err = c.Scan(
srcElem, newSlice.Index(i).Addr().Interface(), option...,
)
if err != nil && !scanOption.ContinueOnError {
Copy link

Copilot AI May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the modified error handling strategy with scanOption.ContinueOnError is intentional; if errors are being skipped on purpose, consider logging or documenting this behavior to aid in debugging partial conversions.

Copilot uses AI. Check for mistakes.
return err
}
}
}
dstPointerReflectValueElem.Set(newSlice)
Expand Down
Loading