@@ -11,6 +11,32 @@ fn path_to_match() -> syn::Path {
1111 syn:: parse_str ( "graphql" ) . expect ( "`graphql` is a valid path" )
1212}
1313
14+ pub fn ident_exists ( ast : & syn:: DeriveInput , ident : & str ) -> Result < ( ) , syn:: Error > {
15+ let graphql_path = path_to_match ( ) ;
16+ let attribute = ast
17+ . attrs
18+ . iter ( )
19+ . find ( |attr| attr. path == graphql_path)
20+ . ok_or_else ( || syn:: Error :: new_spanned ( ast, "The graphql attribute is missing" ) ) ?;
21+
22+ if let syn:: Meta :: List ( items) = & attribute. parse_meta ( ) . expect ( "Attribute is well formatted" ) {
23+ for item in items. nested . iter ( ) {
24+ if let syn:: NestedMeta :: Meta ( syn:: Meta :: Path ( path) ) = item {
25+ if let Some ( ident_) = path. get_ident ( ) {
26+ if ident_ == ident {
27+ return Ok ( ( ) ) ;
28+ }
29+ }
30+ }
31+ }
32+ }
33+
34+ Err ( syn:: Error :: new_spanned (
35+ & ast,
36+ format ! ( "Ident `{}` not found" , ident) ,
37+ ) )
38+ }
39+
1440/// Extract an configuration parameter specified in the `graphql` attribute.
1541pub fn extract_attr ( ast : & syn:: DeriveInput , attr : & str ) -> Result < String , syn:: Error > {
1642 let attributes = & ast. attrs ;
@@ -103,6 +129,10 @@ pub fn extract_fragments_other_variant(ast: &syn::DeriveInput) -> bool {
103129 . unwrap_or ( false )
104130}
105131
132+ pub fn extract_skip_serializing_none ( ast : & syn:: DeriveInput ) -> bool {
133+ ident_exists ( ast, "skip_serializing_none" ) . is_ok ( )
134+ }
135+
106136#[ cfg( test) ]
107137mod test {
108138 use super :: * ;
@@ -219,4 +249,33 @@ mod test {
219249 let parsed = syn:: parse_str ( input) . unwrap ( ) ;
220250 assert ! ( !extract_fragments_other_variant( & parsed) ) ;
221251 }
252+
253+ #[ test]
254+ fn test_skip_serializing_none_set ( ) {
255+ let input = r#"
256+ #[derive(GraphQLQuery)]
257+ #[graphql(
258+ schema_path = "x",
259+ query_path = "x",
260+ skip_serializing_none
261+ )]
262+ struct MyQuery;
263+ "# ;
264+ let parsed = syn:: parse_str ( input) . unwrap ( ) ;
265+ assert ! ( extract_skip_serializing_none( & parsed) ) ;
266+ }
267+
268+ #[ test]
269+ fn test_skip_serializing_none_unset ( ) {
270+ let input = r#"
271+ #[derive(GraphQLQuery)]
272+ #[graphql(
273+ schema_path = "x",
274+ query_path = "x",
275+ )]
276+ struct MyQuery;
277+ "# ;
278+ let parsed = syn:: parse_str ( input) . unwrap ( ) ;
279+ assert ! ( !extract_skip_serializing_none( & parsed) ) ;
280+ }
222281}
0 commit comments