2021-08-03 08:37:19 +00:00
|
|
|
/*
|
|
|
|
* A module the tests RM_ReplyWith family of commands
|
|
|
|
*/
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
#include "valkeymodule.h"
|
2022-11-13 11:12:22 +00:00
|
|
|
#include <math.h>
|
2021-08-03 08:37:19 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
int rw_string(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
|
|
|
|
if (argc != 2) return ValkeyModule_WrongArity(ctx);
|
2021-08-03 08:37:19 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
return ValkeyModule_ReplyWithString(ctx, argv[1]);
|
2021-08-03 08:37:19 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
int rw_cstring(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
|
|
|
|
VALKEYMODULE_NOT_USED(argv);
|
|
|
|
if (argc != 1) return ValkeyModule_WrongArity(ctx);
|
2021-08-03 08:37:19 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
return ValkeyModule_ReplyWithSimpleString(ctx, "A simple string");
|
2021-08-03 08:37:19 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
int rw_int(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
|
|
|
|
if (argc != 2) return ValkeyModule_WrongArity(ctx);
|
2021-08-03 08:37:19 +00:00
|
|
|
|
|
|
|
long long integer;
|
2024-04-23 15:55:44 +00:00
|
|
|
if (ValkeyModule_StringToLongLong(argv[1], &integer) != VALKEYMODULE_OK)
|
|
|
|
return ValkeyModule_ReplyWithError(ctx, "Arg cannot be parsed as an integer");
|
2021-08-03 08:37:19 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
return ValkeyModule_ReplyWithLongLong(ctx, integer);
|
2021-08-03 08:37:19 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 11:12:22 +00:00
|
|
|
/* When one argument is given, it is returned as a double,
|
|
|
|
* when two arguments are given, it returns a/b. */
|
2024-04-23 15:55:44 +00:00
|
|
|
int rw_double(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
|
2022-11-13 11:12:22 +00:00
|
|
|
if (argc==1)
|
2024-04-23 15:55:44 +00:00
|
|
|
return ValkeyModule_ReplyWithDouble(ctx, NAN);
|
2022-11-13 11:12:22 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
if (argc != 2 && argc != 3) return ValkeyModule_WrongArity(ctx);
|
2021-08-03 08:37:19 +00:00
|
|
|
|
2022-11-13 11:12:22 +00:00
|
|
|
double dbl, dbl2;
|
2024-04-23 15:55:44 +00:00
|
|
|
if (ValkeyModule_StringToDouble(argv[1], &dbl) != VALKEYMODULE_OK)
|
|
|
|
return ValkeyModule_ReplyWithError(ctx, "Arg cannot be parsed as a double");
|
2022-11-13 11:12:22 +00:00
|
|
|
if (argc == 3) {
|
2024-04-23 15:55:44 +00:00
|
|
|
if (ValkeyModule_StringToDouble(argv[2], &dbl2) != VALKEYMODULE_OK)
|
|
|
|
return ValkeyModule_ReplyWithError(ctx, "Arg cannot be parsed as a double");
|
2022-11-13 11:12:22 +00:00
|
|
|
dbl /= dbl2;
|
|
|
|
}
|
2021-08-03 08:37:19 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
return ValkeyModule_ReplyWithDouble(ctx, dbl);
|
2021-08-03 08:37:19 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
int rw_longdouble(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
|
|
|
|
if (argc != 2) return ValkeyModule_WrongArity(ctx);
|
2021-08-03 08:37:19 +00:00
|
|
|
|
|
|
|
long double longdbl;
|
2024-04-23 15:55:44 +00:00
|
|
|
if (ValkeyModule_StringToLongDouble(argv[1], &longdbl) != VALKEYMODULE_OK)
|
|
|
|
return ValkeyModule_ReplyWithError(ctx, "Arg cannot be parsed as a double");
|
2021-08-03 08:37:19 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
return ValkeyModule_ReplyWithLongDouble(ctx, longdbl);
|
2021-08-03 08:37:19 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
int rw_bignumber(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
|
|
|
|
if (argc != 2) return ValkeyModule_WrongArity(ctx);
|
2021-10-25 08:31:20 +00:00
|
|
|
|
|
|
|
size_t bignum_len;
|
2024-04-23 15:55:44 +00:00
|
|
|
const char *bignum_str = ValkeyModule_StringPtrLen(argv[1], &bignum_len);
|
2021-10-25 08:31:20 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
return ValkeyModule_ReplyWithBigNumber(ctx, bignum_str, bignum_len);
|
2021-10-25 08:31:20 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
int rw_array(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
|
|
|
|
if (argc != 2) return ValkeyModule_WrongArity(ctx);
|
2021-08-03 08:37:19 +00:00
|
|
|
|
|
|
|
long long integer;
|
2024-04-23 15:55:44 +00:00
|
|
|
if (ValkeyModule_StringToLongLong(argv[1], &integer) != VALKEYMODULE_OK)
|
|
|
|
return ValkeyModule_ReplyWithError(ctx, "Arg cannot be parsed as a integer");
|
2021-08-03 08:37:19 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_ReplyWithArray(ctx, integer);
|
2021-08-03 08:37:19 +00:00
|
|
|
for (int i = 0; i < integer; ++i) {
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_ReplyWithLongLong(ctx, i);
|
2021-08-03 08:37:19 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
return VALKEYMODULE_OK;
|
2021-08-03 08:37:19 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
int rw_map(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
|
|
|
|
if (argc != 2) return ValkeyModule_WrongArity(ctx);
|
2021-08-03 08:37:19 +00:00
|
|
|
|
|
|
|
long long integer;
|
2024-04-23 15:55:44 +00:00
|
|
|
if (ValkeyModule_StringToLongLong(argv[1], &integer) != VALKEYMODULE_OK)
|
|
|
|
return ValkeyModule_ReplyWithError(ctx, "Arg cannot be parsed as a integer");
|
2021-08-03 08:37:19 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_ReplyWithMap(ctx, integer);
|
2021-08-03 08:37:19 +00:00
|
|
|
for (int i = 0; i < integer; ++i) {
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_ReplyWithLongLong(ctx, i);
|
|
|
|
ValkeyModule_ReplyWithDouble(ctx, i * 1.5);
|
2021-08-03 08:37:19 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
return VALKEYMODULE_OK;
|
2021-08-03 08:37:19 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
int rw_set(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
|
|
|
|
if (argc != 2) return ValkeyModule_WrongArity(ctx);
|
2021-08-03 08:37:19 +00:00
|
|
|
|
|
|
|
long long integer;
|
2024-04-23 15:55:44 +00:00
|
|
|
if (ValkeyModule_StringToLongLong(argv[1], &integer) != VALKEYMODULE_OK)
|
|
|
|
return ValkeyModule_ReplyWithError(ctx, "Arg cannot be parsed as a integer");
|
2021-08-03 08:37:19 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_ReplyWithSet(ctx, integer);
|
2021-08-03 08:37:19 +00:00
|
|
|
for (int i = 0; i < integer; ++i) {
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_ReplyWithLongLong(ctx, i);
|
2021-08-03 08:37:19 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
return VALKEYMODULE_OK;
|
2021-08-03 08:37:19 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
int rw_attribute(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
|
|
|
|
if (argc != 2) return ValkeyModule_WrongArity(ctx);
|
2021-08-03 08:37:19 +00:00
|
|
|
|
|
|
|
long long integer;
|
2024-04-23 15:55:44 +00:00
|
|
|
if (ValkeyModule_StringToLongLong(argv[1], &integer) != VALKEYMODULE_OK)
|
|
|
|
return ValkeyModule_ReplyWithError(ctx, "Arg cannot be parsed as a integer");
|
2021-08-03 08:37:19 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
if (ValkeyModule_ReplyWithAttribute(ctx, integer) != VALKEYMODULE_OK) {
|
|
|
|
return ValkeyModule_ReplyWithError(ctx, "Attributes aren't supported by RESP 2");
|
2021-08-03 08:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < integer; ++i) {
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_ReplyWithLongLong(ctx, i);
|
|
|
|
ValkeyModule_ReplyWithDouble(ctx, i * 1.5);
|
2021-08-03 08:37:19 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_ReplyWithSimpleString(ctx, "OK");
|
|
|
|
return VALKEYMODULE_OK;
|
2021-08-03 08:37:19 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
int rw_bool(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
|
|
|
|
VALKEYMODULE_NOT_USED(argv);
|
|
|
|
if (argc != 1) return ValkeyModule_WrongArity(ctx);
|
2021-08-03 08:37:19 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_ReplyWithArray(ctx, 2);
|
|
|
|
ValkeyModule_ReplyWithBool(ctx, 0);
|
|
|
|
return ValkeyModule_ReplyWithBool(ctx, 1);
|
2021-08-03 08:37:19 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
int rw_null(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
|
|
|
|
VALKEYMODULE_NOT_USED(argv);
|
|
|
|
if (argc != 1) return ValkeyModule_WrongArity(ctx);
|
2021-08-03 08:37:19 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
return ValkeyModule_ReplyWithNull(ctx);
|
2021-08-03 08:37:19 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
int rw_error(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
|
|
|
|
VALKEYMODULE_NOT_USED(argv);
|
|
|
|
if (argc != 1) return ValkeyModule_WrongArity(ctx);
|
2021-08-03 08:37:19 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
return ValkeyModule_ReplyWithError(ctx, "An error");
|
2021-08-03 08:37:19 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
int rw_error_format(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
|
|
|
|
if (argc != 3) return ValkeyModule_WrongArity(ctx);
|
2023-04-12 07:11:29 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
return ValkeyModule_ReplyWithErrorFormat(ctx,
|
|
|
|
ValkeyModule_StringPtrLen(argv[1], NULL),
|
|
|
|
ValkeyModule_StringPtrLen(argv[2], NULL));
|
2023-04-12 07:11:29 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
int rw_verbatim(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
|
|
|
|
if (argc != 2) return ValkeyModule_WrongArity(ctx);
|
2021-08-03 08:37:19 +00:00
|
|
|
|
2021-10-25 08:31:20 +00:00
|
|
|
size_t verbatim_len;
|
2024-04-23 15:55:44 +00:00
|
|
|
const char *verbatim_str = ValkeyModule_StringPtrLen(argv[1], &verbatim_len);
|
|
|
|
|
|
|
|
return ValkeyModule_ReplyWithVerbatimString(ctx, verbatim_str, verbatim_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ValkeyModule_OnLoad(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
|
|
|
|
VALKEYMODULE_NOT_USED(argv);
|
|
|
|
VALKEYMODULE_NOT_USED(argc);
|
|
|
|
if (ValkeyModule_Init(ctx, "replywith", 1, VALKEYMODULE_APIVER_1) != VALKEYMODULE_OK)
|
|
|
|
return VALKEYMODULE_ERR;
|
|
|
|
|
|
|
|
if (ValkeyModule_CreateCommand(ctx,"rw.string",rw_string,"",0,0,0) != VALKEYMODULE_OK)
|
|
|
|
return VALKEYMODULE_ERR;
|
|
|
|
if (ValkeyModule_CreateCommand(ctx,"rw.cstring",rw_cstring,"",0,0,0) != VALKEYMODULE_OK)
|
|
|
|
return VALKEYMODULE_ERR;
|
|
|
|
if (ValkeyModule_CreateCommand(ctx,"rw.bignumber",rw_bignumber,"",0,0,0) != VALKEYMODULE_OK)
|
|
|
|
return VALKEYMODULE_ERR;
|
|
|
|
if (ValkeyModule_CreateCommand(ctx,"rw.int",rw_int,"",0,0,0) != VALKEYMODULE_OK)
|
|
|
|
return VALKEYMODULE_ERR;
|
|
|
|
if (ValkeyModule_CreateCommand(ctx,"rw.double",rw_double,"",0,0,0) != VALKEYMODULE_OK)
|
|
|
|
return VALKEYMODULE_ERR;
|
|
|
|
if (ValkeyModule_CreateCommand(ctx,"rw.longdouble",rw_longdouble,"",0,0,0) != VALKEYMODULE_OK)
|
|
|
|
return VALKEYMODULE_ERR;
|
|
|
|
if (ValkeyModule_CreateCommand(ctx,"rw.array",rw_array,"",0,0,0) != VALKEYMODULE_OK)
|
|
|
|
return VALKEYMODULE_ERR;
|
|
|
|
if (ValkeyModule_CreateCommand(ctx,"rw.map",rw_map,"",0,0,0) != VALKEYMODULE_OK)
|
|
|
|
return VALKEYMODULE_ERR;
|
|
|
|
if (ValkeyModule_CreateCommand(ctx,"rw.attribute",rw_attribute,"",0,0,0) != VALKEYMODULE_OK)
|
|
|
|
return VALKEYMODULE_ERR;
|
|
|
|
if (ValkeyModule_CreateCommand(ctx,"rw.set",rw_set,"",0,0,0) != VALKEYMODULE_OK)
|
|
|
|
return VALKEYMODULE_ERR;
|
|
|
|
if (ValkeyModule_CreateCommand(ctx,"rw.bool",rw_bool,"",0,0,0) != VALKEYMODULE_OK)
|
|
|
|
return VALKEYMODULE_ERR;
|
|
|
|
if (ValkeyModule_CreateCommand(ctx,"rw.null",rw_null,"",0,0,0) != VALKEYMODULE_OK)
|
|
|
|
return VALKEYMODULE_ERR;
|
|
|
|
if (ValkeyModule_CreateCommand(ctx,"rw.error",rw_error,"",0,0,0) != VALKEYMODULE_OK)
|
|
|
|
return VALKEYMODULE_ERR;
|
|
|
|
if (ValkeyModule_CreateCommand(ctx,"rw.error_format",rw_error_format,"",0,0,0) != VALKEYMODULE_OK)
|
|
|
|
return VALKEYMODULE_ERR;
|
|
|
|
if (ValkeyModule_CreateCommand(ctx,"rw.verbatim",rw_verbatim,"",0,0,0) != VALKEYMODULE_OK)
|
|
|
|
return VALKEYMODULE_ERR;
|
|
|
|
|
|
|
|
return VALKEYMODULE_OK;
|
2021-08-03 08:37:19 +00:00
|
|
|
}
|