-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Array of alternating values to a Struct/Map #40
Comments
See http://godoc.org/github.com/garyburd/redigo/redis#example-Scan
|
Is there a possibility this could work if multiple results were returned form SORT? If sort returns "1Value1", "1Value2", "2Value1", "2Value2" ,"3Value1", "3Value2" for example? Currenlty I work with a for like this:
|
My loop above works with multiple results. Here's an update to the code showing how to fill in retSlice
It's possible to create a function ScanStructSlice:
This function will use reflection where it's not otherwise needed. |
Sorry that I completely overread that it scans multiple results. I don't know what the performance hit of reflect is, so would that method be a huge performance hit? It would certainly make code with lots of Redis SORTs smaller. Would it also be possible to call into maps this way? You can't get pointers of map values so scan doesn't work for this. It would only be helpful for mockups though, because usually you use a real struct. ScanStruct would benefit from being able to call into maps though, because it would allow Redis HASHes which have varying keys. Using ScanStruct with maps currently panics. |
I added http://godoc.org/github.com/garyburd/redigo/redis#ScanSlice. I did not add support for maps. Sketching out code with a map is not any easier than just using a struct. If the keys are unknown, then the application can construct a map using a for loop, map assignment and the Int, String, Bytes and Float64 functions as appropriate. ScanStruct should return an error for maps. Were you using fe36254 or later? |
Thank you for the addition, it really helped shorting my code. My code with ScanStruct with a map used a redigo revision older than the one you linked. |
There's, IMO, another type of "Multi Bulk Reply" you'd have to consider: Something a "SORT" would return.
Example:
This would return alternating IDs and titles.
A call to a helper function for these alternations could be:
(The name really is horrible and way too long. I don't know what would be better)
Id and title would represent the corresponig struct tag content, you choose them in the order they appear in the result, which also sets the amount of returned values implicitly (and thus the offset needed for the next "row" of returned contents). If the length of src is not divisable by the amount of names that were given after the second argument, the function could either just not fill the remaining spaces or fail gracefully (or both).
I would also love if this worked for Maps. I would love if everything worked for maps!
The text was updated successfully, but these errors were encountered: