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

adding ability to change column rename function #66

Closed
wants to merge 6 commits into from

Conversation

blainehansen
Copy link

The Feature

It would be nice to be able to choose how ScanStruct(s) by default maps its fields to the table, without having to redundantly tag the fields.

A little use case

import "strings"

goqu.SetColumnRenameFunction(strings.ToUpper)

type User struct{
  FirstName string
  LastName string
}

var user User
//SELECT "FIRSTNAME", "LASTNAME" FROM "user" LIMIT 1;
found, err := db.From("user").ScanStruct(&user)

A test that isn't passing?

There's a test that isn't passing, in crud_exec_test.go, TestScanStruct, related to testEmbeddedPtrCrudActionItem. Here's the log:

--- FAIL: TestScanStruct (0.00s)
panic: reflect: reflect.Value.Set using value obtained using unexported field [recovered]
	panic: reflect: reflect.Value.Set using value obtained using unexported field

goroutine 50 [running]:
testing.tRunner.func1(0xc4200f4000)
	/usr/local/go/src/testing/testing.go:742 +0x567
panic(0x9d02e0, 0xc420097060)
	/usr/local/go/src/runtime/panic.go:502 +0x24a
reflect.flag.mustBeAssignable(0x1d6)
	/usr/local/go/src/reflect/value.go:231 +0x23c
reflect.Value.Set(0x9bd060, 0xc42000c480, 0x1d6, 0x9bd060, 0xc42000c4a0, 0x16)
	/usr/local/go/src/reflect/value.go:1367 +0x3d
gopkg.in/doug-martin/goqu%2ev4.initEmbeddedPtr(0xa1ab40, 0xc42000c480, 0x199)
	/go/src/gopkg.in/doug-martin/goqu.v4/crud_exec.go:273 +0x234
gopkg.in/doug-martin/goqu%2ev4.assignVals(0x9bd0e0, 0xc42000c480, 0xc42000e088, 0x1, 0x1, 0xc420264510, 0x0, 0x1)
	/go/src/gopkg.in/doug-martin/goqu.v4/crud_exec.go:227 +0x7bc
gopkg.in/doug-martin/goqu%2ev4.CrudExec.scan(0xae6ec0, 0xc420264390, 0xa65f45, 0x15, 0x0, 0x0, 0x0, 0x0, 0x0, 0x9bd0e0, ...)
	/go/src/gopkg.in/doug-martin/goqu.v4/crud_exec.go:216 +0x88a
gopkg.in/doug-martin/goqu%2ev4.CrudExec.ScanStruct(0xae6ec0, 0xc420264390, 0xa65f45, 0x15, 0x0, 0x0, 0x0, 0x0, 0x0, 0x9bd0e0, ...)
	/go/src/gopkg.in/doug-martin/goqu.v4/crud_exec.go:88 +0x1ca
gopkg.in/doug-martin/goqu%2ev4.(*crudExecTest).TestScanStruct(0xc420124d20)
	/go/src/gopkg.in/doug-martin/goqu.v4/crud_exec_test.go:221 +0x15aa
reflect.Value.call(0xc4203c8a20, 0xc4200a4ae8, 0x13, 0xa603a7, 0x4, 0xc420057f50, 0x1, 0x1, 0xa553c0, 0xc420124d20, ...)
	/usr/local/go/src/reflect/value.go:447 +0xa92
reflect.Value.Call(0xc4203c8a20, 0xc4200a4ae8, 0x13, 0xc420057f50, 0x1, 0x1, 0x10319358, 0x46488b, 0x404b30)
	/usr/local/go/src/reflect/value.go:308 +0xc1
gopkg.in/doug-martin/goqu.v4/vendor/github.com/c2fo/testify/suite.Run.func2(0xc4200f4000)
	/go/src/gopkg.in/doug-martin/goqu.v4/vendor/github.com/c2fo/testify/suite/suite.go:94 +0x1ff
testing.tRunner(0xc4200f4000, 0xc420416b60)
	/usr/local/go/src/testing/testing.go:777 +0x16e
created by testing.(*T).Run
	/usr/local/go/src/testing/testing.go:824 +0x565
FAIL	gopkg.in/doug-martin/goqu.v4	0.024s

It's not passing in git commit cd495d70a4ff280202e22a09de9502a3fe0b66cb, which is master's current state, at least in my checkout when I run GO_VERSION=latest docker-compose run goqu.

Hopefully we can still consider my feature addition :)

@doug-martin
Copy link
Owner

@blainehansen there are some merge conflicts if yo can get those addressed Ill merge and release.

@blainehansen
Copy link
Author

There you go 😄

assert.Contains(t, lowerKeys, "lastlower")

// changing rename function
SetColumnRenameFunction(strings.ToUpper)
Copy link
Owner

Choose a reason for hiding this comment

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

I believe the tests are failing because of this line. I think this exposes what maybe a larger concern which is that this is changing the functionality at a global level. Im starting to think this should be provided as an argument and something that the dataset maintains so you could have different instances of datasets each with potentially different transform methods, that also help prevent tests from inadvertently leak into other tests.

What do you think?

Copy link
Author

Choose a reason for hiding this comment

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

That's actually what I hoped to do initially, to have this whole thing be more "object-oriented", but since the createColumnMap method is pretty divorced from a dataset or database, and it seems like it would take a bit of finagling to pass a relevant object down through all the layers. But we could probably figure it out 😄

@codecov
Copy link

codecov bot commented Oct 1, 2018

Codecov Report

Merging #66 into master will increase coverage by 0.05%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #66      +/-   ##
==========================================
+ Coverage   80.72%   80.78%   +0.05%     
==========================================
  Files          17       17              
  Lines        2169     2175       +6     
==========================================
+ Hits         1751     1757       +6     
  Misses        310      310              
  Partials      108      108
Impacted Files Coverage Δ
crud_exec.go 87.08% <100%> (+0.12%) ⬆️
default_adapter.go 82.39% <100%> (+0.12%) ⬆️

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 6803bc2...3dff505. Read the comment docs.

@doug-martin doug-martin mentioned this pull request Jun 8, 2019
@doug-martin
Copy link
Owner

Fixed merge conflicts and reopened with a new PR #83 Ill release this with v6.1.0

@doug-martin doug-martin closed this Jun 8, 2019
@doug-martin doug-martin mentioned this pull request Jun 9, 2019
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