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

Array of alternating values to a Struct/Map #40

Closed
sinni800 opened this issue Sep 23, 2013 · 6 comments
Closed

Array of alternating values to a Struct/Map #40

sinni800 opened this issue Sep 23, 2013 · 6 comments

Comments

@sinni800
Copy link

There's, IMO, another type of "Multi Bulk Reply" you'd have to consider: Something a "SORT" would return.

Example:

SORT something GET otherkey:*->id GET otherkey:*->title

This would return alternating IDs and titles.

A call to a helper function for these alternations could be:

redis.AlternatingMultiBulkReply(src, &deststruct, "id", "title")

(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!

@garyburd
Copy link
Member

See http://godoc.org/github.com/garyburd/redigo/redis#example-Scan

for len(values) > 0 {
    var destruct desttype
    values, err = redis.Scan(values, &deststruct.id, &deststruct.title)
    if err != nil {
         // handle error
    }
}

@sinni800
Copy link
Author

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:

for x := 0; x < len(values)/2; x++ {
    p := desttype{}
    p.Id = values[x*3+0]
    p.Title =values[x*3+1]
    retslice = append(retslice, p)
}

@garyburd
Copy link
Member

My loop above works with multiple results. Here's an update to the code showing how to fill in retSlice

 var retSlice []desttype
 for len(values) > 0 {
    var p desttype
    values, err = redis.Scan(values, &p.Id, &p.Title)
    if err != nil {
         // handle error
   }
   retslice = append(retslice, p)
}

It's possible to create a function ScanStructSlice:

var p []desttype
err := ScanStructSlice(values, &p, "Id", "Title")

This function will use reflection where it's not otherwise needed.

@sinni800
Copy link
Author

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.

@garyburd
Copy link
Member

garyburd commented Oct 5, 2013

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?

@garyburd garyburd closed this as completed Oct 5, 2013
@sinni800
Copy link
Author

sinni800 commented Oct 7, 2013

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.

@gomodule gomodule locked and limited conversation to collaborators Dec 16, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants