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

add ScanStructs, ScanVals to Scanner interface #273

Merged
merged 2 commits into from
May 20, 2021
Merged

add ScanStructs, ScanVals to Scanner interface #273

merged 2 commits into from
May 20, 2021

Conversation

vlanse
Copy link
Contributor

@vlanse vlanse commented May 10, 2021

ScanStructs, ScanVals methods are added to Scanner interface.
Rationale is to allow reuse goqu very convenient struct tags in case when goqu is used as query builder only (i.e. queries built with goqu but executed via sql module API). In my case such approach allows to make some advanced query processing, like metrics collection or scanning results on-by-one - that's why goqu executor is not always suitable.

Also, I wanted to reuse code from internal/utils as much as possible

@codecov
Copy link

codecov bot commented May 10, 2021

Codecov Report

Merging #273 (eda0d81) into master (2eb18c0) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #273      +/-   ##
==========================================
+ Coverage   96.76%   96.77%   +0.01%     
==========================================
  Files          60       60              
  Lines        3306     3319      +13     
==========================================
+ Hits         3199     3212      +13     
  Misses         91       91              
  Partials       16       16              
Flag Coverage Δ
unittests 96.77% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
exec/query_executor.go 98.48% <100.00%> (-0.34%) ⬇️
exec/scanner.go 93.54% <100.00%> (+6.88%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2eb18c0...eda0d81. Read the comment docs.

@vlanse
Copy link
Contributor Author

vlanse commented May 14, 2021

@doug-martin could you please review and merge if there are no questions?

@doug-martin
Copy link
Owner

@vlanse I'll review and release tomorrow.

Thank you for your all of your contributions!

val := reflect.ValueOf(i)
if !util.IsPointer(val.Kind()) {
return errUnsupportedScanStructsType
if _, err := checkScanStructsTarget(i); err != nil {
Copy link
Owner

Choose a reason for hiding this comment

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

Is there a reason to do this check both here and in the Scanner#ScanStructs? Same for ScanVals?

Copy link
Owner

Choose a reason for hiding this comment

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

I should clarify this, I think we should just do the check in the single place for each to keep it DRY.

Copy link
Contributor Author

@vlanse vlanse May 18, 2021

Choose a reason for hiding this comment

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

I suggest to call checkScanStructsTarget (checkScanValsTarget) only in corresponding Scanner methods because errors that cause checks to fail seem likely to not occur at runtime (they should be detected early by simple tests in fact, along with errors like invalid SQL) , so it does not matter that check will be done before actual query execution.
Will update PR appropriately

@@ -67,7 +67,14 @@ func (ds *databaseSuite) TestScanStructs() {
WithArgs().
WillReturnRows(sqlmock.NewRows([]string{"address", "name"}).
FromCSVString("111 Test Addr,Test1\n211 Test Addr,Test2"))

mock.ExpectQuery(`SELECT \* FROM "items"`).
Copy link
Owner

@doug-martin doug-martin May 19, 2021

Choose a reason for hiding this comment

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

I might be missing something but I don't see why these additional expects added here and below?

Copy link
Contributor Author

@vlanse vlanse May 19, 2021

Choose a reason for hiding this comment

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

Now targets validation is done right in ScanRows(), after query is executed by db, thus mock should expect query before error will be returned.

As I wrote above, this should not be a problem because invalid parameters for scan rows is a kind of error that should be detected by basic tests at early stages of development. So in production this error is not likely to occur.

Of course there is an alternative that keeps previous behaviour, but is seem to be ugly, like need some flag to tell scanner that validation was already done.

Copy link
Owner

Choose a reason for hiding this comment

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

@doug-martin doug-martin merged commit e403829 into doug-martin:master May 20, 2021
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.

2 participants