From fe6978a02ab92fde37f3e4b592ac60c340808f8c Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 11 Aug 2023 16:31:47 -0500 Subject: [PATCH] Test --- types/scan_value.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/types/scan_value.go b/types/scan_value.go index 9f5a7bb6..efe46874 100644 --- a/types/scan_value.go +++ b/types/scan_value.go @@ -249,16 +249,24 @@ func scanStringValue(v reflect.Value, rd Reader, n int) error { } func scanJSONValue(v reflect.Value, rd Reader, n int) error { - // Zero value so it works with SelectOrInsert. - // TODO: better handle slices - v.Set(reflect.New(v.Type()).Elem()) - + // If n is -1, there is no data to scan, so return early. if n == -1 { return nil } + // Create a new instance of the struct type that v represents. + newStruct := reflect.New(v.Type().Elem()).Interface() + + // Decode the JSON data from the reader into the new struct instance. dec := pgjson.NewDecoder(rd) - return dec.Decode(v.Addr().Interface()) + if err := dec.Decode(newStruct); err != nil { + return err + } + + // Set the scanned data to the provided reflect.Value v. + v.Set(reflect.ValueOf(newStruct).Elem()) + + return nil } func scanTimeValue(v reflect.Value, rd Reader, n int) error {