diff --git a/rmutil/util.c b/rmutil/util.c index c810a2d..bd5f1b6 100644 --- a/rmutil/util.c +++ b/rmutil/util.c @@ -231,3 +231,18 @@ RedisModuleCallReply *RedisModule_CallReplyArrayElementByPath(RedisModuleCallRep return ele; } + +int RedisModule_TryGetValue(RedisModuleKey *key, const RedisModuleType *type, void **out) { + if (key == NULL) { + return RMUTIL_VALUE_MISSING; + } + int keytype = RedisModule_KeyType(key); + if (keytype == REDISMODULE_KEYTYPE_EMPTY) { + return RMUTIL_VALUE_EMPTY; + } else if (keytype == REDISMODULE_KEYTYPE_MODULE && RedisModule_ModuleTypeGetType(key) == type) { + *out = RedisModule_ModuleTypeGetValue(key); + return RMUTIL_VALUE_OK; + } else { + return RMUTIL_VALUE_MISMATCH; + } +} \ No newline at end of file diff --git a/rmutil/util.h b/rmutil/util.h index 46bd918..3b3be76 100644 --- a/rmutil/util.h +++ b/rmutil/util.h @@ -111,4 +111,23 @@ int RMUtilInfo_GetDouble(RMUtilInfo *info, const char *key, double *d); RedisModuleCallReply *RedisModule_CallReplyArrayElementByPath(RedisModuleCallReply *rep, const char *path); +/** + * Extract the module type from an opened key. + */ +typedef enum { + RMUTIL_VALUE_OK = 0, + RMUTIL_VALUE_MISSING, + RMUTIL_VALUE_EMPTY, + RMUTIL_VALUE_MISMATCH +} RMUtil_TryGetValueStatus; + +/** + * Tries to extract the module-specific type from the value. + * @param key an opened key (may be null) + * @param type the pointer to the type to match to + * @param[out] out if the value is present, will be set to it. + * @return a value in the @ref RMUtil_TryGetValueStatus enum. + */ +int RedisModule_TryGetValue(RedisModuleKey *key, const RedisModuleType *type, void **out); + #endif