1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/**
Create an input object

Input objects are used as data carriers for complex input values to
fields and mutations. Unlike the other helper macros,
`graphql_input_object!` actually *creates* the struct you define. It
does not add anything to the struct definition itself - what you type
is what will be generated:

```rust
# #[macro_use] extern crate juniper;
#
graphql_input_object!(
    description: "Coordinates for the user"

    struct Coordinates {
        longitude: f64 as "The X coordinate, from -180 to +180",
        latitude: f64 as "The Y coordinate, from -90 to +90",
    }
);

# fn main() { }
```

This macro creates the struct as specified and implements
`FromInputValue` to automatically parse values provided from variables
and arguments.

If you want to expose the struct under a different name than the Rust
type, you can write `struct Coordinates as "MyCoordinates" { ...`.

You can specify *default values* for input object fields; the syntax
is similar to argument default values:

```rust
# #[macro_use] extern crate juniper;
#
graphql_input_object!(
    struct SampleObject {
        foo = 123: i64 as "A sample field, defaults to 123 if omitted"
    }
);

# fn main() { }
```

*/
#[macro_export]
macro_rules! graphql_input_object {
    // Calls $val.$func($arg) if $arg is not None
    ( @maybe_apply, None, $func:ident, $val:expr ) => { $val };
    ( @maybe_apply, $arg:tt, $func:ident, $val:expr ) => { $val.$func($arg) };

    // Calls $val.description($descr) when $descr is not empty
    ( @apply_description, , $val:expr ) => { $val };
    ( @apply_description, $descr:tt , $val:expr ) => { $val.description($descr) };

    // Generate the FromInputValue::from method body, provided a
    // HashMap<&str, &InputValue> in $var
    (
        @generate_from_input_value,
        $name:tt, $var:tt,
        ( $($field_name:ident $(= $default:tt)* : $field_type:ty $(as $descr:tt)* $(,)* ),* )
    ) => {
        Some($name {
            $( $field_name: {
                let n: String = $crate::to_camel_case(stringify!($field_name));
                let v: Option<&&$crate::InputValue> = $var.get(&n[..]);

                println!("Found variable for {:?}: {:?} in {:?}", n, v, $var);

                match v {
                    $( Some(&&$crate::InputValue::Null) | None if true => $default, )*
                        Some(v) => $crate::FromInputValue::from(v).unwrap(),
                        _ => $crate::FromInputValue::from(&$crate::InputValue::null()).unwrap()
                }
            } ),*
        })
    };

    // Generate the ToInputValue::To method body, provided self in $self
    (
        @generate_to_input_value,
        $name:tt, $selfvar:tt,
        ( $($field_name:ident $(= $default:tt)* : $field_type:ty $(as $descr:tt)* $(,)* ),* )
    ) => {
        $crate::InputValue::object(vec![
            $(
                ($crate::to_camel_case(stringify!($field_name)), $selfvar.$field_name.to())
            ),*
        ].into_iter().collect())
    };

    // Generate the struct declaration, including (Rust) meta attributes
    (
        @generate_struct_fields,
        ( $($meta:tt)* ), ( $($pubmod:tt)* ), $name:tt,
        ( $($field_name:ident $(= $default:tt)* : $field_type:ty $(as $descr:tt)* $(,)* ),* )
    ) => {
        $($meta)* $($pubmod)* struct $name {
            $( $field_name: $field_type, )*
        }
    };

    // Generate single field meta for field with default value
    (
        @generate_single_meta_field,
        $reg:tt,
        ( $field_name:ident = $default:tt : $field_type:ty $(as $descr:tt)* )
    ) => {
        graphql_input_object!(
            @apply_description,
            $($descr)*,
            $reg.arg_with_default::<$field_type>(
                &$crate::to_camel_case(stringify!($field_name)),
                &$default))
    };

    // Generate single field meta for field without default value
    (
        @generate_single_meta_field,
        $reg:tt,
        ( $field_name:ident : $field_type:ty $(as $descr:tt)* )
    ) => {
        graphql_input_object!(
            @apply_description,
            $($descr)*,
            $reg.arg::<$field_type>(
                &$crate::to_camel_case(stringify!($field_name))))
    };

    // Generate the input field meta list, i.e. &[Argument] for
    (
        @generate_meta_fields,
        $reg:tt,
        ( $($field_name:ident $(= $default:tt)* : $field_type:ty $(as $descr:tt)* $(,)* ),* )
    ) => {
        &[
            $(
                graphql_input_object!(
                    @generate_single_meta_field,
                    $reg,
                    ( $field_name $(= $default)* : $field_type $(as $descr)* )
                )
            ),*
        ]
    };

    // #[...] struct $name { ... }
    // struct $name { ... }
    (
        @parse,
        ( $_ignore1:tt, $_ignore2:tt, $_ignore3:tt, $_ignore4:tt, $_ignore5:tt, $descr:tt ),
        $(#[$meta:meta])* struct $name:ident { $($fields:tt)* } $($rest:tt)*
    ) => {
        graphql_input_object!(
            @parse,
            ( ( $(#[$meta])* ), ( ), $name, (stringify!($name)), ($($fields)*), $descr ),
            $($rest)*
        );
    };

    // #[...] pub struct $name { ... }
    // pub struct $name { ... }
    (
        @parse,
        ( $_ignore1:tt, $_ignore2:tt, $_ignore3:tt, $_ignore4:tt, $_ignore5:tt, $descr:tt ),
        $(#[$meta:meta])* pub struct $name:ident { $($fields:tt)* } $($rest:tt)*
    ) => {
        graphql_input_object!(
            @parse,
            ( ( $(#[$meta])* ), ( pub ), $name, (stringify!($name)), ($($fields)*), $descr ),
            $($rest)*
        );
    };

    // #[...] struct $name as "GraphQLName" { ... }
    // struct $name as "GraphQLName" { ... }
    (
        @parse,
        ( $_ignore1:tt, $_ignore2:tt, $_ignore3:tt, $_ignore4:tt, $_ignore5:tt, $descr:tt ),
        $(#[$meta:meta])* struct $name:ident as $outname:tt { $($fields:tt)* } $($rest:tt)*
    ) => {
        graphql_input_object!(
            @parse,
            ( ( $($meta)* ), ( ), $name, $outname, ($($fields)*), $descr ),
            $($rest)*
        );
    };

    // #[...] pub struct $name as "GraphQLName" { ... }
    // pub struct $name as "GraphQLName" { ... }
    (
        @parse,
        ( $_ignore1:tt, $_ignore2:tt, $_ignore3:tt, $_ignore4:tt, $_ignore5:tt, $descr:tt ),
        $(#[$meta:meta])* pub struct $name:ident as $outname:tt { $($fields:tt)* } $($rest:tt)*
    ) => {
        graphql_input_object!(
            @parse,
            ( ( $($meta)* ), ( pub ), $name, $outname, ($($fields)*), $descr ),
            $($rest)*
        );
    };

    // description: <description>
    (
        @parse,
        ( $meta:tt, $pubmod:tt, $name:tt, $outname:tt, $fields:tt, $_ignore:tt ),
        description: $descr:tt $($rest:tt)*
    ) => {
        graphql_input_object!(
            @parse,
            ( $meta, $pubmod, $name, $outname, $fields, $descr ),
            $($rest)*
        );
    };

    // No more data to parse, generate the struct and impls
    (
        @parse,
        ( $meta:tt, $pubmod:tt, $name:tt, $outname:tt, $fields:tt, $descr:tt ),
    ) => {
        graphql_input_object!(@generate_struct_fields, $meta, $pubmod, $name, $fields);

        impl $crate::FromInputValue for $name {
            fn from(value: &$crate::InputValue) -> Option<$name> {
                if let Some(obj) = value.to_object_value() {
                    graphql_input_object!(@generate_from_input_value, $name, obj, $fields)
                }
                else {
                   None
                }
            }
        }

        impl $crate::ToInputValue for $name {
            fn to(&self) -> $crate::InputValue {
                graphql_input_object!(@generate_to_input_value, $name, self, $fields)
            }
        }

        impl $crate::GraphQLType for $name {
            type Context = ();

            fn name() -> Option<&'static str> {
                Some($outname)
            }

            fn meta<'r>(registry: &mut $crate::Registry<'r>) -> $crate::meta::MetaType<'r> {
                let fields = graphql_input_object!(@generate_meta_fields, registry, $fields);
                graphql_input_object!(
                    @maybe_apply, $descr, description,
                    registry.build_input_object_type::<$name>(fields)).into_meta()
            }
        }
    };

    // Entry point: parse calls starting with a struct declaration
    ( $(#[$meta:meta])* struct $($items:tt)* ) => {
        graphql_input_object!(
            @parse,
            ( ( ), ( ), None, None, None, None ),
            $(#[$meta])* struct $($items)*
        );
    };

    // Entry point: parse calls starting with a public struct declaration
    ( $(#[$meta:meta])* pub struct $($items:tt)* ) => {
        graphql_input_object!(
            @parse,
            ( ( ), ( ), None, None, None, None ),
            $(#[$meta])* pub struct $($items)*
        );
    };

    // Entry point: parse calls starting with the description
    ( description: $($items:tt)* ) => {
        graphql_input_object!(
            @parse,
            ( ( ), ( ), None, None, None, None ),
            description: $($items)*
        );
    };
}