dynamodb_marshall 1.106.0

Serialize and deserialize AttributeValue to Value
Documentation
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#![doc = include_str!("../README.md")]

pub mod dynamodb {
    use std::collections::HashMap;
    use std::error::Error;

    use aws_sdk_dynamodb::types::AttributeValue;
    use serde::Serialize;
    use serde::de::DeserializeOwned;
    use serde_json::{Map, Number, Value};

    pub fn marshall_t<T: Serialize>(m: &T) -> Result<AttributeValue, Box<dyn Error>> {
        let m = serde_json::to_value(m)?;
        Ok(marshall(&m))
    }

    pub fn unmarshall_t<T: DeserializeOwned>(m: &AttributeValue) -> Result<T, serde_json::Error> {
        let m = unmarshall(m);
        serde_json::from_value(m)
    }

    pub fn marshall(m: &Value) -> AttributeValue {
        match m {
            Value::Null => AttributeValue::Null(true),
            Value::Bool(b) => AttributeValue::Bool(*b),
            Value::Number(n) => AttributeValue::N(n.to_string()),
            Value::String(s) => AttributeValue::S(s.to_owned()),
            Value::Array(arr) => AttributeValue::L(arr.iter().map(marshall).collect()),
            Value::Object(o) => {
                let new_map = o
                    .iter()
                    .map(|(k, v)| (k.to_owned(), marshall(v)))
                    .collect::<HashMap<String, AttributeValue>>();

                AttributeValue::M(new_map)
            }
        }
    }

    pub fn unmarshall(m: &AttributeValue) -> Value {
        match m {
            AttributeValue::S(s) => Value::String(s.to_owned()),
            AttributeValue::B(blob) => Value::Array(
                blob.clone()
                    .into_inner()
                    .iter()
                    .map(|v| Value::Number(Number::from(*v)))
                    .collect::<Vec<Value>>(),
            ),
            AttributeValue::Bool(b) => Value::Bool(*b),
            AttributeValue::M(o) => {
                let new_map: Map<String, Value> = o
                    .iter()
                    .map(|(k, v)| (k.to_owned(), unmarshall(v)))
                    .collect();
                Value::Object(new_map)
            }
            AttributeValue::N(v) => {
                if v.contains('.') {
                    v.parse::<f64>().map_or_else(
                        |_| serde_json::json!(v),
                        |parsed_float| serde_json::json!(parsed_float),
                    )
                } else {
                    v.parse::<i64>().map_or_else(
                        |_| serde_json::json!(v),
                        |parsed_int| serde_json::json!(parsed_int),
                    )
                }
            }
            AttributeValue::L(arr) => {
                Value::Array(arr.iter().map(unmarshall).collect::<Vec<Value>>())
            }
            AttributeValue::Ns(arr) => Value::Array(
                arr.iter()
                    .map(|v| unmarshall(&AttributeValue::N(v.to_string())))
                    .collect::<Vec<Value>>(),
            ),
            AttributeValue::Bs(arr) => Value::Array(
                arr.iter()
                    .map(|v| unmarshall(&AttributeValue::B(v.to_owned())))
                    .collect::<Vec<Value>>(),
            ),
            AttributeValue::Ss(arr) => Value::Array(
                arr.iter()
                    .map(|s| Value::String(s.to_owned()))
                    .collect::<Vec<Value>>(),
            ),
            _ => Value::Null, // covers AttributeValue::Null(_) too
        }
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use aws_sdk_dynamodb::types::AttributeValue;
    use serde::{Deserialize, Serialize};
    use serde_json::{Value as JsonValue, json};

    use crate::dynamodb;

    #[derive(Debug, Serialize, Deserialize, Default, Eq, PartialEq, Clone)]
    struct Example {
        hello: String,
        world: bool,
        a: i32,
        some: HashMap<String, String>,
        values: Vec<HashMap<String, String>>,
        other: Example2,
        others: Vec<Example2>,
        fake: Option<Example2>,
    }

    #[derive(Debug, Serialize, Deserialize, Default, Eq, PartialEq, Clone)]
    struct Example2 {
        hola: String,
        mundo: bool,
        algo: HashMap<String, String>,
        otro: u64,
    }

    #[test]
    fn it_works() {
        let example = Example {
            hello: "world".to_string(),
            world: false,
            a: 42,
            some: HashMap::from([("some".into(), "42".into()), ("value".into(), "42".into())]),
            values: vec![
                HashMap::from([("some".into(), "42".into()), ("value".into(), "42".into())]),
                HashMap::from([("some".into(), "42".into()), ("value".into(), "42".into())]),
                HashMap::from([("some".into(), "42".into()), ("value".into(), "42".into())]),
                HashMap::from([("some".into(), "42".into()), ("value".into(), "42".into())]),
                HashMap::from([("some".into(), "42".into()), ("value".into(), "42".into())]),
            ],
            other: Example2 {
                hola: "mundo".to_string(),
                mundo: true,
                algo: HashMap::from([("some".into(), "42".into()), ("value".into(), "42".into())]),
                otro: 42,
            },
            others: vec![
                Example2 {
                    hola: "mundo1".to_string(),
                    mundo: true,
                    algo: HashMap::from([
                        ("some".into(), "42".into()),
                        ("value".into(), "42".into()),
                    ]),
                    otro: 42,
                },
                Example2 {
                    hola: "mundo2".to_string(),
                    mundo: true,
                    algo: HashMap::from([
                        ("some".into(), "42".into()),
                        ("value".into(), "42".into()),
                    ]),
                    otro: 42,
                },
                Example2 {
                    hola: "mundo3".to_string(),
                    mundo: true,
                    algo: HashMap::from([
                        ("some".into(), "42".into()),
                        ("value".into(), "42".into()),
                    ]),
                    otro: 42,
                },
                Example2 {
                    hola: "mundo4".to_string(),
                    mundo: true,
                    algo: HashMap::from([
                        ("some".into(), "42".into()),
                        ("value".into(), "42".into()),
                    ]),
                    otro: 42,
                },
                Example2 {
                    hola: "mundo5".to_string(),
                    mundo: true,
                    algo: HashMap::from([
                        ("some".into(), "42".into()),
                        ("value".into(), "42".into()),
                    ]),
                    otro: 42,
                },
            ],
            fake: None,
        };

        // Clone object for later assertion
        let example_cloned = serde_json::to_value(&example)
            .expect("Failed to de example")
            .to_string();
        // println!(">> {example_cloned}");

        // Serialize example to AttributeValue
        let attr = dynamodb::marshall_t(&example).unwrap();

        // Deserialize example back into a serde json Value
        let result = dynamodb::unmarshall(&attr);

        // Check if they are equal
        assert_eq!(result.to_string(), example_cloned);
    }

    #[test]
    fn test_simple_struct() {
        #[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
        struct SimpleStruct {
            name: String,
            age: u32,
        }

        let example = SimpleStruct {
            name: "Alice".to_owned(),
            age: 30,
        };

        let as_json = serde_json::to_value(&example).unwrap().to_string();
        let attr = dynamodb::marshall_t(&example).expect("Failed to marshall SimpleStruct");
        let round_trip = dynamodb::unmarshall(&attr);

        // Compare JSON strings
        assert_eq!(round_trip.to_string(), as_json);
    }

    #[test]
    fn test_optional_values_some() {
        #[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
        struct WithOption {
            id: u32,
            label: Option<String>,
        }

        // Here, label is Some
        let example = WithOption {
            id: 999,
            label: Some("Hello Option".to_owned()),
        };

        let as_json = serde_json::to_value(&example).unwrap().to_string();
        let attr = dynamodb::marshall_t(&example).expect("Failed to marshall WithOption");
        let round_trip = dynamodb::unmarshall(&attr);

        assert_eq!(round_trip.to_string(), as_json);
    }

    #[test]
    fn test_optional_values_none() {
        #[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
        struct WithOption {
            id: u32,
            label: Option<String>,
        }

        // Here, label is None
        let example = WithOption {
            id: 999,
            label: None,
        };

        let as_json = serde_json::to_value(&example).unwrap().to_string();
        let attr = dynamodb::marshall_t(&example).expect("Failed to marshall WithOption");
        let round_trip = dynamodb::unmarshall(&attr);

        assert_eq!(round_trip.to_string(), as_json);
    }

    #[test]
    fn test_floating_numbers() {
        #[derive(Debug, Serialize, Deserialize, PartialEq)]
        struct Floats {
            value1: f64,
            value2: f32,
        }

        let example = Floats {
            value1: 123.456,
            value2: 78.9,
        };

        let as_json = serde_json::to_value(&example).unwrap().to_string();
        let attr = dynamodb::marshall_t(&example).expect("Failed to marshall Floats");
        let round_trip = dynamodb::unmarshall(&attr);

        assert_eq!(round_trip.to_string(), as_json);
    }

    #[test]
    fn test_byte_array() {
        #[derive(Debug, Serialize, Deserialize, PartialEq)]
        struct BinaryData {
            data: Vec<u8>,
        }

        let example = BinaryData {
            data: vec![1, 2, 3, 4, 255],
        };

        let as_json = serde_json::to_value(&example).unwrap().to_string();
        let attr = dynamodb::marshall_t(&example).expect("Failed to marshall BinaryData");
        let round_trip = dynamodb::unmarshall(&attr);

        // Check if the JSON matches.
        // The byte array is stored as a JSON array of numbers, e.g., [1,2,3,4,255].
        assert_eq!(round_trip.to_string(), as_json);
    }

    #[test]
    fn test_large_integers() {
        #[derive(Debug, Serialize, Deserialize, PartialEq)]
        struct LargeIntegers {
            big_positive: i64,
            big_negative: i64,
        }

        let example = LargeIntegers {
            big_positive: 9_223_372_036_854_775_807,  // i64::MAX
            big_negative: -9_223_372_036_854_775_808, // i64::MIN
        };

        let as_json = serde_json::to_value(&example).unwrap().to_string();
        let attr = dynamodb::marshall_t(&example).expect("Failed to marshall LargeIntegers");
        let round_trip = dynamodb::unmarshall(&attr);

        assert_eq!(round_trip.to_string(), as_json);
    }

    #[test]
    fn test_empty_collections() {
        #[derive(Debug, Serialize, Deserialize, PartialEq)]
        struct EmptyStruct {
            empty_vec: Vec<i32>,
            empty_map: HashMap<String, String>,
        }

        let data = EmptyStruct {
            empty_vec: vec![],
            empty_map: HashMap::new(),
        };
        let json_str = serde_json::to_value(&data).unwrap().to_string();

        let attr = dynamodb::marshall_t(&data).expect("Failed to marshall empty collections");
        let round_trip = dynamodb::unmarshall(&attr);

        assert_eq!(round_trip.to_string(), json_str);
    }

    #[test]
    fn test_dynamodb_sets() {
        let input = AttributeValue::Ss(vec![
            "apple".to_owned(),
            "banana".to_owned(),
            "cherry".to_owned(),
        ]);

        // Convert from AttributeValue to serde_json::Value
        let as_json = dynamodb::unmarshall(&input);
        // Should be an array of strings ["apple", "banana", "cherry"]
        assert_eq!(
            as_json,
            JsonValue::Array(vec![
                JsonValue::String("apple".to_string()),
                JsonValue::String("banana".to_string()),
                JsonValue::String("cherry".to_string()),
            ])
        );

        // Convert back to AttributeValue -- lossy :(
        let rem = dynamodb::marshall(&as_json);
        assert_eq!(
            rem,
            AttributeValue::L(vec![
                AttributeValue::S("apple".to_owned()),
                AttributeValue::S("banana".to_owned()),
                AttributeValue::S("cherry".to_owned()),
            ])
        );
    }

    #[test]
    fn test_unparseable_numbers() {
        let input = AttributeValue::N("123abc".to_string());

        // Dynamically convert to JSON Value
        let json_val = dynamodb::unmarshall(&input);
        // We expect it to remain a string in JSON, because it can't parse as i64 or f64
        assert_eq!(json_val, JsonValue::String("123abc".to_string()));

        // Converting back should yield the original `AttributeValue::S("123abc")`
        // Because when we marshall that JSON string, we get an S-attribute.
        let rem = dynamodb::marshall(&json_val);
        assert_eq!(rem, AttributeValue::S("123abc".to_string()));
    }

    #[test]
    fn test_out_of_range_integers() {
        // This number is larger than i64::MAX
        let input = AttributeValue::N("999999999999999999999".to_string());

        let json_val = dynamodb::unmarshall(&input);
        // Fallback is string in JSON since it can't parse into i64/f64
        assert_eq!(
            json_val,
            JsonValue::String("999999999999999999999".to_string())
        );

        // Round-trip
        let rem = dynamodb::marshall(&json_val);
        // Should become S("999999999999999999999") on re-marshall
        assert_eq!(rem, AttributeValue::S("999999999999999999999".to_string()));
    }

    #[test]
    fn test_nested_optional_fields() {
        #[derive(Debug, Serialize, Deserialize, PartialEq)]
        struct Inner {
            value: Option<String>,
        }

        #[derive(Debug, Serialize, Deserialize, PartialEq)]
        struct Outer {
            inner: Option<Inner>,
        }

        let example = Outer {
            inner: Some(Inner {
                value: None, // The inner struct is present, but the field is None
            }),
        };

        let json_str = serde_json::to_value(&example).unwrap().to_string();
        let attr = dynamodb::marshall_t(&example).expect("Failed to marshall nested optional");
        let round_trip = dynamodb::unmarshall(&attr);

        assert_eq!(round_trip.to_string(), json_str);
    }

    #[test]
    fn test_mix_null_and_valid() {
        let json_val = json!({
            "someField": null,
            "otherField": 42,
            "arrayField": [null, 1, true, "string"],
        });

        let attr = dynamodb::marshall(&json_val);
        let round_trip = dynamodb::unmarshall(&attr);

        assert_eq!(round_trip, json_val);
    }

    #[test]
    fn test_booleans_in_arrays_and_maps() {
        let json_val = json!({
            "trueVal": true,
            "falseVal": false,
            "mixedArray": [true, false, 123, "hello"],
        });

        let attr = dynamodb::marshall(&json_val);
        let round_trip = dynamodb::unmarshall(&attr);

        assert_eq!(round_trip, json_val);
    }

    #[test]
    fn test_special_strings() {
        #[derive(Debug, Serialize, Deserialize, PartialEq)]
        struct SpecialStrings {
            regular: String,
            emoji: String,
            unicode: String,
        }

        let data = SpecialStrings {
            regular: "Hello World!".to_owned(),
            emoji: "πŸ˜€πŸ”₯".to_owned(),
            unicode: "γ“γ‚“γ«γ‘γ―δΈ–η•Œ".to_owned(),
        };

        let json_str = serde_json::to_value(&data).unwrap().to_string();
        let attr = dynamodb::marshall_t(&data).expect("Failed to marshall special strings");
        let round_trip = dynamodb::unmarshall(&attr);

        assert_eq!(round_trip.to_string(), json_str);
    }

    #[test]
    fn test_mixed_array_data_types() {
        let json_val = json!(["hello", 123, true, null, {"nested": "object"}]);

        let attr = dynamodb::marshall(&json_val);
        let round_trip = dynamodb::unmarshall(&attr);

        assert_eq!(round_trip, json_val);
    }

    #[test]
    fn test_deeply_nested() {
        // 3-level nested JSON
        let json_val = json!({
            "level1": {
                "level2": {
                    "level3": {
                        "value": 999
                    }
                }
            }
        });

        let attr = dynamodb::marshall(&json_val);
        let round_trip = dynamodb::unmarshall(&attr);
        assert_eq!(round_trip, json_val);
    }

    #[test]
    fn test_zero_values() {
        #[derive(Debug, Serialize, Deserialize, PartialEq)]
        struct Zeros {
            int_zero: i32,
            float_zero: f64,
            empty_str: String,
        }

        let data = Zeros {
            int_zero: 0,
            float_zero: 0.0,
            empty_str: "".to_owned(),
        };

        let json_str = serde_json::to_value(&data).unwrap().to_string();
        let attr = dynamodb::marshall_t(&data).expect("Failed to marshall zeros");
        let round_trip = dynamodb::unmarshall(&attr);

        assert_eq!(round_trip.to_string(), json_str);
    }

    #[test]
    fn test_enums() {
        #[derive(Debug, Serialize, Deserialize, PartialEq)]
        enum SimpleEnum {
            UnitVariant,
            NewTypeVariant(i32),
            StructVariant { x: String, y: bool },
        }

        let data = SimpleEnum::StructVariant {
            x: "example".to_string(),
            y: true,
        };

        let json_str = serde_json::to_value(&data).unwrap().to_string();
        let attr = dynamodb::marshall_t(&data).expect("Failed to marshall enum");
        let round_trip = dynamodb::unmarshall(&attr);

        assert_eq!(round_trip.to_string(), json_str);
    }
}