-
Notifications
You must be signed in to change notification settings - Fork 415
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework
#[derive(GraphQLInputObject)]
macro implementation (#1052)
Co-authored-by: Kai Ren <tyranron@gmail.com>
- Loading branch information
Showing
29 changed files
with
1,925 additions
and
1,227 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//! Common functions, definitions and extensions for parsing and code generation | ||
//! of [GraphQL default values][0] | ||
//! | ||
//! [0]: https://spec.graphql.org/October2021#DefaultValue | ||
|
||
use proc_macro2::TokenStream; | ||
use quote::{quote, ToTokens}; | ||
use syn::{ | ||
parse::{Parse, ParseStream}, | ||
token, | ||
}; | ||
|
||
use crate::common::parse::ParseBufferExt as _; | ||
|
||
/// Representation of a [GraphQL default value][0] for code generation. | ||
/// | ||
/// [0]: https://spec.graphql.org/October2021#DefaultValue | ||
#[derive(Clone, Debug)] | ||
pub(crate) enum Value { | ||
/// [`Default`] implementation should be used. | ||
Default, | ||
|
||
/// Explicit [`Expr`]ession to be used as the [default value][0]. | ||
/// | ||
/// [`Expr`]: syn::Expr | ||
/// [0]: https://spec.graphql.org/October2021#DefaultValue | ||
Expr(Box<syn::Expr>), | ||
} | ||
|
||
impl Default for Value { | ||
fn default() -> Self { | ||
Self::Default | ||
} | ||
} | ||
|
||
impl From<Option<syn::Expr>> for Value { | ||
fn from(opt: Option<syn::Expr>) -> Self { | ||
match opt { | ||
Some(expr) => Self::Expr(Box::new(expr)), | ||
None => Self::Default, | ||
} | ||
} | ||
} | ||
|
||
impl Parse for Value { | ||
fn parse(input: ParseStream<'_>) -> syn::Result<Self> { | ||
Ok(input | ||
.try_parse::<token::Eq>()? | ||
.map(|_| input.parse::<syn::Expr>()) | ||
.transpose()? | ||
.into()) | ||
} | ||
} | ||
|
||
impl ToTokens for Value { | ||
fn to_tokens(&self, into: &mut TokenStream) { | ||
match self { | ||
Self::Default => quote! { | ||
::std::default::Default::default() | ||
} | ||
.to_tokens(into), | ||
Self::Expr(expr) => expr.to_tokens(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 was deleted.
Oops, something went wrong.
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
Oops, something went wrong.