diff --git a/rmutil/util.c b/rmutil/util.c index 610cfd2..f5656e9 100644 --- a/rmutil/util.c +++ b/rmutil/util.c @@ -193,4 +193,31 @@ int RMUtil_ParseArgsAfter(const char *token, RedisModuleString **argv, int argc, va_end(ap); return rc; +} + +RedisModuleCallReply *RedisModule_CallReplyArrayElementByPath( + RedisModuleCallReply *rep, const char *path) { + if (rep == NULL) return NULL; + + RedisModuleCallReply *ele = rep; + const char *s = path; + char *e; + long idx; + do { + errno = 0; + idx = strtol(s, &e, 10); + + if ((errno == ERANGE && (idx == LONG_MAX || idx == LONG_MIN)) || + (errno != 0 && idx == 0) || + (REDISMODULE_REPLY_ARRAY != RedisModule_CallReplyType(ele)) || + (s == e)) { + ele = NULL; + break; + } + s = e; + ele = RedisModule_CallReplyArrayElement(ele, idx - 1); + + } while ((ele != NULL) && (*e != '\0')); + + return ele; } \ No newline at end of file diff --git a/rmutil/util.h b/rmutil/util.h index 3927b10..867f371 100644 --- a/rmutil/util.h +++ b/rmutil/util.h @@ -94,5 +94,13 @@ int RMUtilInfo_GetString(RMUtilInfo *info, const char *key, const char **str); */ int RMUtilInfo_GetDouble(RMUtilInfo *info, const char *key, double *d); +/* +* Returns a call reply array's element given by a space-delimited path. E.g., +* the path "1 2 3" will return the 3rd element from the 2 element of the 1st +* element from an array (or NULL if not found) +*/ +RedisModuleCallReply *RedisModule_CallReplyArrayElementByPath( + RedisModuleCallReply *rep, const char *path); + #endif