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

fix(scan): update setupValuerAndSetter to use default values when pointer t… #6938

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Waldeedle
Copy link

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

Addresses #6819
Adding the else condition to use the base type when data is nil ensures that the struct field is always set by the scanIntoStructMethod

User Case Description

It fixes the example from the docs where you initialize the var once:

    var datasetRow T
    for rows.Next() {
        err := db.ScanRows(rows, &datasetRow)

@Waldeedle Waldeedle force-pushed the master branch 2 times, most recently from c1afb54 to 88330cb Compare April 3, 2024 20:58
@Waldeedle Waldeedle marked this pull request as draft April 4, 2024 01:52
@Waldeedle Waldeedle marked this pull request as ready for review April 4, 2024 11:28
@Waldeedle Waldeedle marked this pull request as draft April 4, 2024 11:33
@Waldeedle Waldeedle marked this pull request as ready for review April 4, 2024 11:44
@Waldeedle Waldeedle changed the title fix: update setupValuerAndSetter to use default values when pointer t… fix(scan): update setupValuerAndSetter to use default values when pointer t… Apr 4, 2024
@Waldeedle
Copy link
Author

@jinzhu any concerns with this PR?

@Waldeedle
Copy link
Author

@a631807682 am I missing something for this to be reviewed?

fieldIsEmbeddedPointerTypeStruct := len(field.BindNames) > 1 && len(field.StructField.Index) > 0 && field.StructField.Index[0] < 0
fieldValue := reflect.ValueOf(values[idx]).Elem()
if !fieldIsEmbeddedPointerTypeStruct && fieldValue.Kind() == reflect.Ptr && fieldValue.IsNil() {
db.AddError(field.Set(db.Statement.Context, reflectValue, field.DefaultValueInterface))
Copy link
Member

Choose a reason for hiding this comment

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

Why use field.DefaultValueInterface? I don't think it is an expected behavior for most users.

Copy link
Author

Choose a reason for hiding this comment

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

field.DefaultValueInterface ensures that I return the struct back with default/zero values for each field. i.e. nil for pointers and 0 for ints. It helps to reset the struct.

Copy link
Author

Choose a reason for hiding this comment

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

@jinzhu any concerns with that?

Copy link
Author

Choose a reason for hiding this comment

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

@jinzhu
I think it is expected usage based on your docs: https://gorm.io/docs/sql_builder.html#Scan-sql-Rows-into-struct

var user User
for rows.Next() {
  // ScanRows scan a row into user
  db.ScanRows(rows, &user)

  // do something
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants