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
//! Allows the usage of [HashMap] in GraphQL with [GraphQLMap] by serializing it as a regular list of typed entries,
//! instead of the default opaque JSON.
//!
//! GraphQl [doesn't support](https://github.com/graphql/graphql-spec/issues/101) this.

use std::{
    borrow::Cow,
    collections::HashMap,
    fmt::{Debug, Display},
};

use async_graphql::{
    parser::types::Field, registry::Registry, resolver_utils::resolve_list, ContextSelectionSet, InputType,
    InputValueError, InputValueResult, OutputType, Positioned, ServerResult, Value,
};

use crate::error::{Error, GenericErrorCode, MapToErr};

/// This type represents a [HashMap] in GraphQL
#[derive(Debug, Clone, Default)]
pub struct GraphQLMap<E: GraphQLMapEntry>(Vec<E>);

/// Entry for a [GraphQLMap]
pub trait GraphQLMapEntry {
    type Key: Eq + std::hash::Hash;
    type Item;

    fn new(key: Self::Key, value: Self::Item) -> Self;
    fn into_parts(self) -> (Self::Key, Self::Item);
}

/// Creates a new entry type implementing [GraphQLMapEntry] for the given type.
///
/// ## Examples
///
/// ```
/// #[derive(Clone)]
/// pub struct MyType;
///
/// graphql_starter::map_entry_for!(
///     #[derive(Clone)]
///     MyType
/// );
/// ```
/// Expands to:
/// ``` ignore
/// #[derive(Clone)]
/// pub struct MyTypeEntry {
///     pub key: String,
///     pub value: MyType,
/// }
/// ```
///
/// You can also configure the key type or the entry type name:
/// ```
/// # use graphql_starter::map_entry_for;
/// # pub struct MyType {
/// #    value: String,
/// # }
/// map_entry_for!(MyType { key = u32 });
/// map_entry_for!(MyType { entry = MyCustomTypeEntry });
/// map_entry_for!(MyType { key = &'static str, entry = MyEntry });
/// ```
#[macro_export]
macro_rules! map_entry_for {
    ($(#[$attr:meta])* $name:ident) => {
        $crate::map_entry_for!{ $(#[$attr])* $name { key = String } }
    };

    ($(#[$attr:meta])* $name:ident { key = $key:ty }) => {
        $crate::crates::paste::paste! {
            $(#[$attr])*
            pub struct [<$name Entry>] {
                /// The entry key
                pub key: $key,
                /// The entry value
                pub value: $name,
            }
            impl $crate::graphql::GraphQLMapEntry for [<$name Entry>] {
                type Key = $key;
                type Item = $name;

                fn new(key: Self::Key, value: Self::Item) -> Self {
                    [<$name Entry>] { key, value }
                }

                fn into_parts(self) -> (Self::Key, Self::Item) {
                    (self.key, self.value)
                }
            }
        }
    };

    ($(#[$attr:meta])* $name:ident { entry = $entry:ident }) => {
        $crate::map_entry_for!{ $(#[$attr])* $name { entry = $entry, key = String } }
    };

    ($(#[$attr:meta])* $name:ident { key = $key:ty, entry = $entry:ident }) => {
        $crate::map_entry_for!{ $(#[$attr])* $name { entry = $entry, key = $key } }
    };

    ($(#[$attr:meta])* $name:ident { entry = $entry:ident, key = $key:ty }) => {
        $(#[$attr])*
        pub struct $entry {
            /// The entry key
            pub key: $key,
            /// The entry value
            pub value: $name,
        }
        impl $crate::graphql::GraphQLMapEntry for $entry {
            type Key = $key;
            type Item = $name;

            fn new(key: Self::Key, value: Self::Item) -> Self {
                $entry { key, value }
            }

            fn into_parts(self) -> (Self::Key, Self::Item) {
                (self.key, self.value)
            }
        }
    };
}

impl<E, K, T> From<HashMap<K, T>> for GraphQLMap<E>
where
    E: GraphQLMapEntry,
    K: Into<<E as GraphQLMapEntry>::Key>,
    T: Into<<E as GraphQLMapEntry>::Item>,
{
    fn from(map: HashMap<K, T>) -> Self {
        GraphQLMap(map.into_iter().map(|(k, v)| E::new(k.into(), v.into())).collect())
    }
}

impl<E> GraphQLMap<E>
where
    E: GraphQLMapEntry,
{
    /// Try to build a new [GraphQLMap] from a [HashMap]
    pub fn try_from<K, V>(map: HashMap<K, V>) -> Result<Self, Box<Error>>
    where
        K: Eq + std::hash::Hash + Display,
        K: TryInto<<E as GraphQLMapEntry>::Key>,
        <K as TryInto<<E as GraphQLMapEntry>::Key>>::Error: Display + Send + Sync + 'static,
        V: TryInto<<E as GraphQLMapEntry>::Item>,
        <V as TryInto<<E as GraphQLMapEntry>::Item>>::Error: Display + Send + Sync + 'static,
    {
        let mut vec = Vec::with_capacity(map.len());
        for (key, value) in map.into_iter() {
            let key = key.try_into().map_to_internal_err("Invalid map key")?;
            let value = value.try_into().map_to_internal_err("Invalid map value")?;

            vec.push(E::new(key, value));
        }
        Ok(GraphQLMap(vec))
    }
}

impl<E, K, V> TryFrom<GraphQLMap<E>> for HashMap<K, V>
where
    E: GraphQLMapEntry,
    K: Eq + std::hash::Hash + Display,
    <E as GraphQLMapEntry>::Key: TryInto<K>,
    <<E as GraphQLMapEntry>::Key as TryInto<K>>::Error: Display + Send + Sync + 'static,
    <E as GraphQLMapEntry>::Item: TryInto<V>,
    <<E as GraphQLMapEntry>::Item as TryInto<V>>::Error: Display + Send + Sync + 'static,
{
    type Error = Box<Error>;

    fn try_from(value: GraphQLMap<E>) -> Result<Self, Self::Error> {
        let mut map = HashMap::<K, V>::with_capacity(value.0.len());
        for e in value.0.into_iter() {
            let (key, value) = e.into_parts();

            let key = key
                .try_into()
                .map_to_err(GenericErrorCode::BadRequest, "Invalid map key")?;
            let value = value
                .try_into()
                .map_to_err(GenericErrorCode::BadRequest, "Invalid map value")?;

            #[allow(clippy::map_entry)] // If we insert first, we no longer have the key to generate the error message
            if map.contains_key(&key) {
                return Err((GenericErrorCode::BadRequest, format!("Duplicated key: {key}")).into());
            } else {
                map.insert(key, value);
            }
        }
        Ok(map)
    }
}

#[async_trait::async_trait]
impl<T: OutputType + GraphQLMapEntry> OutputType for GraphQLMap<T> {
    fn type_name() -> Cow<'static, str> {
        Cow::Owned(format!("[{}]", T::qualified_type_name()))
    }

    fn qualified_type_name() -> String {
        format!("[{}]!", T::qualified_type_name())
    }

    fn create_type_info(registry: &mut Registry) -> String {
        T::create_type_info(registry);
        Self::qualified_type_name()
    }

    async fn resolve(&self, ctx: &ContextSelectionSet<'_>, field: &Positioned<Field>) -> ServerResult<Value> {
        resolve_list(ctx, field, &self.0, Some(self.0.len())).await
    }
}
impl<T: InputType + GraphQLMapEntry> InputType for GraphQLMap<T> {
    type RawValueType = Self;

    fn type_name() -> Cow<'static, str> {
        Cow::Owned(format!("[{}]", T::qualified_type_name()))
    }

    fn qualified_type_name() -> String {
        format!("[{}]!", T::qualified_type_name())
    }

    fn create_type_info(registry: &mut Registry) -> String {
        T::create_type_info(registry);
        Self::qualified_type_name()
    }

    fn parse(value: Option<Value>) -> InputValueResult<Self> {
        match value.unwrap_or_default() {
            Value::List(values) => {
                let list: Vec<_> = values
                    .into_iter()
                    .map(|value| InputType::parse(Some(value)))
                    .collect::<Result<_, _>>()
                    .map_err(InputValueError::propagate)?;

                Ok(GraphQLMap(list))
            }
            value => {
                let list = vec![InputType::parse(Some(value)).map_err(InputValueError::propagate)?];
                Ok(GraphQLMap(list))
            }
        }
    }

    fn to_value(&self) -> Value {
        Value::List(self.0.iter().map(InputType::to_value).collect())
    }

    fn as_raw_value(&self) -> Option<&Self::RawValueType> {
        Some(self)
    }
}