Skip to content

Commit ff8a03d

Browse files
committed
Split the info examples into more.
1 parent be7fc5f commit ff8a03d

File tree

7 files changed

+121
-56
lines changed

7 files changed

+121
-56
lines changed

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ crate-type = ["cdylib"]
8484
name = "info_handler_macro"
8585
crate-type = ["cdylib"]
8686

87+
[[example]]
88+
name = "info_handler_builder"
89+
crate-type = ["cdylib"]
90+
91+
[[example]]
92+
name = "info_handler_struct"
93+
crate-type = ["cdylib"]
94+
8795
[[example]]
8896
name = "info"
8997
crate-type = ["cdylib"]

examples/info_handler_builder.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use redis_module::InfoContext;
2+
use redis_module::{redis_module, RedisResult};
3+
use redis_module_macros::info_command_handler;
4+
5+
#[info_command_handler]
6+
fn add_info(ctx: &InfoContext, _for_crash_report: bool) -> RedisResult<()> {
7+
ctx.builder()
8+
.add_section("info")
9+
.field("field", "value")?
10+
.add_dictionary("dictionary")
11+
.field("key", "value")?
12+
.build_dictionary()?
13+
.build_section()?
14+
.build_info();
15+
16+
Ok(())
17+
}
18+
19+
//////////////////////////////////////////////////////
20+
21+
redis_module! {
22+
name: "info_handler_builder",
23+
version: 1,
24+
allocator: (redis_module::alloc::RedisAlloc, redis_module::alloc::RedisAlloc),
25+
data_types: [],
26+
commands: [],
27+
}

examples/info_handler_macro.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
1-
use std::collections::HashMap;
2-
3-
use redis_module::InfoContext;
41
use redis_module::{redis_module, RedisResult};
2+
use redis_module::{InfoContext, Status};
53
use redis_module_macros::info_command_handler;
6-
use redis_module_macros::InfoSection;
7-
8-
#[derive(Debug, Clone, InfoSection)]
9-
struct InfoData {
10-
field: String,
11-
dictionary: HashMap<String, String>,
12-
}
134

145
#[info_command_handler]
156
fn add_info(ctx: &InfoContext, _for_crash_report: bool) -> RedisResult<()> {
16-
let mut dictionary = HashMap::new();
17-
dictionary.insert("key".to_owned(), "value".into());
18-
let data = InfoData {
19-
field: "test_helper_value".to_owned(),
20-
dictionary,
21-
};
22-
ctx.build_one_section(data)
7+
if ctx.add_info_section(Some("info")) == Status::Ok {
8+
ctx.add_info_field_str("field", "value");
9+
}
10+
11+
Ok(())
2312
}
2413

2514
//////////////////////////////////////////////////////

examples/info_handler_struct.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use std::collections::HashMap;
2+
3+
use redis_module::InfoContext;
4+
use redis_module::{redis_module, RedisResult};
5+
use redis_module_macros::{info_command_handler, InfoSection};
6+
7+
#[derive(Debug, Clone, InfoSection)]
8+
struct Info {
9+
field: String,
10+
dictionary: HashMap<String, String>,
11+
}
12+
13+
#[info_command_handler]
14+
fn add_info(ctx: &InfoContext, _for_crash_report: bool) -> RedisResult<()> {
15+
let mut dictionary = HashMap::new();
16+
dictionary.insert("key".to_owned(), "value".into());
17+
let data = Info {
18+
field: "value".to_owned(),
19+
dictionary,
20+
};
21+
ctx.build_one_section(data)
22+
}
23+
24+
//////////////////////////////////////////////////////
25+
26+
redis_module! {
27+
name: "info_handler_struct",
28+
version: 1,
29+
allocator: (redis_module::alloc::RedisAlloc, redis_module::alloc::RedisAlloc),
30+
data_types: [],
31+
commands: [],
32+
}

examples/test_helper.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use std::collections::HashMap;
22

3-
use redis_module::InfoContext;
43
use redis_module::{redis_module, Context, RedisError, RedisResult, RedisString};
5-
use redis_module_macros::InfoSection;
4+
use redis_module::{InfoContext, Status};
65

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

36-
#[derive(Debug, Clone, InfoSection)]
37-
struct InfoData {
38-
field: String,
39-
dictionary: HashMap<String, String>,
40-
}
41-
4235
fn add_info(ctx: &InfoContext, _for_crash_report: bool) -> RedisResult<()> {
43-
let mut dictionary = HashMap::new();
44-
dictionary.insert("key".to_owned(), "value".into());
45-
let data = InfoData {
46-
field: "test_helper_value".to_owned(),
47-
dictionary,
48-
};
49-
ctx.build_one_section(data)
36+
if ctx.add_info_section(Some("test_helper")) == Status::Ok {
37+
ctx.add_info_field_str("field", "value");
38+
}
39+
40+
Ok(())
5041
}
5142

5243
//////////////////////////////////////////////////////

src/context/mod.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,18 +1222,26 @@ impl InfoContext {
12221222
}
12231223

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

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

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

tests/integration.rs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -113,24 +113,34 @@ fn test_command_name() -> Result<()> {
113113

114114
#[test]
115115
fn test_helper_info() -> Result<()> {
116-
const MODULES: [&str; 2] = ["test_helper", "info_handler_macro"];
117-
118-
MODULES.iter().try_for_each(|module| {
119-
let port: u16 = 6483;
120-
let _guards = vec![start_redis_server_with_module(module, port)
121-
.with_context(|| "failed to start redis server")?];
122-
let mut con =
123-
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;
124-
125-
let res: String = redis::cmd("INFO")
126-
.arg(module)
127-
.query(&mut con)
128-
.with_context(|| format!("failed to run INFO {module}"))?;
129-
assert!(res.contains(&format!("{module}_field:test_helper_value")));
130-
assert!(res.contains("dictionary:key=value"));
131-
132-
Ok(())
133-
})
116+
const MODULES: [(&str, bool); 4] = [
117+
("test_helper", false),
118+
("info_handler_macro", false),
119+
("info_handler_builder", true),
120+
("info_handler_struct", true),
121+
];
122+
123+
MODULES
124+
.into_iter()
125+
.try_for_each(|(module, has_dictionary)| {
126+
let port: u16 = 6483;
127+
let _guards = vec![start_redis_server_with_module(module, port)
128+
.with_context(|| "failed to start redis server")?];
129+
let mut con =
130+
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;
131+
132+
let res: String = redis::cmd("INFO")
133+
.arg(module)
134+
.query(&mut con)
135+
.with_context(|| format!("failed to run INFO {module}"))?;
136+
println!("Now processing {module}. Result: {res}.");
137+
assert!(res.contains(&format!("{module}_field:value")));
138+
if has_dictionary {
139+
assert!(res.contains("dictionary:key=value"));
140+
}
141+
142+
Ok(())
143+
})
134144
}
135145

136146
#[allow(unused_must_use)]

0 commit comments

Comments
 (0)