Refactor vars! macro position [skip ci]

This commit is contained in:
tyranron 2022-06-08 10:50:47 +02:00
parent b28acbd96d
commit e347f25718
No known key found for this signature in database
GPG key ID: 762E144FB230A4F0
4 changed files with 86 additions and 82 deletions

View file

@ -2,9 +2,10 @@ use crate::{behavior, resolve};
pub use crate::{
ast::InputValue,
macros::{input_value, value},
macros::{input_value, value, vars},
resolve::Type,
value::Value,
executor::Variables,
};
pub trait Interface<S>

View file

@ -76,7 +76,7 @@ pub use crate::{
introspection::IntrospectionFormat,
macros::{
helper::subscription::{ExtractTypeFromStream, IntoFieldResult},
input_value as graphql_input_value, value as graphql_value,
input_value as graphql_input_value, value as graphql_value, vars as graphql_vars,
},
parser::{ParseError, ScalarToken, Spanning},
schema::{

View file

@ -8,8 +8,7 @@ pub mod reflect;
mod input_value;
mod value;
#[macro_use]
mod graphql_vars;
mod vars;
#[doc(inline)]
pub use self::{input_value::input_value, value::value};
pub use self::{input_value::input_value, value::value, vars::vars};

View file

@ -1,20 +1,18 @@
//! [`graphql_vars!`] macro implementation.
//!
//! [`graphql_vars!`]: graphql_vars
//! [`vars!`] macro implementation.
/// Constructs [`Variables`] via JSON-like syntax.
/// Constructs [`graphql::Variables`] via JSON-like syntax.
///
/// [`Variables`] key should implement [`Into`]`<`[`String`]`>`.
/// [`graphql::Variables`] key should implement [`Into`]`<`[`String`]`>`.
/// ```rust
/// # use std::borrow::Cow;
/// #
/// # use juniper::{graphql_vars, Variables};
/// # use juniper::graphql;
/// #
/// let code = 200;
/// let features = vec!["key", "value"];
/// let key: Cow<'static, str> = "key".into();
///
/// let value: Variables = graphql_vars! {
/// let value: graphql::Variables = graphql::vars! {
/// "code": code,
/// "success": code == 200,
/// features[0]: features[1],
@ -22,12 +20,11 @@
/// };
/// ```
///
/// See [`graphql_input_value!`] for more info on syntax of value after `:`.
/// See [`graphql::input_value!`] for more info on syntax of value after `:`.
///
/// [`graphql_input_value!`]: crate::graphql_input_value
/// [`Variables`]: crate::Variables
#[macro_export]
macro_rules! graphql_vars {
/// [`graphql::input_value!`]: crate::graphql::input_value
/// [`graphql::Variables`]: crate::graphql::Variables
macro_rules! vars {
////////////
// Object //
////////////
@ -38,12 +35,12 @@ macro_rules! graphql_vars {
// Insert the current entry followed by trailing comma.
(@object $object:ident [$($key:tt)+] ($value:expr) , $($rest:tt)*) => {
let _ = $object.insert(($($key)+).into(), $value);
$crate::graphql_vars! {@object $object () ($($rest)*) ($($rest)*)};
$crate::graphql::vars! {@object $object () ($($rest)*) ($($rest)*)};
};
// Current entry followed by unexpected token.
(@object $object:ident [$($key:tt)+] ($value:expr) $unexpected:tt $($rest:tt)*) => {
$crate::graphql_vars! {@unexpected $unexpected};
$crate::graphql::vars! {@unexpected $unexpected};
};
// Insert the last entry without trailing comma.
@ -53,7 +50,7 @@ macro_rules! graphql_vars {
// Next value is `null`.
(@object $object:ident ($($key:tt)+) (: null $($rest:tt)*) $copy:tt) => {
$crate::graphql_vars! {
$crate::graphql::vars! {
@object $object
[$($key)+]
($crate::graphql_input_value!(null)) $($rest)*
@ -62,7 +59,7 @@ macro_rules! graphql_vars {
// Next value is `None`.
(@object $object:ident ($($key:tt)+) (: None $($rest:tt)*) $copy:tt) => {
$crate::graphql_vars! {
$crate::graphql::vars! {
@object $object
[$($key)+]
($crate::graphql_input_value!(None)) $($rest)*
@ -71,7 +68,7 @@ macro_rules! graphql_vars {
// Next value is a variable.
(@object $object:ident ($($key:tt)+) (: @$var:ident $($rest:tt)*) $copy:tt) => {
$crate::graphql_vars! {
$crate::graphql::vars! {
@object $object
[$($key)+]
($crate::graphql_input_value!(@$var)) $($rest)*
@ -80,7 +77,7 @@ macro_rules! graphql_vars {
// Next value is an array.
(@object $object:ident ($($key:tt)+) (: [$($array:tt)*] $($rest:tt)*) $copy:tt) => {
$crate::graphql_vars! {
$crate::graphql::vars! {
@object $object
[$($key)+]
($crate::graphql_input_value!([$($array)*])) $($rest)*
@ -89,7 +86,7 @@ macro_rules! graphql_vars {
// Next value is a map.
(@object $object:ident ($($key:tt)+) (: {$($map:tt)*} $($rest:tt)*) $copy:tt) => {
$crate::graphql_vars! {
$crate::graphql::vars! {
@object $object
[$($key)+]
($crate::graphql_input_value!({$($map)*})) $($rest)*
@ -98,7 +95,7 @@ macro_rules! graphql_vars {
// Next value is `true`, `false` or enum ident followed by a comma.
(@object $object:ident ($($key:tt)+) (: $ident:ident , $($rest:tt)*) $copy:tt) => {
$crate::graphql_vars! {
$crate::graphql::vars! {
@object $object
[$($key)+]
($crate::graphql_input_value!($ident)) , $($rest)*
@ -107,7 +104,7 @@ macro_rules! graphql_vars {
// Next value is `true`, `false` or enum ident without trailing comma.
(@object $object:ident ($($key:tt)+) (: $last:ident ) $copy:tt) => {
$crate::graphql_vars! {
$crate::graphql::vars! {
@object $object
[$($key)+]
($crate::graphql_input_value!($last))
@ -116,7 +113,7 @@ macro_rules! graphql_vars {
// Next value is an expression followed by comma.
(@object $object:ident ($($key:tt)+) (: $value:expr , $($rest:tt)*) $copy:tt) => {
$crate::graphql_vars! {
$crate::graphql::vars! {
@object $object
[$($key)+]
($crate::graphql_input_value!($value)) , $($rest)*
@ -125,7 +122,7 @@ macro_rules! graphql_vars {
// Last value is an expression with no trailing comma.
(@object $object:ident ($($key:tt)+) (: $value:expr) $copy:tt) => {
$crate::graphql_vars! {
$crate::graphql::vars! {
@object $object
[$($key)+]
($crate::graphql_input_value!($value))
@ -135,44 +132,44 @@ macro_rules! graphql_vars {
// Missing value for last entry. Trigger a reasonable error message.
(@object $object:ident ($($key:tt)+) (:) $copy:tt) => {
// "unexpected end of macro invocation"
$crate::graphql_vars! {};
$crate::graphql::vars! {};
};
// Missing colon and value for last entry. Trigger a reasonable error
// message.
(@object $object:ident ($($key:tt)+) () $copy:tt) => {
// "unexpected end of macro invocation"
$crate::graphql_vars! {};
$crate::graphql::vars! {};
};
// Misplaced colon. Trigger a reasonable error message.
(@object $object:ident () (: $($rest:tt)*) ($colon:tt $($copy:tt)*)) => {
// Takes no arguments so "no rules expected the token `:`".
$crate::graphql_vars! {@unexpected $colon};
$crate::graphql::vars! {@unexpected $colon};
};
// Found a comma inside a key. Trigger a reasonable error message.
(@object $object:ident ($($key:tt)*) (, $($rest:tt)*) ($comma:tt $($copy:tt)*)) => {
// Takes no arguments so "no rules expected the token `,`".
$crate::graphql_vars! {@unexpected $comma};
$crate::graphql::vars! {@unexpected $comma};
};
// Key is fully parenthesized. This avoids clippy double_parens false
// positives because the parenthesization may be necessary here.
(@object $object:ident () (($key:expr) : $($rest:tt)*) $copy:tt) => {
$crate::graphql_vars! {
$crate::graphql::vars! {
@object $object ($key) (: $($rest)*) (: $($rest)*)
};
};
// Refuse to absorb colon token into key expression.
(@object $object:ident ($($key:tt)*) (: $($unexpected:tt)+) $copy:tt) => {
$crate::graphql_vars! {@unexpected $($unexpected)+};
$crate::graphql::vars! {@unexpected $($unexpected)+};
};
// Munch a token into the current key.
(@object $object:ident ($($key:tt)*) ($tt:tt $($rest:tt)*) $copy:tt) => {
$crate::graphql_vars! {
$crate::graphql::vars! {
@object $object
($($key)* $tt)
($($rest)*) ($($rest)*)
@ -189,26 +186,33 @@ macro_rules! graphql_vars {
// Defaults //
//////////////
() => {{ $crate::Variables::<_>::new() }};
() => {{ $crate::graphql::Variables::<_>::new() }};
( $($map:tt)+ ) => {{
let mut object = $crate::Variables::<_>::new();
$crate::graphql_vars! {@object object () ($($map)*) ($($map)*)};
let mut object = $crate::graphql::Variables::<_>::new();
$crate::graphql::vars! {@object object () ($($map)*) ($($map)*)};
object
}};
}
#[doc(inline)]
pub(super) use vars;
#[cfg(test)]
mod tests {
use indexmap::{indexmap, IndexMap};
type V = crate::Variables;
use crate::graphql;
type IV = crate::InputValue;
use super::vars;
type V = graphql::Variables;
type IV = graphql::InputValue;
#[test]
fn empty() {
assert_eq!(graphql_vars! {}, V::new());
assert_eq!(vars! {}, V::new());
}
#[test]
@ -216,37 +220,37 @@ mod tests {
let val = 42;
assert_eq!(
graphql_vars! {"key": 123},
vars! {"key": 123},
vec![("key".to_owned(), IV::scalar(123))]
.into_iter()
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": "val"},
vars! {"key": "val"},
vec![("key".to_owned(), IV::scalar("val"))]
.into_iter()
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": 1.23},
vars! {"key": 1.23},
vec![("key".to_owned(), IV::scalar(1.23))]
.into_iter()
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": 1 + 2},
vars! {"key": 1 + 2},
vec![("key".to_owned(), IV::scalar(3))]
.into_iter()
.collect(),
);
assert_eq!(
graphql_vars! {"key": false},
vars! {"key": false},
vec![("key".to_owned(), IV::scalar(false))]
.into_iter()
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": (val)},
vars! {"key": (val)},
vec![("key".to_owned(), IV::scalar(42))]
.into_iter()
.collect::<V>(),
@ -256,13 +260,13 @@ mod tests {
#[test]
fn r#enum() {
assert_eq!(
graphql_vars! {"key": ENUM},
vars! {"key": ENUM},
vec![("key".to_owned(), IV::enum_value("ENUM"))]
.into_iter()
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": lowercase},
vars! {"key": lowercase},
vec![("key".to_owned(), IV::enum_value("lowercase"))]
.into_iter()
.collect::<V>(),
@ -272,19 +276,19 @@ mod tests {
#[test]
fn variable() {
assert_eq!(
graphql_vars! {"key": @var},
vars! {"key": @var},
vec![("key".to_owned(), IV::variable("var"))]
.into_iter()
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": @array},
vars! {"key": @array},
vec![("key".to_owned(), IV::variable("array"))]
.into_iter()
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": @object},
vars! {"key": @object},
vec![("key".to_owned(), IV::variable("object"))]
.into_iter()
.collect::<V>(),
@ -296,46 +300,46 @@ mod tests {
let val = 42;
assert_eq!(
graphql_vars! {"key": []},
vars! {"key": []},
vec![("key".to_owned(), IV::list(vec![]))]
.into_iter()
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": [null]},
vars! {"key": [null]},
vec![("key".to_owned(), IV::list(vec![IV::Null]))]
.into_iter()
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": [1]},
vars! {"key": [1]},
vec![("key".to_owned(), IV::list(vec![IV::scalar(1)]))]
.into_iter()
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": [1 + 2]},
vars! {"key": [1 + 2]},
vec![("key".to_owned(), IV::list(vec![IV::scalar(3)]))]
.into_iter()
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": [(val)]},
vars! {"key": [(val)]},
vec![("key".to_owned(), IV::list(vec![IV::scalar(42)]))]
.into_iter()
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": [ENUM]},
vars! {"key": [ENUM]},
vec![("key".to_owned(), IV::list(vec![IV::enum_value("ENUM")]))]
.into_iter()
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": [lowercase]},
vars! {"key": [lowercase]},
vec![(
"key".to_owned(),
IV::list(vec![IV::enum_value("lowercase")])
@ -345,26 +349,26 @@ mod tests {
);
assert_eq!(
graphql_vars! {"key": [@var]},
vars! {"key": [@var]},
vec![("key".to_owned(), IV::list(vec![IV::variable("var")]))]
.into_iter()
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": [@array]},
vars! {"key": [@array]},
vec![("key".to_owned(), IV::list(vec![IV::variable("array")]))]
.into_iter()
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": [@object]},
vars! {"key": [@object]},
vec![("key".to_owned(), IV::list(vec![IV::variable("object")]))]
.into_iter()
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": [1, [2], 3]},
vars! {"key": [1, [2], 3]},
vec![(
"key".to_owned(),
IV::list(vec![
@ -377,7 +381,7 @@ mod tests {
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": [1, [2 + 3], 3]},
vars! {"key": [1, [2 + 3], 3]},
vec![(
"key".to_owned(),
IV::list(vec![
@ -390,7 +394,7 @@ mod tests {
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": [1, [ENUM], (val)]},
vars! {"key": [1, [ENUM], (val)]},
vec![(
"key".to_owned(),
IV::list(vec![
@ -403,7 +407,7 @@ mod tests {
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": [1 + 2, [(val)], @val]},
vars! {"key": [1 + 2, [(val)], @val]},
vec![(
"key".to_owned(),
IV::list(vec![
@ -416,7 +420,7 @@ mod tests {
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": [1, [@val], ENUM]},
vars! {"key": [1, [@val], ENUM]},
vec![(
"key".to_owned(),
IV::list(vec![
@ -435,21 +439,21 @@ mod tests {
let val = 42;
assert_eq!(
graphql_vars! {"key": {}},
vars! {"key": {}},
vec![("key".to_owned(), IV::object(IndexMap::<String, _>::new()))]
.into_iter()
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": {"key": null}},
vars! {"key": {"key": null}},
vec![("key".to_owned(), IV::object(indexmap! {"key" => IV::Null}))]
.into_iter()
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": {"key": 123}},
vars! {"key": {"key": 123}},
vec![(
"key".to_owned(),
IV::object(indexmap! {"key" => IV::scalar(123)}),
@ -458,7 +462,7 @@ mod tests {
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": {"key": 1 + 2}},
vars! {"key": {"key": 1 + 2}},
vec![(
"key".to_owned(),
IV::object(indexmap! {"key" => IV::scalar(3)}),
@ -467,7 +471,7 @@ mod tests {
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": {"key": (val)}},
vars! {"key": {"key": (val)}},
vec![(
"key".to_owned(),
IV::object(indexmap! {"key" => IV::scalar(42)}),
@ -477,7 +481,7 @@ mod tests {
);
assert_eq!(
graphql_vars! {"key": {"key": []}},
vars! {"key": {"key": []}},
vec![(
"key".to_owned(),
IV::object(indexmap! {"key" => IV::list(vec![])}),
@ -486,7 +490,7 @@ mod tests {
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": {"key": [null]}},
vars! {"key": {"key": [null]}},
vec![(
"key".to_owned(),
IV::object(indexmap! {"key" => IV::list(vec![IV::Null])}),
@ -495,7 +499,7 @@ mod tests {
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": {"key": [1]}},
vars! {"key": {"key": [1]}},
vec![(
"key".to_owned(),
IV::object(indexmap! {"key" => IV::list(vec![IV::scalar(1)])}),
@ -504,7 +508,7 @@ mod tests {
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": {"key": [1 + 2]}},
vars! {"key": {"key": [1 + 2]}},
vec![(
"key".to_owned(),
IV::object(indexmap! {"key" => IV::list(vec![IV::scalar(3)])}),
@ -513,7 +517,7 @@ mod tests {
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": {"key": [(val)]}},
vars! {"key": {"key": [(val)]}},
vec![(
"key".to_owned(),
IV::object(indexmap! {"key" => IV::list(vec![IV::scalar(42)])}),
@ -522,7 +526,7 @@ mod tests {
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": {"key": ENUM}},
vars! {"key": {"key": ENUM}},
vec![(
"key".to_owned(),
IV::object(indexmap! {"key" => IV::enum_value("ENUM")}),
@ -531,7 +535,7 @@ mod tests {
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": {"key": lowercase}},
vars! {"key": {"key": lowercase}},
vec![(
"key".to_owned(),
IV::object(indexmap! {"key" => IV::enum_value("lowercase")}),
@ -540,7 +544,7 @@ mod tests {
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": {"key": @val}},
vars! {"key": {"key": @val}},
vec![(
"key".to_owned(),
IV::object(indexmap! {"key" => IV::variable("val")}),
@ -549,7 +553,7 @@ mod tests {
.collect::<V>(),
);
assert_eq!(
graphql_vars! {"key": {"key": @array}},
vars! {"key": {"key": @array}},
vec![(
"key".to_owned(),
IV::object(indexmap! {"key" => IV::variable("array")}),
@ -558,7 +562,7 @@ mod tests {
.collect::<V>(),
);
assert_eq!(
graphql_vars! {
vars! {
"inner": {
"key1": (val),
"key2": "val",