Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions examples/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use lazy_static::lazy_static;
use valkey_module::alloc::ValkeyAlloc;
use valkey_module::{
configuration::{ConfigurationContext, ConfigurationFlags},
enum_configuration, valkey_module, ConfigurationValue, Context, ValkeyError, ValkeyGILGuard,
ValkeyResult, ValkeyString, ValkeyValue,
enum_configuration, enum_configuration2, valkey_module, ConfigurationValue, Context,
ValkeyError, ValkeyGILGuard, ValkeyResult, ValkeyString, ValkeyValue,
};

enum_configuration! {
Expand All @@ -19,6 +19,14 @@ enum_configuration! {
}
}

enum_configuration2! {
#[derive(PartialEq)]
enum EnumConfiguration2 {
Val1 = ("val_1", 1),
Val2 = ("val_2", 2),
}
}

lazy_static! {
static ref NUM_OF_CONFIGURATION_CHANGES: ValkeyGILGuard<i64> = ValkeyGILGuard::default();
static ref CONFIGURATION_I64: ValkeyGILGuard<i64> = ValkeyGILGuard::default();
Expand All @@ -39,6 +47,8 @@ lazy_static! {
ValkeyGILGuard::new(EnumConfiguration::Val1);
static ref CONFIGURATION_MUTEX_ENUM: Mutex<EnumConfiguration> =
Mutex::new(EnumConfiguration::Val1);
static ref CONFIGURATION_ENUM2: ValkeyGILGuard<EnumConfiguration2> =
ValkeyGILGuard::new(EnumConfiguration2::Val1);
}

fn on_configuration_changed<G, T: ConfigurationValue<G>>(
Expand Down Expand Up @@ -133,6 +143,7 @@ valkey_module! {
["enum", &*CONFIGURATION_ENUM, EnumConfiguration::Val1, ConfigurationFlags::DEFAULT, Some(Box::new(on_configuration_changed))],
["reject_enum", &*CONFIGURATION_REJECT_ENUM, EnumConfiguration::Val1, ConfigurationFlags::DEFAULT, Some(Box::new(on_configuration_changed)), Some(Box::new(on_enum_config_set::<ValkeyString, ValkeyGILGuard<EnumConfiguration>>))],
["enum_mutex", &*CONFIGURATION_MUTEX_ENUM, EnumConfiguration::Val1, ConfigurationFlags::DEFAULT, Some(Box::new(on_configuration_changed))],
["enum2", &*CONFIGURATION_ENUM2, EnumConfiguration2::Val1, ConfigurationFlags::DEFAULT, Some(Box::new(on_configuration_changed))],
],
module_args_as_configuration: true,
]
Expand Down
77 changes: 61 additions & 16 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,8 @@ bitflags! {
}

#[macro_export]
macro_rules! enum_configuration {
($(#[$meta:meta])* $vis:vis enum $name:ident {
$($(#[$vmeta:meta])* $vname:ident = $val:expr,)*
}) => {
use $crate::configuration::EnumConfigurationValue;
$(#[$meta])*
$vis enum $name {
$($(#[$vmeta])* $vname = $val,)*
}

macro_rules! enum_configuration_tryfrom_trait {
($name:ident, $($vname:ident)*) => {
impl std::convert::TryFrom<i32> for $name {
type Error = $crate::ValkeyError;

Expand All @@ -59,19 +51,23 @@ macro_rules! enum_configuration {
}
}
}
}
}

#[macro_export]
macro_rules! enum_configuration_from_trait {
($name:ident) => {
impl std::convert::From<$name> for i32 {
fn from(val: $name) -> Self {
val as i32
}
}
};
}

impl EnumConfigurationValue for $name {
fn get_options(&self) -> (Vec<String>, Vec<i32>) {
(vec![$(stringify!($vname).to_string(),)*], vec![$($val,)*])
}
}

#[macro_export]
macro_rules! enum_configuration_clone_trait {
($name:ident, $($vname:ident)*) => {
impl Clone for $name {
fn clone(&self) -> Self {
match self {
Expand All @@ -82,6 +78,55 @@ macro_rules! enum_configuration {
}
}

#[macro_export]
macro_rules! enum_configuration {
($(#[$meta:meta])* $vis:vis enum $name:ident {
$($(#[$vmeta:meta])* $vname:ident = $val:expr,)*
}) => {
$(#[$meta])*
$vis enum $name {
$($(#[$vmeta])* $vname = $val,)*
}

$crate::enum_configuration_tryfrom_trait!($name, $($vname)*);

$crate::enum_configuration_from_trait!($name);

$crate::enum_configuration_clone_trait!($name, $($vname)*);

impl $crate::configuration::EnumConfigurationValue for $name {
fn get_options(&self) -> (Vec<String>, Vec<i32>) {
(vec![$(stringify!($vname).to_string(),)*], vec![$($val,)*])
}
}
}
}

#[macro_export]
macro_rules! enum_configuration2 {
($(#[$meta:meta])* $vis:vis enum $name:ident {
$($(#[$vmeta:meta])* $vname:ident = ($sname:expr, $val:expr),)*
}) => {
$(#[$meta])*
$vis enum $name {
$($(#[$vmeta])* $vname = $val,)*
}

$crate::enum_configuration_tryfrom_trait!($name, $($vname)*);

$crate::enum_configuration_from_trait!($name);

$crate::enum_configuration_clone_trait!($name, $($vname)*);


impl $crate::configuration::EnumConfigurationValue for $name {
fn get_options(&self) -> (Vec<String>, Vec<i32>) {
(vec![$($sname.to_string(),)*], vec![$($val,)*])
}
}
}
}

/// [`ConfigurationContext`] is used as a special context that indicate that we are
/// running with the Valkey GIL is held but we should not perform all the regular
/// operation we can perfrom on the regular Context.
Expand Down
8 changes: 6 additions & 2 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,10 @@ fn test_configuration() -> Result<()> {
config_set("configuration.enum_mutex", "Val2")?;
assert_eq!(config_get("configuration.enum_mutex")?, "Val2");

assert_eq!(config_get("configuration.enum2")?, "val_1");
config_set("configuration.enum2", "val_2")?;
assert_eq!(config_get("configuration.enum2")?, "val_2");

// Validate that configs can be rejected
let value = config_set("configuration.reject_valkey_string", "rejectvalue");
assert!(value
Expand All @@ -632,7 +636,7 @@ fn test_configuration() -> Result<()> {
let res: i64 = redis::cmd("configuration.num_changes")
.query(&mut con)
.with_context(|| "failed to run configuration.num_changes")?;
assert_eq!(res, 26); // the first configuration initialisation is counted as well, so we will get 22 changes.
assert_eq!(res, 28); // the first configuration initialisation is counted as well, so we will get 22 changes.

// Validate that configs with logic to reject values can also succeed
assert_eq!(config_get("configuration.reject_valkey_string")?, "default");
Expand All @@ -654,7 +658,7 @@ fn test_configuration() -> Result<()> {
let res: i64 = redis::cmd("configuration.num_changes")
.query(&mut con)
.with_context(|| "failed to run configuration.num_changes")?;
assert_eq!(res, 28);
assert_eq!(res, 30);

Ok(())
}
Expand Down