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

How to convert Hmget() result to a slice of defined struct? #260

Closed
piavgh opened this issue May 26, 2023 · 2 comments
Closed

How to convert Hmget() result to a slice of defined struct? #260

piavgh opened this issue May 26, 2023 · 2 comments

Comments

@piavgh
Copy link

piavgh commented May 26, 2023

Hi @rueian ,

I'm trying to use rueidis to replace go-redis (due to this issue redis/go-redis#2046),
But at the moment, there is no documentation on how to convert a Hmget() result into a slice of defined struct

For example, I want to get the list of pools data by Hmget(), params is the list of pool addresses

cmd := r.redisClient.B().Hmget().Key(r.keyPools).Field(addresses...).Build()
	poolDataList, err := r.redisClient.Do(ctx, cmd).ToArray()
	if err != nil {
		return nil, err
	}

How to convert poolDataList to something like []Pool?
Thanks

@rueian
Copy link
Collaborator

rueian commented May 26, 2023

Hi @piavgh,

There are two options:

  1. Using the .AsStrSlice() instead of the .ToArray() to get a string slice, then you can construct your []Pool from it.
  2. Loop over the items of poolDataList and invoke .ToString() on each of them and check nil value by using rueidis.IsRedisNil(err)

Note that if you use .AsStrSlice(), the nil value will be present as an empty string in the string slice.

@piavgh
Copy link
Author

piavgh commented May 27, 2023

@rueian : thanks, now I got it

@piavgh piavgh closed this as completed May 27, 2023
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

No branches or pull requests

2 participants