reply.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * A module the tests RM_ReplyWith family of commands
  3. */
  4. #include "redismodule.h"
  5. int rw_string(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  6. if (argc != 2) return RedisModule_WrongArity(ctx);
  7. return RedisModule_ReplyWithString(ctx, argv[1]);
  8. }
  9. int rw_cstring(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  10. REDISMODULE_NOT_USED(argv);
  11. if (argc != 1) return RedisModule_WrongArity(ctx);
  12. return RedisModule_ReplyWithSimpleString(ctx, "A simple string");
  13. }
  14. int rw_int(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  15. if (argc != 2) return RedisModule_WrongArity(ctx);
  16. long long integer;
  17. if (RedisModule_StringToLongLong(argv[1], &integer) != REDISMODULE_OK)
  18. return RedisModule_ReplyWithError(ctx, "Arg cannot be parsed as an integer");
  19. return RedisModule_ReplyWithLongLong(ctx, integer);
  20. }
  21. int rw_double(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  22. if (argc != 2) return RedisModule_WrongArity(ctx);
  23. double dbl;
  24. if (RedisModule_StringToDouble(argv[1], &dbl) != REDISMODULE_OK)
  25. return RedisModule_ReplyWithError(ctx, "Arg cannot be parsed as a double");
  26. return RedisModule_ReplyWithDouble(ctx, dbl);
  27. }
  28. int rw_longdouble(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  29. if (argc != 2) return RedisModule_WrongArity(ctx);
  30. long double longdbl;
  31. if (RedisModule_StringToLongDouble(argv[1], &longdbl) != REDISMODULE_OK)
  32. return RedisModule_ReplyWithError(ctx, "Arg cannot be parsed as a double");
  33. return RedisModule_ReplyWithLongDouble(ctx, longdbl);
  34. }
  35. int rw_bignumber(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  36. if (argc != 2) return RedisModule_WrongArity(ctx);
  37. size_t bignum_len;
  38. const char *bignum_str = RedisModule_StringPtrLen(argv[1], &bignum_len);
  39. return RedisModule_ReplyWithBigNumber(ctx, bignum_str, bignum_len);
  40. }
  41. int rw_array(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  42. if (argc != 2) return RedisModule_WrongArity(ctx);
  43. long long integer;
  44. if (RedisModule_StringToLongLong(argv[1], &integer) != REDISMODULE_OK)
  45. return RedisModule_ReplyWithError(ctx, "Arg cannot be parsed as a integer");
  46. RedisModule_ReplyWithArray(ctx, integer);
  47. for (int i = 0; i < integer; ++i) {
  48. RedisModule_ReplyWithLongLong(ctx, i);
  49. }
  50. return REDISMODULE_OK;
  51. }
  52. int rw_map(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  53. if (argc != 2) return RedisModule_WrongArity(ctx);
  54. long long integer;
  55. if (RedisModule_StringToLongLong(argv[1], &integer) != REDISMODULE_OK)
  56. return RedisModule_ReplyWithError(ctx, "Arg cannot be parsed as a integer");
  57. RedisModule_ReplyWithMap(ctx, integer);
  58. for (int i = 0; i < integer; ++i) {
  59. RedisModule_ReplyWithLongLong(ctx, i);
  60. RedisModule_ReplyWithDouble(ctx, i * 1.5);
  61. }
  62. return REDISMODULE_OK;
  63. }
  64. int rw_set(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  65. if (argc != 2) return RedisModule_WrongArity(ctx);
  66. long long integer;
  67. if (RedisModule_StringToLongLong(argv[1], &integer) != REDISMODULE_OK)
  68. return RedisModule_ReplyWithError(ctx, "Arg cannot be parsed as a integer");
  69. RedisModule_ReplyWithSet(ctx, integer);
  70. for (int i = 0; i < integer; ++i) {
  71. RedisModule_ReplyWithLongLong(ctx, i);
  72. }
  73. return REDISMODULE_OK;
  74. }
  75. int rw_attribute(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  76. if (argc != 2) return RedisModule_WrongArity(ctx);
  77. long long integer;
  78. if (RedisModule_StringToLongLong(argv[1], &integer) != REDISMODULE_OK)
  79. return RedisModule_ReplyWithError(ctx, "Arg cannot be parsed as a integer");
  80. if (RedisModule_ReplyWithAttribute(ctx, integer) != REDISMODULE_OK) {
  81. return RedisModule_ReplyWithError(ctx, "Attributes aren't supported by RESP 2");
  82. }
  83. for (int i = 0; i < integer; ++i) {
  84. RedisModule_ReplyWithLongLong(ctx, i);
  85. RedisModule_ReplyWithDouble(ctx, i * 1.5);
  86. }
  87. RedisModule_ReplyWithSimpleString(ctx, "OK");
  88. return REDISMODULE_OK;
  89. }
  90. int rw_bool(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  91. REDISMODULE_NOT_USED(argv);
  92. if (argc != 1) return RedisModule_WrongArity(ctx);
  93. RedisModule_ReplyWithArray(ctx, 2);
  94. RedisModule_ReplyWithBool(ctx, 0);
  95. return RedisModule_ReplyWithBool(ctx, 1);
  96. }
  97. int rw_null(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  98. REDISMODULE_NOT_USED(argv);
  99. if (argc != 1) return RedisModule_WrongArity(ctx);
  100. return RedisModule_ReplyWithNull(ctx);
  101. }
  102. int rw_error(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  103. REDISMODULE_NOT_USED(argv);
  104. if (argc != 1) return RedisModule_WrongArity(ctx);
  105. return RedisModule_ReplyWithError(ctx, "An error");
  106. }
  107. int rw_verbatim(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  108. if (argc != 2) return RedisModule_WrongArity(ctx);
  109. size_t verbatim_len;
  110. const char *verbatim_str = RedisModule_StringPtrLen(argv[1], &verbatim_len);
  111. return RedisModule_ReplyWithVerbatimString(ctx, verbatim_str, verbatim_len);
  112. }
  113. int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  114. REDISMODULE_NOT_USED(argv);
  115. REDISMODULE_NOT_USED(argc);
  116. if (RedisModule_Init(ctx, "replywith", 1, REDISMODULE_APIVER_1) != REDISMODULE_OK)
  117. return REDISMODULE_ERR;
  118. if (RedisModule_CreateCommand(ctx,"rw.string",rw_string,"",0,0,0) != REDISMODULE_OK)
  119. return REDISMODULE_ERR;
  120. if (RedisModule_CreateCommand(ctx,"rw.cstring",rw_cstring,"",0,0,0) != REDISMODULE_OK)
  121. return REDISMODULE_ERR;
  122. if (RedisModule_CreateCommand(ctx,"rw.bignumber",rw_bignumber,"",0,0,0) != REDISMODULE_OK)
  123. return REDISMODULE_ERR;
  124. if (RedisModule_CreateCommand(ctx,"rw.int",rw_int,"",0,0,0) != REDISMODULE_OK)
  125. return REDISMODULE_ERR;
  126. if (RedisModule_CreateCommand(ctx,"rw.double",rw_double,"",0,0,0) != REDISMODULE_OK)
  127. return REDISMODULE_ERR;
  128. if (RedisModule_CreateCommand(ctx,"rw.longdouble",rw_longdouble,"",0,0,0) != REDISMODULE_OK)
  129. return REDISMODULE_ERR;
  130. if (RedisModule_CreateCommand(ctx,"rw.array",rw_array,"",0,0,0) != REDISMODULE_OK)
  131. return REDISMODULE_ERR;
  132. if (RedisModule_CreateCommand(ctx,"rw.map",rw_map,"",0,0,0) != REDISMODULE_OK)
  133. return REDISMODULE_ERR;
  134. if (RedisModule_CreateCommand(ctx,"rw.attribute",rw_attribute,"",0,0,0) != REDISMODULE_OK)
  135. return REDISMODULE_ERR;
  136. if (RedisModule_CreateCommand(ctx,"rw.set",rw_set,"",0,0,0) != REDISMODULE_OK)
  137. return REDISMODULE_ERR;
  138. if (RedisModule_CreateCommand(ctx,"rw.bool",rw_bool,"",0,0,0) != REDISMODULE_OK)
  139. return REDISMODULE_ERR;
  140. if (RedisModule_CreateCommand(ctx,"rw.null",rw_null,"",0,0,0) != REDISMODULE_OK)
  141. return REDISMODULE_ERR;
  142. if (RedisModule_CreateCommand(ctx,"rw.error",rw_error,"",0,0,0) != REDISMODULE_OK)
  143. return REDISMODULE_ERR;
  144. if (RedisModule_CreateCommand(ctx,"rw.verbatim",rw_verbatim,"",0,0,0) != REDISMODULE_OK)
  145. return REDISMODULE_ERR;
  146. return REDISMODULE_OK;
  147. }