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

XINFO GROUPS will crash #287

Closed
Sola1Go opened this issue Oct 28, 2021 · 5 comments
Closed

XINFO GROUPS will crash #287

Sola1Go opened this issue Oct 28, 2021 · 5 comments

Comments

@Sola1Go
Copy link

Sola1Go commented Oct 28, 2021

Unable to parse XINFO
XINFO method is not supported yet, and some difficulties have been encountered in parsing it for me.

auto res = redis->command<std::vector<std::vector<OptionalStringPair>>>("xinfo", "groups", "group_name");

I got: terminate called after throwing an instance of 'sw::redis::ProtoError'
what(): Expect ARRAY reply

auto res = redis->command<std::vector<std::vector<OptionalString>>>("xinfo", "groups", "group_name");

I got: terminate called after throwing an instance of 'sw::redis::ProtoError'
what(): Expect STRING reply
And Expect STRING reply means that the type indicated in redis.command<> is expected a string instead of the correct return is expected to be string?

Environment:

  • OS: NixOS
  • Compiler: clang 11.1.0
  • hiredis version: 1.0.0 master
  • redis-plus-plus version: 1.3.1 master

By the way
Is there a better way to judge whether a group/consumer exists?
Regards

@sewenew
Copy link
Owner

sewenew commented Oct 28, 2021

XINFO returns an array reply which can be expressed as an array of map. However the value part of the map might be string or integer.

If you are using C++17, you can use std::variant as the type of the value part (the recommended way):

using XInfoRes = std::vector<std::unordered_map<std::string, std::variant<long long, std::string>>>;

auto res = redis->command<XInfoRes>("xinfo", "groups", "group_name");

You can check this for detail.

However, if you are using an older c++ standard, you have to define XInfoRes as an array of tuple that exactly matches the returned result. You can check this for example.

Or, you can make it return a std::unique_ptr<redisReply, sw::redis::ReplyDeleter>, and parse it manually:

auto r = redis.command("xinfo", "groups", "group_name");
// parse *r manually.

Regards

@Sola1Go
Copy link
Author

Sola1Go commented Oct 29, 2021

thx~ 😆

@Sola1Go Sola1Go closed this as completed Oct 29, 2021
@Sola1Go
Copy link
Author

Sola1Go commented Aug 18, 2023

@sewenew
Starting with Redis version 7.0.0, returns of the command XINFO GROUPS have been enhanced with entries entries-read and lag.

These two keys are certain to be present in returns, but their values might be NULL.

// code:
using XInfoRes = std::vector<std::unordered_map<std::string, std::variant<long long, std::string>>>;

auto res = redis->command<XInfoRes>("xinfo", "groups", "group_name");

// crash:
// terminate called after throwing an instance of 'sw::redis::ParseError'
// what():  expect STRING or STATUS or VERB or BIGNUM reply, but got NULL reply

This is the document for XINFO GROUPS.

Regards

@Sola1Go Sola1Go reopened this Aug 18, 2023
@Sola1Go Sola1Go changed the title How to parse XINFO? XINFO GROUPS will crash Aug 18, 2023
@sewenew
Copy link
Owner

sewenew commented Aug 20, 2023

If the value of entries-read and lag might be null, you can add optional<long long> to the std::variant definition:

using XInfoRes = std::vector<std::unordered_map<std::string, std::variant<long long, std::string, std::optional<long long>>>>;

You can try the above code, and if you still get problem, feel free to let me know.

Regards

@Sola1Go
Copy link
Author

Sola1Go commented Aug 21, 2023

thx again ~
Regards

@Sola1Go Sola1Go closed this as completed Aug 21, 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