Skip to content

Commit

Permalink
Split the info examples into more.
Browse files Browse the repository at this point in the history
  • Loading branch information
iddm committed Jul 18, 2023
1 parent be7fc5f commit ff8a03d
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 56 deletions.
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ crate-type = ["cdylib"]
name = "info_handler_macro"
crate-type = ["cdylib"]

[[example]]
name = "info_handler_builder"
crate-type = ["cdylib"]

[[example]]
name = "info_handler_struct"
crate-type = ["cdylib"]

[[example]]
name = "info"
crate-type = ["cdylib"]
Expand Down
27 changes: 27 additions & 0 deletions examples/info_handler_builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use redis_module::InfoContext;
use redis_module::{redis_module, RedisResult};
use redis_module_macros::info_command_handler;

#[info_command_handler]
fn add_info(ctx: &InfoContext, _for_crash_report: bool) -> RedisResult<()> {
ctx.builder()
.add_section("info")
.field("field", "value")?
.add_dictionary("dictionary")
.field("key", "value")?
.build_dictionary()?
.build_section()?
.build_info();

Ok(())
}

//////////////////////////////////////////////////////

redis_module! {
name: "info_handler_builder",
version: 1,
allocator: (redis_module::alloc::RedisAlloc, redis_module::alloc::RedisAlloc),
data_types: [],
commands: [],
}
23 changes: 6 additions & 17 deletions examples/info_handler_macro.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
use std::collections::HashMap;

use redis_module::InfoContext;
use redis_module::{redis_module, RedisResult};
use redis_module::{InfoContext, Status};
use redis_module_macros::info_command_handler;
use redis_module_macros::InfoSection;

#[derive(Debug, Clone, InfoSection)]
struct InfoData {
field: String,
dictionary: HashMap<String, String>,
}

#[info_command_handler]
fn add_info(ctx: &InfoContext, _for_crash_report: bool) -> RedisResult<()> {
let mut dictionary = HashMap::new();
dictionary.insert("key".to_owned(), "value".into());
let data = InfoData {
field: "test_helper_value".to_owned(),
dictionary,
};
ctx.build_one_section(data)
if ctx.add_info_section(Some("info")) == Status::Ok {
ctx.add_info_field_str("field", "value");
}

Ok(())
}

//////////////////////////////////////////////////////
Expand Down
32 changes: 32 additions & 0 deletions examples/info_handler_struct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use std::collections::HashMap;

use redis_module::InfoContext;
use redis_module::{redis_module, RedisResult};
use redis_module_macros::{info_command_handler, InfoSection};

#[derive(Debug, Clone, InfoSection)]
struct Info {
field: String,
dictionary: HashMap<String, String>,
}

#[info_command_handler]
fn add_info(ctx: &InfoContext, _for_crash_report: bool) -> RedisResult<()> {
let mut dictionary = HashMap::new();
dictionary.insert("key".to_owned(), "value".into());
let data = Info {
field: "value".to_owned(),
dictionary,
};
ctx.build_one_section(data)
}

//////////////////////////////////////////////////////

redis_module! {
name: "info_handler_struct",
version: 1,
allocator: (redis_module::alloc::RedisAlloc, redis_module::alloc::RedisAlloc),
data_types: [],
commands: [],
}
21 changes: 6 additions & 15 deletions examples/test_helper.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::collections::HashMap;

use redis_module::InfoContext;
use redis_module::{redis_module, Context, RedisError, RedisResult, RedisString};
use redis_module_macros::InfoSection;
use redis_module::{InfoContext, Status};

fn test_helper_version(ctx: &Context, _args: Vec<RedisString>) -> RedisResult {
let ver = ctx.get_redis_version()?;
Expand Down Expand Up @@ -33,20 +32,12 @@ fn test_helper_err(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
Ok(().into())
}

#[derive(Debug, Clone, InfoSection)]
struct InfoData {
field: String,
dictionary: HashMap<String, String>,
}

fn add_info(ctx: &InfoContext, _for_crash_report: bool) -> RedisResult<()> {
let mut dictionary = HashMap::new();
dictionary.insert("key".to_owned(), "value".into());
let data = InfoData {
field: "test_helper_value".to_owned(),
dictionary,
};
ctx.build_one_section(data)
if ctx.add_info_section(Some("test_helper")) == Status::Ok {
ctx.add_info_field_str("field", "value");
}

Ok(())
}

//////////////////////////////////////////////////////
Expand Down
20 changes: 14 additions & 6 deletions src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1222,18 +1222,26 @@ impl InfoContext {
}

#[deprecated = "Please use [`InfoContext::builder`] instead."]
pub fn add_info_section(&self, name: Option<&str>) -> RedisResult<()> {
add_info_section(self.ctx, name).into()
/// The `name` of the sction will be prefixed with the module name
/// and an underscore: `<module name>_<name>`.
pub fn add_info_section(&self, name: Option<&str>) -> Status {
add_info_section(self.ctx, name)
}

#[deprecated = "Please use [`InfoContext::builder`] instead."]
pub fn add_info_field_str(&self, name: &str, content: &str) -> RedisResult<()> {
add_info_field_str(self.ctx, name, content).into()
/// The `name` will be prefixed with the module name and an
/// underscore: `<module name>_<name>`. The `content` pass is left
/// "as is".
pub fn add_info_field_str(&self, name: &str, content: &str) -> Status {
add_info_field_str(self.ctx, name, content)
}

#[deprecated = "Please use [`InfoContext::builder`] instead."]
pub fn add_info_field_long_long(&self, name: &str, value: c_longlong) -> RedisResult<()> {
add_info_field_long_long(self.ctx, name, value).into()
/// The `name` will be prefixed with the module name and an
/// underscore: `<module name>_<name>`. The `value` pass is left
/// "as is".
pub fn add_info_field_long_long(&self, name: &str, value: c_longlong) -> Status {
add_info_field_long_long(self.ctx, name, value)
}
}

Expand Down
46 changes: 28 additions & 18 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,24 +113,34 @@ fn test_command_name() -> Result<()> {

#[test]
fn test_helper_info() -> Result<()> {
const MODULES: [&str; 2] = ["test_helper", "info_handler_macro"];

MODULES.iter().try_for_each(|module| {
let port: u16 = 6483;
let _guards = vec![start_redis_server_with_module(module, port)
.with_context(|| "failed to start redis server")?];
let mut con =
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;

let res: String = redis::cmd("INFO")
.arg(module)
.query(&mut con)
.with_context(|| format!("failed to run INFO {module}"))?;
assert!(res.contains(&format!("{module}_field:test_helper_value")));
assert!(res.contains("dictionary:key=value"));

Ok(())
})
const MODULES: [(&str, bool); 4] = [
("test_helper", false),
("info_handler_macro", false),
("info_handler_builder", true),
("info_handler_struct", true),
];

MODULES
.into_iter()
.try_for_each(|(module, has_dictionary)| {
let port: u16 = 6483;
let _guards = vec![start_redis_server_with_module(module, port)
.with_context(|| "failed to start redis server")?];
let mut con =
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;

let res: String = redis::cmd("INFO")
.arg(module)
.query(&mut con)
.with_context(|| format!("failed to run INFO {module}"))?;
println!("Now processing {module}. Result: {res}.");
assert!(res.contains(&format!("{module}_field:value")));
if has_dictionary {
assert!(res.contains("dictionary:key=value"));
}

Ok(())
})
}

#[allow(unused_must_use)]
Expand Down

0 comments on commit ff8a03d

Please sign in to comment.