Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new instance of the struct type for scanjsonvalue #1979 #1984

Open
wants to merge 1 commit into
base: v10
Choose a base branch
from
Open
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
18 changes: 13 additions & 5 deletions types/scan_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading