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

Question passing an Int slice into WhereIn #227

Closed
fi0 opened this issue Nov 25, 2017 · 9 comments
Closed

Question passing an Int slice into WhereIn #227

fi0 opened this issue Nov 25, 2017 · 9 comments
Labels

Comments

@fi0
Copy link

fi0 commented Nov 25, 2017

how do I pass a slice into whereIn?

example
qm.WhereIn("id in ?", 1, 2, 3)

these won't work

ids := []int{1, 2, 3}
qm.WhereIn("id in ?", ids)
ids := []int{1, 2, 3}
qm.WhereIn("id in ?", ids...)
@aarondl
Copy link
Member

aarondl commented Nov 25, 2017

The bottom one should be working, can you turn on debug mode and show what kind of query or error you're getting? Need more information to determine what the problem is :)

@payaaam
Copy link

payaaam commented Nov 30, 2017

I have had problems with this too. The solution is to convert your slice to []interface{}.

Example

ids := []int{1, 2, 3}
convertedIDs := make([]interface{}, len(ids))
for index, num := range ids {
	convertedIDs[index] =  num
}

qm.WhereIn("id IN ?", convertedIDs...)

@fi0
Copy link
Author

fi0 commented Dec 2, 2017

thank you it works

@fi0 fi0 closed this as completed Dec 2, 2017
@aarondl
Copy link
Member

aarondl commented Dec 8, 2017

I actually think we should try to fix this. It shouldn't really behave like this and we may be able to do something about it.

@aarondl aarondl reopened this Dec 8, 2017
@ceshihao
Copy link
Contributor

This problem is from golang/go#16235
It seems to be fixed in go1.9 by supporting custom import types.

@aarondl aarondl added the planned label Mar 2, 2018
@aarondl
Copy link
Member

aarondl commented Jun 4, 2018

Unfortunately after looking into it, there is no way to be able to do this :(

It's possible to use arrays in general as ceshihao is suggesting, but that requires driver support and is actually usable today without any changes.
It's a Go problem that we can't convert the slices easily. :(

@aarondl aarondl closed this as completed Jun 4, 2018
@tonyhb
Copy link

tonyhb commented Feb 21, 2020

@aarondl I'm pretty sure we can fix this with a quick bit of reflection.

Right now we always expect that a user splats their variadic arg. That's the root cause of the issue.

For argument's sake, lets allow people to call WhereIn with a slice (qm.WhereIn("id IN ?", ids) where ids is a slice).

Then, we inspect our variadic argument to see if len is 1. If so, we may have a slice passed. We can then fix WhereIn as follows:

func WhereIn(clause string, args ...interface{}) QueryMod {
	if len(args) == 1 && reflect.TypeOf(args[0]).Kind() == reflect.Slice {
		val := reflect.ValueOf(args[0])
		slice := make([]interface{}, val.Len())
		for i := 0; i < val.Len(); i++ {
			slice[i] = val.Index(i)
		}
		return WhereIn(clause, slice...)
	}

	fmt.Printf("continue: %vv", args)
}

We use reflect to see if the only item in arg is a slice, and if so we handle the conversion of slice to interfaces for people automatically. Type safe, no impact, better UX.

I'll make a PR now

@tonyhb
Copy link

tonyhb commented Feb 21, 2020

Here's a quick demo: https://play.golang.org/p/0rrgz5cwFtj

@JC1738
Copy link

JC1738 commented Mar 13, 2023

Still doesn't work unless you use something like this

func ToSliceOfAny[T any](s []T) []any { result := make([]any, len(s)) for i, v := range s { result[i] = v } return result }

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

Successfully merging a pull request may close this issue.

6 participants