-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements
RedisValue
derive proc macro. (#335)
`RedisValue` derive proc macro allows to automatically generate the `From` trait of a given struct and convert it to `RedisValue`. Example: ```rust #[derive(RedisValue)] struct RedisValueDeriveInner { i: i64, } #[derive(RedisValue)] struct RedisValueDerive { i: i64, f: f64, s: String, u: usize, v: Vec<i64>, v2: Vec<RedisValueDeriveInner>, hash_map: HashMap<String, String>, hash_set: HashSet<String>, ordered_map: BTreeMap<String, RedisValueDeriveInner>, ordered_set: BTreeSet<String>, } #[command( { flags: [ReadOnly, NoMandatoryKeys], arity: -1, key_spec: [ { notes: "test redis value derive macro", flags: [ReadOnly, Access], begin_search: Index({ index : 0 }), find_keys: Range({ last_key : 0, steps : 0, limit : 0 }), } ] } )] fn redis_value_derive(_ctx: &Context, _args: Vec<RedisString>) -> RedisResult { Ok(RedisValueDerive { i: 10, f: 1.1, s: "s".to_owned(), u: 20, v: vec![1, 2, 3], v2: vec![ RedisValueDeriveInner { i: 1 }, RedisValueDeriveInner { i: 2 }, ], hash_map: HashMap::from([("key".to_owned(), "val`".to_owned())]), hash_set: HashSet::from(["key".to_owned()]), ordered_map: BTreeMap::from([("key".to_owned(), RedisValueDeriveInner { i: 10 })]), ordered_set: BTreeSet::from(["key".to_owned()]), } .into()) } ``` The `From` implementation generates a `RedisValue::OrderMap` such that the fields names are the map keys and the values are the result of running `Into` function on the field value and convert it into a `RedisValue`. The code above will generate the following reply (in resp3): ``` 127.0.0.1:6379> redis_value_derive 1# "f" => (double) 1.1 2# "hash_map" => 1# "key" => "val" 3# "hash_set" => 1~ "key" 4# "i" => (integer) 10 5# "ordered_map" => 1# "key" => 1# "i" => (integer) 10 6# "ordered_set" => 1~ "key" 7# "s" => "s" 8# "u" => (integer) 20 9# "v" => 1) (integer) 1 2) (integer) 2 3) (integer) 3 10# "v2" => 1) 1# "i" => (integer) 1 2) 1# "i" => (integer) 2 ```
- Loading branch information
1 parent
4306b65
commit b0939e7
Showing
7 changed files
with
295 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn::{parse_macro_input, Data, DeriveInput, Fields}; | ||
|
||
pub fn redis_value(item: TokenStream) -> TokenStream { | ||
let struct_input: DeriveInput = parse_macro_input!(item); | ||
let struct_data = match struct_input.data { | ||
Data::Struct(s) => s, | ||
_ => { | ||
return quote! {compile_error!("RedisValue derive can only be apply on struct.")}.into() | ||
} | ||
}; | ||
|
||
let struct_name = struct_input.ident; | ||
|
||
let fields = match struct_data.fields { | ||
Fields::Named(f) => f, | ||
_ => { | ||
return quote! {compile_error!("RedisValue derive can only be apply on struct with named fields.")}.into() | ||
} | ||
}; | ||
|
||
let fields = fields | ||
.named | ||
.into_iter() | ||
.map(|v| { | ||
let name = v.ident.ok_or("Field without a name is not supported.")?; | ||
Ok(name) | ||
}) | ||
.collect::<Result<Vec<_>, &str>>(); | ||
|
||
let fields = match fields { | ||
Ok(f) => f, | ||
Err(e) => return quote! {compile_error!(#e)}.into(), | ||
}; | ||
|
||
let fields_names: Vec<_> = fields.iter().map(|v| v.to_string()).collect(); | ||
|
||
let res = quote! { | ||
impl From<#struct_name> for redis_module::redisvalue::RedisValue { | ||
fn from(val: #struct_name) -> redis_module::redisvalue::RedisValue { | ||
redis_module::redisvalue::RedisValue::OrderedMap(std::collections::BTreeMap::from([ | ||
#(( | ||
redis_module::redisvalue::RedisValueKey::String(#fields_names.to_owned()), | ||
val.#fields.into() | ||
), )* | ||
])) | ||
} | ||
} | ||
}; | ||
res.into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters