1
0

reply.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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_array(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  36. if (argc != 2) return RedisModule_WrongArity(ctx);
  37. long long integer;
  38. if (RedisModule_StringToLongLong(argv[1], &integer) != REDISMODULE_OK)
  39. return RedisModule_ReplyWithError(ctx, "Arg cannot be parsed as a integer");
  40. RedisModule_ReplyWithArray(ctx, integer);
  41. for (int i = 0; i < integer; ++i) {
  42. RedisModule_ReplyWithLongLong(ctx, i);
  43. }
  44. return REDISMODULE_OK;
  45. }
  46. int rw_map(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  47. if (argc != 2) return RedisModule_WrongArity(ctx);
  48. long long integer;
  49. if (RedisModule_StringToLongLong(argv[1], &integer) != REDISMODULE_OK)
  50. return RedisModule_ReplyWithError(ctx, "Arg cannot be parsed as a integer");
  51. RedisModule_ReplyWithMap(ctx, integer);
  52. for (int i = 0; i < integer; ++i) {
  53. RedisModule_ReplyWithLongLong(ctx, i);
  54. RedisModule_ReplyWithDouble(ctx, i * 1.5);
  55. }
  56. return REDISMODULE_OK;
  57. }
  58. int rw_set(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  59. if (argc != 2) return RedisModule_WrongArity(ctx);
  60. long long integer;
  61. if (RedisModule_StringToLongLong(argv[1], &integer) != REDISMODULE_OK)
  62. return RedisModule_ReplyWithError(ctx, "Arg cannot be parsed as a integer");
  63. RedisModule_ReplyWithSet(ctx, integer);
  64. for (int i = 0; i < integer; ++i) {
  65. RedisModule_ReplyWithLongLong(ctx, i);
  66. }
  67. return REDISMODULE_OK;
  68. }
  69. int rw_attribute(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  70. if (argc != 2) return RedisModule_WrongArity(ctx);
  71. long long integer;
  72. if (RedisModule_StringToLongLong(argv[1], &integer) != REDISMODULE_OK)
  73. return RedisModule_ReplyWithError(ctx, "Arg cannot be parsed as a integer");
  74. if (RedisModule_ReplyWithAttribute(ctx, integer) != REDISMODULE_OK) {
  75. return RedisModule_ReplyWithError(ctx, "Attributes aren't supported by RESP 2");
  76. }
  77. for (int i = 0; i < integer; ++i) {
  78. RedisModule_ReplyWithLongLong(ctx, i);
  79. }
  80. RedisModule_ReplyWithSimpleString(ctx, "OK");
  81. return REDISMODULE_OK;
  82. }
  83. int rw_bool(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  84. REDISMODULE_NOT_USED(argv);
  85. if (argc != 1) return RedisModule_WrongArity(ctx);
  86. RedisModule_ReplyWithArray(ctx, 2);
  87. RedisModule_ReplyWithBool(ctx, 0);
  88. return RedisModule_ReplyWithBool(ctx, 1);
  89. }
  90. int rw_null(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  91. REDISMODULE_NOT_USED(argv);
  92. if (argc != 1) return RedisModule_WrongArity(ctx);
  93. return RedisModule_ReplyWithNull(ctx);
  94. }
  95. int rw_error(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  96. REDISMODULE_NOT_USED(argv);
  97. if (argc != 1) return RedisModule_WrongArity(ctx);
  98. return RedisModule_ReplyWithError(ctx, "An error");
  99. }
  100. int rw_verbatim(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  101. if (argc != 1) return RedisModule_WrongArity(ctx);
  102. return RedisModule_ReplyWithVerbatimString(ctx, argv[1]);
  103. }
  104. int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  105. REDISMODULE_NOT_USED(argv);
  106. REDISMODULE_NOT_USED(argc);
  107. if (RedisModule_Init(ctx, "replywith", 1, REDISMODULE_APIVER_1) != REDISMODULE_OK)
  108. return REDISMODULE_ERR;
  109. if (RedisModule_CreateCommand(ctx,"rw.string",rw_string,"",0,0,0) != REDISMODULE_OK)
  110. return REDISMODULE_ERR;
  111. if (RedisModule_CreateCommand(ctx,"rw.cstring",rw_cstring,"",0,0,0) != REDISMODULE_OK)
  112. return REDISMODULE_ERR;
  113. if (RedisModule_CreateCommand(ctx,"rw.int",rw_int,"",0,0,0) != REDISMODULE_OK)
  114. return REDISMODULE_ERR;
  115. if (RedisModule_CreateCommand(ctx,"rw.double",rw_double,"",0,0,0) != REDISMODULE_OK)
  116. return REDISMODULE_ERR;
  117. if (RedisModule_CreateCommand(ctx,"rw.longdouble",rw_longdouble,"",0,0,0) != REDISMODULE_OK)
  118. return REDISMODULE_ERR;
  119. if (RedisModule_CreateCommand(ctx,"rw.array",rw_array,"",0,0,0) != REDISMODULE_OK)
  120. return REDISMODULE_ERR;
  121. if (RedisModule_CreateCommand(ctx,"rw.map",rw_map,"",0,0,0) != REDISMODULE_OK)
  122. return REDISMODULE_ERR;
  123. if (RedisModule_CreateCommand(ctx,"rw.attribute",rw_attribute,"",0,0,0) != REDISMODULE_OK)
  124. return REDISMODULE_ERR;
  125. if (RedisModule_CreateCommand(ctx,"rw.set",rw_set,"",0,0,0) != REDISMODULE_OK)
  126. return REDISMODULE_ERR;
  127. if (RedisModule_CreateCommand(ctx,"rw.bool",rw_bool,"",0,0,0) != REDISMODULE_OK)
  128. return REDISMODULE_ERR;
  129. if (RedisModule_CreateCommand(ctx,"rw.null",rw_null,"",0,0,0) != REDISMODULE_OK)
  130. return REDISMODULE_ERR;
  131. if (RedisModule_CreateCommand(ctx,"rw.error",rw_error,"",0,0,0) != REDISMODULE_OK)
  132. return REDISMODULE_ERR;
  133. if (RedisModule_CreateCommand(ctx,"rw.verbatim",rw_verbatim,"",0,0,0) != REDISMODULE_OK)
  134. return REDISMODULE_ERR;
  135. return REDISMODULE_OK;
  136. }