azure_functions/bindings/
generic_input.rs

1use crate::{generic::Value, http::Body, rpc::TypedData};
2
3/// Represents a generic input binding.
4///
5/// The following binding attributes are supported:
6///
7/// | Name                    | Description                                                                                                                |
8/// |-------------------------|----------------------------------------------------------------------------------------------------------------------------|
9/// | `type`                  | The binding type.                                                                                                          |
10/// | `name`                  | The name of the parameter being bound.                                                                                     |
11/// | `*`                     | The additional binding attributes specific to the binding type. Supported value types are strings, booleans, and integers. |
12///
13/// # Examples
14///
15/// An example of using a `GenericInput` binding instead of a `CosmosDbDocument` binding:
16///
17/// ```rust
18/// use azure_functions::{
19///     bindings::{GenericInput, HttpRequest, HttpResponse},
20///     func,
21///     generic::Value,
22/// };
23/// use serde_json::from_str;
24///
25/// #[func]
26/// #[binding(name = "req", route = "read/{id}")]
27/// #[binding(
28///     type = "cosmosDB",
29///     name = "document",
30///     connectionStringSetting = "connection",
31///     databaseName = "exampledb",
32///     collectionName = "documents",
33///     id = "{id}",
34///     partitionKey = "{id}"
35/// )]
36/// pub fn read_document(req: HttpRequest, document: GenericInput) -> HttpResponse {
37///     match document.data {
38///         Value::String(s) => {
39///             let v: serde_json::Value = from_str(&s).expect("expected JSON data");
40///             if v.is_null() {
41///                 format!(
42///                     "Document with id '{}' does not exist.",
43///                     req.route_params().get("id").unwrap()
44///                 )
45///                 .into()
46///             } else {
47///                 v.into()
48///             }
49///         }
50///         _ => panic!("expected string for CosmosDB document data"),
51///     }
52/// }
53/// ```
54#[derive(Debug, Clone)]
55pub struct GenericInput {
56    /// The input binding data.
57    pub data: Value,
58}
59
60impl<'a> Into<Body<'a>> for GenericInput {
61    fn into(self) -> Body<'a> {
62        self.data.into()
63    }
64}
65
66#[doc(hidden)]
67impl From<TypedData> for GenericInput {
68    fn from(data: TypedData) -> Self {
69        GenericInput { data: data.into() }
70    }
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76    use crate::rpc::typed_data::Data;
77    use serde_json::json;
78
79    #[test]
80    fn it_converts_from_typed_data() {
81        let binding: GenericInput = TypedData {
82            data: Some(Data::Json(r#"{ "foo": "bar" }"#.to_string())),
83        }
84        .into();
85
86        assert_eq!(binding.data, Value::Json(json!({ "foo": "bar" })));
87    }
88}