Created
November 11, 2016 12:33
-
-
Save garrensmith/7b885eb839724263794af98341fbfef8 to your computer and use it in GitHub Desktop.
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
use serde_json; | |
macro_rules! query_type { | |
("fields" => $fields:expr) => {{ | |
println!("field {:?}", $fields); | |
"boom".to_string() | |
}}; | |
( | |
{$section:expr => { | |
$( | |
$key:expr => { | |
$comp:expr => $val:expr | |
} | |
),* | |
} | |
}) => {{ | |
let mut selector = Map::new(); | |
$( | |
let mut kv = Map::new(); | |
kv.insert($comp.to_string(), serde_json::to_value($val)); | |
selector.insert($key.to_string(), kv); | |
println!("selector {:?} {:?} {:?}", $key, $comp, $val); | |
)* | |
selector | |
}}; | |
} | |
macro_rules! query { | |
( {$($section:expr => $content:tt),*} ) => {{ | |
use serde_json; | |
use serde_json::{Map}; | |
let mut map = Map::new(); | |
$( | |
println!("section {:?}", $section); | |
map.insert($section.to_string(), query_type!({$section => $content})); | |
)* | |
map | |
}}; | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
use serde_json::{Value}; | |
use serde_json; | |
#[test] | |
fn create_selector_query () { | |
let query = query!({ | |
"selector" => { | |
"_id" => { | |
"$eq" => "boom" | |
}, | |
"name" => { | |
"$lte" => "garren" | |
} | |
} | |
}); | |
let _id = query.get("selector").unwrap().get("_id").unwrap(); | |
let name = query.get("selector").unwrap().get("name").unwrap(); | |
assert_eq!(_id.get("$eq").unwrap().to_string(), "\"boom\""); | |
assert_eq!(name.get("$lte").unwrap().to_string(), "\"garren\""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment