diff options
author | Oran Agra <oran@redislabs.com> | 2022-04-26 20:29:20 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-26 20:29:20 +0300 |
commit | 81926254586f64fc6a9b393bf9afb0d2eacc5234 (patch) | |
tree | 51e28f518fc8f6219fd82395f76df3266b5ed3d1 /tests/modules | |
parent | 46ec6ad98e6d118bd98a447dcd2307e80bb0fcd6 (diff) | |
download | redis-81926254586f64fc6a9b393bf9afb0d2eacc5234.tar.gz |
Add module API flag for using enum configs as bit flags (#10643)
Enables registration of an enum config that'll let the user pass multiple keywords that
will be combined with `|` as flags into the integer config value.
```
const char *enum_vals[] = {"none", "one", "two", "three"};
const int int_vals[] = {0, 1, 2, 4};
if (RedisModule_RegisterEnumConfig(ctx, "flags", 3, REDISMODULE_CONFIG_DEFAULT | REDISMODULE_CONFIG_BITFLAGS, enum_vals, int_vals, 4, getFlagsConfigCommand, setFlagsConfigCommand, NULL, NULL) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
```
doing:
`config set moduleconfigs.flags "two three"` will result in 6 being passed to`setFlagsConfigCommand`.
Diffstat (limited to 'tests/modules')
-rw-r--r-- | tests/modules/moduleconfigs.c | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/tests/modules/moduleconfigs.c b/tests/modules/moduleconfigs.c index af30bda34..0a6380461 100644 --- a/tests/modules/moduleconfigs.c +++ b/tests/modules/moduleconfigs.c @@ -6,6 +6,7 @@ long long longval; long long memval; RedisModuleString *strval = NULL; int enumval; +int flagsval; /* Series of get and set callbacks for each type of config, these rely on the privdata ptr * to point to the config, and they register the configs as such. Note that one could also just @@ -68,6 +69,20 @@ int setEnumConfigCommand(const char *name, int val, void *privdata, RedisModuleS return REDISMODULE_OK; } +int getFlagsConfigCommand(const char *name, void *privdata) { + REDISMODULE_NOT_USED(name); + REDISMODULE_NOT_USED(privdata); + return flagsval; +} + +int setFlagsConfigCommand(const char *name, int val, void *privdata, RedisModuleString **err) { + REDISMODULE_NOT_USED(name); + REDISMODULE_NOT_USED(err); + REDISMODULE_NOT_USED(privdata); + flagsval = val; + return REDISMODULE_OK; +} + int boolApplyFunc(RedisModuleCtx *ctx, void *privdata, RedisModuleString **err) { REDISMODULE_NOT_USED(ctx); REDISMODULE_NOT_USED(privdata); @@ -106,10 +121,13 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) } /* On the stack to make sure we're copying them. */ - const char *enum_vals[3] = {"one", "two", "three"}; - const int int_vals[3] = {0, 2, 4}; + const char *enum_vals[] = {"none", "one", "two", "three"}; + const int int_vals[] = {0, 1, 2, 4}; - if (RedisModule_RegisterEnumConfig(ctx, "enum", 0, REDISMODULE_CONFIG_DEFAULT, enum_vals, int_vals, 3, getEnumConfigCommand, setEnumConfigCommand, NULL, NULL) == REDISMODULE_ERR) { + if (RedisModule_RegisterEnumConfig(ctx, "enum", 1, REDISMODULE_CONFIG_DEFAULT, enum_vals, int_vals, 4, getEnumConfigCommand, setEnumConfigCommand, NULL, NULL) == REDISMODULE_ERR) { + return REDISMODULE_ERR; + } + if (RedisModule_RegisterEnumConfig(ctx, "flags", 3, REDISMODULE_CONFIG_DEFAULT | REDISMODULE_CONFIG_BITFLAGS, enum_vals, int_vals, 4, getFlagsConfigCommand, setFlagsConfigCommand, NULL, NULL) == REDISMODULE_ERR) { return REDISMODULE_ERR; } /* Memory config here. */ |