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
use crate::{generic::Value, http::Body, rpc::TypedData};

/// Represents a generic input binding.
///
/// The following binding attributes are supported:
///
/// | Name                    | Description                                                                                                                |
/// |-------------------------|----------------------------------------------------------------------------------------------------------------------------|
/// | `type`                  | The binding type.                                                                                                          |
/// | `name`                  | The name of the parameter being bound.                                                                                     |
/// | `*`                     | The additional binding attributes specific to the binding type. Supported value types are strings, booleans, and integers. |
///
/// # Examples
///
/// An example of using a `GenericInput` binding instead of a `CosmosDbDocument` binding:
///
/// ```rust
/// use azure_functions::{
///     bindings::{GenericInput, HttpRequest, HttpResponse},
///     func,
///     generic::Value,
/// };
/// use serde_json::from_str;
///
/// #[func]
/// #[binding(name = "req", route = "read/{id}")]
/// #[binding(
///     type = "cosmosDB",
///     name = "document",
///     connectionStringSetting = "connection",
///     databaseName = "exampledb",
///     collectionName = "documents",
///     id = "{id}",
///     partitionKey = "{id}"
/// )]
/// pub fn read_document(req: HttpRequest, document: GenericInput) -> HttpResponse {
///     match document.data {
///         Value::String(s) => {
///             let v: serde_json::Value = from_str(&s).expect("expected JSON data");
///             if v.is_null() {
///                 format!(
///                     "Document with id '{}' does not exist.",
///                     req.route_params().get("id").unwrap()
///                 )
///                 .into()
///             } else {
///                 v.into()
///             }
///         }
///         _ => panic!("expected string for CosmosDB document data"),
///     }
/// }
/// ```
#[derive(Debug, Clone)]
pub struct GenericInput {
    /// The input binding data.
    pub data: Value,
}

impl<'a> Into<Body<'a>> for GenericInput {
    fn into(self) -> Body<'a> {
        self.data.into()
    }
}

#[doc(hidden)]
impl From<TypedData> for GenericInput {
    fn from(data: TypedData) -> Self {
        GenericInput { data: data.into() }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::rpc::typed_data::Data;
    use serde_json::json;

    #[test]
    fn it_converts_from_typed_data() {
        let binding: GenericInput = TypedData {
            data: Some(Data::Json(r#"{ "foo": "bar" }"#.to_string())),
        }
        .into();

        assert_eq!(binding.data, Value::Json(json!({ "foo": "bar" })));
    }
}