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

Why does readMap return an array? #29

Closed
guybe7 opened this issue May 5, 2022 · 4 comments
Closed

Why does readMap return an array? #29

guybe7 opened this issue May 5, 2022 · 4 comments

Comments

@guybe7
Copy link

guybe7 commented May 5, 2022

first of all, I'm extremely new to golang, so sorry in advance if what I'm saying doesn't make much sense

func readMap(i *bufio.Reader) (m RedisMessage, err error) {
	length, err := readI(i)
	if err == errChunked {
		m.values, err = readE(i)
	} else {
		m.values, err = readA(i, int(length*2))
	}
	if err != nil {
		return RedisMessage{}, err
	}
	return
}

IIUC readMap will assign m.values, which is an array.
shouldn't RedisMessage have a map member?

@rueian
Copy link
Collaborator

rueian commented May 5, 2022

Hi @guybe7,

The RedisMessage is just an intermediate container. It could have a map member of course, but that would make it a bigger struct. Reusing the values field is enough for holding map response temporary.

@guybe7
Copy link
Author

guybe7 commented May 5, 2022

hi @rueian thanks for the quick reply.

i guess my question is: if I execute Hgetall, will the returned reply be of type map?

@rueian
Copy link
Collaborator

rueian commented May 5, 2022

You can use RedisMessage.AsStrMap to get a map:

c, _ := rueidis.NewClient(rueidis.ClientOption{InitAddress: []string{"127.0.0.1:6377"}})
defer c.Close()

m, _ := c.Do(context.Background(), c.B().Hgetall().Key("key").Build()).AsStrMap()

for key, value := range m {
	fmt.Println(key, value)
}

Error handling is omitted.

@guybe7
Copy link
Author

guybe7 commented May 5, 2022

thanks!

@guybe7 guybe7 closed this as completed May 5, 2022
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