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
use crate::rpc::protocol;
use serde_json::{from_str, Map, Value};
use std::fmt;

/// Represents an Azure Storage table input or output binding.
///
/// # Examples
///
/// Read a table storage row based on a key posted to the `example` queue:
///
/// ```rust
/// # extern crate azure_functions;
/// # #[macro_use] extern crate log;
/// use azure_functions::bindings::{QueueTrigger, Table};
/// use azure_functions::func;
///
/// #[func]
/// #[binding(name = "trigger", queue_name = "example")]
/// #[binding(name = "table", table_name = "MyTable", partition_key = "MyPartition", row_key = "{queueTrigger}")]
/// pub fn log_row(trigger: &QueueTrigger, table: &Table) {
///     info!("Row: {:?}", table.rows().nth(0));
/// }
/// ```
/// Run an Azure Storage table query based on a HTTP request:
///
/// ```rust
/// # extern crate azure_functions;
/// # #[macro_use] extern crate log;
/// use azure_functions::bindings::{HttpRequest, Table};
/// use azure_functions::func;
///
/// #[func]
/// #[binding(name = "table", table_name = "MyTable", filter = "{filter}")]
/// pub fn log_rows(req: &HttpRequest, table: &Table) {
///     for row in table.rows() {
///         info!("Row: {:?}", row);
///     }
/// }
#[derive(Default, Debug, Clone)]
pub struct Table(Value);

/// Represents the data of an Azure Storage table row.
pub type Row = Map<String, Value>;

impl Table {
    /// Creates a new table binding.
    ///
    /// The new table binding can be used for output.
    pub fn new() -> Table {
        Table(Value::Array(Vec::new()))
    }

    /// Gets whether or not the table binding is empty (no rows).
    pub fn is_empty(&self) -> bool {
        self.0.as_array().unwrap().is_empty()
    }

    /// Gets the current length of the rows stored in the table binding.
    pub fn len(&self) -> usize {
        self.0.as_array().unwrap().len()
    }

    /// Gets the iterator over the rows stored in the table binding.
    ///
    /// For input bindings, this will be the rows returned from either a single entity lookup
    /// or a filter query.
    ///
    /// For output bindings, this will be the rows that have been added to the table binding.
    pub fn rows(&self) -> impl Iterator<Item = &Row> {
        self.0
            .as_array()
            .unwrap()
            .iter()
            .map(|x| x.as_object().unwrap())
    }

    /// Adds a new row to the table binding with the specified partition and row keys.
    pub fn add_row(&mut self, partition_key: &str, row_key: &str) -> &mut Row {
        let array = self.0.as_array_mut().unwrap();

        array.push(json!({
            "PartitionKey": partition_key,
            "RowKey": row_key
        }));

        array.last_mut().unwrap().as_object_mut().unwrap()
    }

    /// Adds a row as a value to the table.
    pub fn add_row_value(&mut self, value: Value) {
        let array = self.0.as_array_mut().unwrap();

        array.push(value);
    }

    /// Gets the table as a JSON value.
    pub fn as_value(&self) -> &Value {
        &self.0
    }

    /// Converts the table binding to a JSON value.
    pub fn into_value(self) -> Value {
        self.0
    }
}

impl fmt::Display for Table {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<protocol::TypedData> for Table {
    fn from(data: protocol::TypedData) -> Self {
        if data.has_json() {
            let mut rows: Value =
                from_str(data.get_json()).expect("expected valid JSON data for table binding");

            if rows.is_object() {
                rows = Value::Array(vec![rows]);
            }

            if !rows.is_array() {
                panic!("expected an object or array for table binding data");
            }

            Table(rows)
        } else {
            Table::new()
        }
    }
}

impl Into<protocol::TypedData> for Table {
    fn into(self) -> protocol::TypedData {
        let mut data = protocol::TypedData::new();
        data.set_json(self.0.to_string());
        data
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fmt::Write;

    #[test]
    fn it_constructs_an_empty_table() {
        let table = Table::new();
        assert_eq!(table.len(), 0);
        assert_eq!(table.rows().count(), 0);
        assert!(table.is_empty());
    }

    #[test]
    fn it_is_not_empty_when_rows_are_present() {
        let mut table = Table::new();
        table.add_row("partition1", "row1");
        assert!(!table.is_empty());
    }

    #[test]
    fn it_has_a_length_equal_to_number_of_rows() {
        let mut table = Table::new();
        assert_eq!(table.len(), 0);
        table.add_row("partition1", "row1");
        table.add_row("partition2", "row2");
        table.add_row("partition3", "row3");
        assert_eq!(table.len(), 3);
    }

    #[test]
    fn it_iterates_rows() {
        let mut table = Table::new();
        assert_eq!(table.len(), 0);
        table.add_row("partition1", "row1");
        table.add_row("partition2", "row2");
        table.add_row("partition3", "row3");
        assert_eq!(table.len(), 3);

        for (i, row) in table.rows().enumerate() {
            assert_eq!(
                row.get("PartitionKey").unwrap().as_str().unwrap(),
                format!("partition{}", i + 1)
            );
            assert_eq!(
                row.get("RowKey").unwrap().as_str().unwrap(),
                format!("row{}", i + 1)
            );
        }
    }

    #[test]
    fn it_adds_row_value() {
        let mut table = Table::new();
        assert_eq!(table.len(), 0);
        table.add_row_value(json!({
            "PartitionKey": "partition1",
            "RowKey": "row1",
            "data": "hello world"
        }));

        assert_eq!(
            table.to_string(),
            r#"[{"PartitionKey":"partition1","RowKey":"row1","data":"hello world"}]"#
        );
    }

    #[test]
    fn it_casts_to_value_reference() {
        let mut table = Table::new();
        table.add_row("partition1", "row1");

        assert_eq!(
            table.as_value().to_string(),
            r#"[{"PartitionKey":"partition1","RowKey":"row1"}]"#
        );
    }

    #[test]
    fn it_converts_to_value() {
        let mut table = Table::new();
        table.add_row("partition1", "row1");

        assert_eq!(
            table.into_value().to_string(),
            r#"[{"PartitionKey":"partition1","RowKey":"row1"}]"#
        );
    }

    #[test]
    fn it_displays_as_a_string() {
        let mut table = Table::new();
        {
            let row = table.add_row("partition1", "row1");
            row.insert("data".to_string(), Value::String("value".to_string()));
        }
        let mut s = String::new();
        write!(s, "{}", table).unwrap();

        assert_eq!(
            s,
            r#"[{"PartitionKey":"partition1","RowKey":"row1","data":"value"}]"#
        );
    }

    #[test]
    fn it_converts_from_typed_data() {
        const TABLE: &'static str =
            r#"[{"PartitionKey":"partition1","RowKey":"row1","data":"value"}]"#;

        let mut data = protocol::TypedData::new();
        data.set_json(TABLE.to_string());

        let table: Table = data.into();
        assert_eq!(table.len(), 1);
        assert_eq!(table.to_string(), TABLE);

        let mut data = protocol::TypedData::new();
        data.set_string("".to_string());

        let table: Table = data.into();
        assert_eq!(table.len(), 0);
        assert!(table.is_empty());
    }

    #[test]
    fn it_converts_to_typed_data() {
        let mut table = Table::new();
        {
            let row = table.add_row("partition1", "row1");
            row.insert("data".to_string(), Value::String("value".to_string()));
        }
        let data: protocol::TypedData = table.into();
        assert!(data.has_json());
        assert_eq!(
            data.get_json(),
            r#"[{"PartitionKey":"partition1","RowKey":"row1","data":"value"}]"#
        );
    }
}