jsonschema-transpiler 2.0.1

A tool to transpile JSON Schema into schemas for data processing
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
use super::ast;
use super::TranslateFrom;
use super::{Context, ResolveMethod};
use std::collections::HashMap;

const DEFAULT_COLUMN: &str = "root";
const MAX_DESCRIPTION_LENGTH: usize = 1024;

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "UPPERCASE", tag = "type")]
pub enum Atom {
    Int64,
    Numeric,
    Float64,
    Bool,
    String,
    Bytes,
    Date,
    Datetime,
    Geography,
    Time,
    Timestamp,
    Json,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "UPPERCASE")]
pub enum Mode {
    Nullable,
    Required,
    Repeated,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "type", rename = "RECORD")]
pub struct Record {
    #[serde(with = "fields_as_vec")]
    fields: HashMap<String, Box<Tag>>,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum Type {
    Atom(Atom),
    Record(Record),
}

/// See: https://cloud.google.com/bigquery/docs/schemas#standard_sql_data_types
#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "type")]
pub struct Tag {
    #[serde(skip_serializing_if = "Option::is_none")]
    name: Option<String>,
    #[serde(flatten, rename = "type")]
    data_type: Box<Type>,
    mode: Mode,
    #[serde(skip_serializing_if = "Option::is_none")]
    description: Option<String>,
}

impl TranslateFrom<ast::Tag> for Tag {
    type Error = String;

    fn translate_from(tag: ast::Tag, context: &Context) -> Result<Self, Self::Error> {
        let mut tag = tag;
        tag.collapse();
        tag.infer_name(context.normalize_case);
        tag.infer_nullability(context.force_nullable);

        let fmt_reason =
            |reason: &str| -> String { format!("{} - {}", tag.fully_qualified_name(), reason) };
        let handle_error = |reason: &str| -> Result<Type, Self::Error> {
            let message = fmt_reason(reason);
            match context.resolve_method {
                ResolveMethod::Cast => {
                    warn!("{}", message);
                    Ok(Type::Atom(Atom::String))
                }
                ResolveMethod::Drop => Err(message),
                ResolveMethod::Panic => panic!("{}", message),
            }
        };

        let data_type = match &tag.data_type {
            ast::Type::Atom(atom) => Type::Atom(match atom {
                ast::Atom::Boolean => Atom::Bool,
                ast::Atom::Integer => Atom::Int64,
                ast::Atom::Number => Atom::Float64,
                ast::Atom::String => Atom::String,
                ast::Atom::Datetime => Atom::Timestamp,
                ast::Atom::Bytes => Atom::Bytes,
                ast::Atom::Json => {
                    if context.is_json_object_path(&tag.fully_qualified_name()) {
                        Atom::Json
                    } else {
                        match handle_error("json atom") {
                            Ok(_) => Atom::String,
                            Err(reason) => return Err(reason),
                        }
                    }
                }
            }),
            ast::Type::Object(object) => {
                let fields: HashMap<String, Box<Tag>> = if object.fields.is_empty() {
                    HashMap::new()
                } else {
                    object
                        .fields
                        .iter()
                        .map(|(k, v)| (k.to_string(), Tag::translate_from(*v.clone(), context)))
                        .filter(|(_, v)| v.is_ok())
                        .map(|(k, v)| (k, Box::new(v.unwrap())))
                        .collect()
                };

                if fields.is_empty() {
                    handle_error("empty object")?
                } else {
                    Type::Record(Record { fields })
                }
            }
            ast::Type::Tuple(tuple) => {
                let fields: Result<Vec<_>, _> = tuple
                    .items
                    .iter()
                    .map(|v| Tag::translate_from(v.clone(), context))
                    .collect();

                let named_fields: HashMap<String, Box<Tag>> = fields?
                    .into_iter()
                    .enumerate()
                    .map(|(i, mut v)| {
                        // The name is actually derived from the value, not from
                        // the associated key. Modify the name in place.
                        let name = format!("f{}_", i);
                        v.name = Some(name.clone());
                        (name, Box::new(v))
                    })
                    .collect();

                Type::Record(Record {
                    fields: named_fields,
                })
            }
            ast::Type::Array(array) => {
                // workaround for nested lists
                let child_is_array = matches!(&array.items.data_type, ast::Type::Array(_));
                let sub_tag = match Tag::translate_from(*array.items.clone(), context) {
                    Ok(tag) => tag,
                    Err(_) => return Err(fmt_reason("untyped array")),
                };
                if child_is_array {
                    Type::Record(Record {
                        fields: hashmap! {"list".into() => Box::new(sub_tag)},
                    })
                } else {
                    *sub_tag.data_type
                }
            }
            ast::Type::Map(map) => {
                let key = Tag::translate_from(*map.key.clone(), context).unwrap();
                let value = match Tag::translate_from(*map.value.clone(), context) {
                    Ok(tag) => Some(tag),
                    Err(_) => {
                        if context.allow_maps_without_value {
                            None
                        } else {
                            return Err(fmt_reason("untyped map value"));
                        }
                    }
                };
                let fields: HashMap<String, Box<Tag>> =
                    vec![Some(("key", key)), value.map(|v| ("value", v))]
                        .into_iter()
                        .flatten()
                        .map(|(k, v)| (k.to_string(), Box::new(v)))
                        .collect();
                Type::Record(Record { fields })
            }
            _ => handle_error("unknown type")?,
        };

        let mode = if tag.is_array() || tag.is_map() {
            Mode::Repeated
        } else if tag.is_null() || tag.nullable {
            Mode::Nullable
        } else {
            Mode::Required
        };

        // If schema uses both title and description, format description as "<title> - <description>".
        // Otherwise set description to whatever is available.
        let description = match tag.title {
            Some(title) => match tag.description {
                Some(description) => Some(format!("{} - {}", title, description)),
                None => Some(title),
            },
            None => tag.description,
        };

        // The maximum length is 1024 characters for BigQuery schemas
        let truncated_description =
            description.map(|d| d.chars().take(MAX_DESCRIPTION_LENGTH).collect());

        Ok(Tag {
            name: tag.name.clone(),
            data_type: Box::new(data_type),
            mode,
            description: truncated_description,
        })
    }
}

/// BigQuery expects a schema that begins as JSON array when creating or
/// updating a table. This enum extracts or wraps the appropriate tag generated
/// from the ast. When the root is a record, the fields will be extracted from
/// the schema that is logically equivalent to `jq '.fields'`. When it is an
/// atom, array, or map, the tag is renamed to `root` and placed as a single
/// element in a wrapped array.
#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum Schema {
    Root(Vec<Tag>),
}

impl TranslateFrom<ast::Tag> for Schema {
    type Error = &'static str;

    fn translate_from(tag: ast::Tag, context: &Context) -> Result<Self, Self::Error> {
        let mut bq_tag = Tag::translate_from(tag.clone(), context).unwrap();
        match *bq_tag.data_type {
            // Maps and arrays are both treated as a Record type with different
            // modes. These should not be extracted if they are the root-type.
            Type::Record(_) if tag.is_array() || tag.is_map() => {
                assert!(bq_tag.name.is_none());
                bq_tag.name = Some(DEFAULT_COLUMN.into());
                Ok(Schema::Root(vec![bq_tag]))
            }
            Type::Atom(_) => {
                assert!(bq_tag.name.is_none());
                bq_tag.name = Some(DEFAULT_COLUMN.into());
                Ok(Schema::Root(vec![bq_tag]))
            }
            Type::Record(record) => {
                let mut vec: Vec<_> = record.fields.into_iter().collect();
                vec.sort_by_key(|(key, _)| key.to_string());
                let columns = vec.into_iter().map(|(_, v)| *v).collect();
                Ok(Schema::Root(columns))
            }
        }
    }
}

/// Allows serialization from a HashMap to a Vector. This makes it possible to
/// traverse any given path in time linear to the depth of the schema.
///
/// See: https://serde.rs/custom-date-format.html#date-in-a-custom-format
mod fields_as_vec {
    use super::Tag;
    use serde::ser::{SerializeSeq, Serializer};
    use serde::{Deserialize, Deserializer};
    use std::collections::HashMap;
    use std::iter::FromIterator;

    pub fn serialize<S>(map: &HashMap<String, Box<Tag>>, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut seq = serializer.serialize_seq(Some(map.len()))?;
        let mut vec: Vec<(String, &Tag)> = map.iter().map(|(k, v)| (k.to_string(), &**v)).collect();
        vec.sort_by_key(|(k, _)| k.to_string());

        for (_, element) in vec {
            seq.serialize_element(element)?;
        }
        seq.end()
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<HashMap<String, Box<Tag>>, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s: Vec<Box<Tag>> = Vec::deserialize(deserializer)?;
        let map = HashMap::<String, Box<Tag>>::from_iter(s.into_iter().map(|record| {
            let name: String = record.name.clone().unwrap();
            (name, record)
        }));
        Ok(map)
    }
}

#[cfg(test)]
mod tests {
    use super::super::traits::TranslateInto;
    use super::*;
    use pretty_assertions::assert_eq;
    use serde_json::{self, json, Value};

    fn transform_tag(data: Value) -> Value {
        let context = Context {
            ..Default::default()
        };
        let ast_tag: ast::Tag = serde_json::from_value(data).unwrap();
        let bq_tag: Tag = ast_tag.translate_into(&context).unwrap();
        json!(bq_tag)
    }

    fn transform_tag_with_context(data: Value, context: &Context) -> Value {
        let ast_tag: ast::Tag = serde_json::from_value(data).unwrap();
        let bq_tag: Tag = ast_tag.translate_into(context).unwrap();
        json!(bq_tag)
    }

    fn transform_schema(data: Value) -> Value {
        let context = Context {
            ..Default::default()
        };
        let ast_tag: ast::Tag = serde_json::from_value(data).unwrap();
        let bq_tag: Schema = ast_tag.translate_into(&context).unwrap();
        json!(bq_tag)
    }

    #[test]
    fn test_serialize_atom() {
        let atom = Tag {
            name: None,
            data_type: Box::new(Type::Atom(Atom::Bool)),
            mode: Mode::Nullable,
            description: None,
        };
        let expect = json!({
            "type": "BOOL",
            "mode": "NULLABLE",
        });
        assert_eq!(expect, json!(atom))
    }

    #[test]
    fn test_deserialize_atom() {
        let atom: Tag = serde_json::from_value(json!({
            "name": "test-int",
            "mode": "REPEATED",
            "type": "INT64"
        }))
        .unwrap();

        match atom.name {
            Some(name) => assert_eq!(name, "test-int"),
            _ => panic!(),
        };
        match *atom.data_type {
            Type::Atom(Atom::Int64) => (),
            _ => panic!(),
        };
        match atom.mode {
            Mode::Repeated => (),
            _ => panic!(),
        };
    }

    #[test]
    fn test_serialize_record() {
        let atom = Tag {
            name: Some("test-int".into()),
            data_type: Box::new(Type::Atom(Atom::Int64)),
            mode: Mode::Nullable,
            description: Some("test description".to_string()),
        };

        let mut record = Record {
            fields: HashMap::new(),
        };
        record.fields.insert("test-int".into(), Box::new(atom));

        let root = Tag {
            name: None,
            data_type: Box::new(Type::Record(record)),
            mode: Mode::Nullable,
            description: Some("test description".to_string()),
        };

        let expect = json!({
            "type": "RECORD",
            "mode": "NULLABLE",
            "description": "test description",
            "fields": [{
                "name": "test-int",
                "type": "INT64",
                "mode": "NULLABLE",
                "description": "test description"
            }]
        });

        assert_eq!(expect, json!(root))
    }

    #[test]
    fn test_deserialize_record() {
        let record: Tag = serde_json::from_value(json!({
            "type": "RECORD",
            "mode": "NULLABLE",
            "fields": [{
                "name": "test-int",
                "type": "INT64",
                "mode": "NULLABLE",
                "description": "test description"
            }]
        }))
        .unwrap();

        let test_int = match &*record.data_type {
            Type::Record(record) => &record.fields["test-int"],
            _ => panic!(),
        };
        match *test_int.data_type {
            Type::Atom(Atom::Int64) => (),
            _ => panic!(),
        };

        if test_int.description.as_ref().unwrap() != "test description" {
            panic!();
        }
    }

    #[test]
    fn test_serialize_nested_record() {
        let atom = Tag {
            name: Some("test-int".into()),
            data_type: Box::new(Type::Atom(Atom::Int64)),
            mode: Mode::Nullable,
            description: Some("innermost record".to_string()),
        };

        let mut record_b = Record {
            fields: HashMap::new(),
        };
        record_b.fields.insert("test-int".into(), Box::new(atom));

        let tag_b = Tag {
            name: Some("test-record-b".into()),
            data_type: Box::new(Type::Record(record_b)),
            mode: Mode::Nullable,
            description: Some("inner record".to_string()),
        };

        let mut record_a = Record {
            fields: HashMap::new(),
        };
        record_a
            .fields
            .insert("test-record-b".into(), Box::new(tag_b));

        let root = Tag {
            name: Some("test-record-a".into()),
            data_type: Box::new(Type::Record(record_a)),
            mode: Mode::Nullable,
            description: Some("outer record".to_string()),
        };

        let expect = json!({
            "name": "test-record-a",
            "type": "RECORD",
            "mode": "NULLABLE",
            "description": "outer record",
            "fields": [{
                "name": "test-record-b",
                "type": "RECORD",
                "description": "inner record",
                "fields": [{
                    "name": "test-int",
                    "type": "INT64",
                    "mode": "NULLABLE",
                    "description": "innermost record"
                }],
                "mode": "NULLABLE"
            }]
        });

        assert_eq!(expect, json!(root))
    }

    #[test]
    fn test_deserialize_nested_record() {
        let data = json!({
            "name": "test-record-a",
            "type": "RECORD",
            "mode": "NULLABLE",
            "description": "outer record",
            "fields": [{
                "name": "test-record-b",
                "type": "RECORD",
                "description": "inner record",
                "fields": [{
                    "name": "test-int",
                    "type": "INT64",
                    "mode": "NULLABLE",
                    "description": "innermost record"
                }],
                "mode": "NULLABLE"
            }]
        });
        let record_a: Tag = serde_json::from_value(data).unwrap();
        let record_b = match &*record_a.data_type {
            Type::Record(record) => &record.fields["test-record-b"],
            _ => panic!(),
        };
        let test_int = match &*record_b.data_type {
            Type::Record(record) => &record.fields["test-int"],
            _ => panic!(),
        };
        match *test_int.data_type {
            Type::Atom(Atom::Int64) => (),
            _ => panic!(),
        }
    }

    #[test]
    fn test_from_ast_null() {
        let data = json!({
            "type": "null"
        });
        let expect = json!({
            "type": "STRING",
            "mode": "NULLABLE",
        });
        assert_eq!(expect, transform_tag(data));
    }

    #[test]
    fn test_from_ast_atom() {
        let data = json!({
            "type": {"atom": "integer"}
        });
        let expect = json!({
            "type": "INT64",
            "mode": "REQUIRED",
        });
        assert_eq!(expect, transform_tag(data));
    }

    #[test]
    fn test_from_ast_nullable_atom() {
        let data = json!({
            "type": {"atom": "integer"},
            "nullable": true,
        });
        let expect = json!({
            "type": "INT64",
            "mode": "NULLABLE",
        });
        assert_eq!(expect, transform_tag(data));
    }

    #[test]
    fn test_from_ast_union_nullable_atom() {
        let data = json!({
        "type": {
            "union": {
                "items": [
                    {"type": "null"},
                    {"type": {"atom": "integer"}},
        ]}}});
        let expect = json!({
            "type": "INT64",
            "mode": "NULLABLE",
        });
        assert_eq!(expect, transform_tag(data));
    }

    #[test]
    fn test_from_ast_object() {
        let data = json!({
        "type": {
            "object": {
                "required": ["test-atom", "test-object"],
                "fields": {
                    "test-null": {"type": "null"},
                    "test-atom": {"type": {"atom": "integer"}},
                    "test-object": {"type": {
                        "object": {
                            "fields": {
                                "test-nested-atom": {"type": {"atom": "number"}}
                            }}}}}}}});
        let expect = json!({
            "type": "RECORD",
            "mode": "REQUIRED",
            "fields": [
                {"name": "test_atom", "type": "INT64", "mode": "REQUIRED"},
                {"name": "test_null", "type": "STRING", "mode": "NULLABLE"},
                {"name": "test_object", "type": "RECORD", "mode": "REQUIRED", "fields": [
                    {"name": "test_nested_atom", "type": "FLOAT64", "mode": "NULLABLE"},
                ]},
            ]
        });
        assert_eq!(expect, transform_tag(data));
    }

    #[test]
    fn test_from_ast_array() {
        let data = json!({
        "description": "foo",
        "type": {
            "array": {
                "items": {
                    "type": {"atom": "integer"}},
        }}});
        let expect = json!({
            "type": "INT64",
            "mode": "REPEATED",
            "description": "foo",
        });
        assert_eq!(expect, transform_tag(data));
    }

    #[test]
    fn from_bigquery_array_array() {
        let data = json!({
            "type": {"array": {"items": {
                "type": {"array": {"items":
                    {"type": {"atom": "integer"}}}}}}}
        });
        let expect = json!({
            "type": "RECORD",
            "mode": "REPEATED",
            "fields": [
                {
                    "name": "list",
                    "type": "INT64",
                    "mode": "REPEATED"
                }
            ]
        });
        assert_eq!(expect, transform_tag(data));
    }

    #[test]
    fn test_from_ast_tuple() {
        let data = json!({
            "type": {
                "tuple": {
                    "items": [
                        {"type": {"atom": "boolean"}},
                        {"type": {"atom": "integer"}},
                    ]
                }
            }
        });
        let expect = json!({
            "type": "RECORD",
            "mode": "REQUIRED",
            "fields": [
                {"name": "f0_", "type": "BOOL", "mode": "REQUIRED"},
                {"name": "f1_", "type": "INT64", "mode": "REQUIRED"}
            ]
        });
        assert_eq!(expect, transform_tag(data));
    }

    #[test]
    fn test_from_ast_map() {
        let data = json!({
        "type": {
            "map": {
                "key": {"type": {"atom": "string"}},
                "value": {"type": {"atom": "integer"}}
        }}});
        let expect = json!({
        "type": "RECORD",
        "mode": "REPEATED",
        "fields": [
            {"name": "key", "type": "STRING", "mode": "REQUIRED"},
            {"name": "value", "type": "INT64", "mode": "REQUIRED"},
        ]});
        assert_eq!(expect, transform_tag(data));
    }

    #[test]
    fn test_from_ast_map_without_value() {
        let data = json!({
        "description": "foo",
        "type": {
            "map": {
                "key": {"type": {"atom": "string"}},
                "value": {"type": {"atom": "json"}},
        }}});
        let expect = json!({
        "type": "RECORD",
        "mode": "REPEATED",
        "description": "foo",
        "fields": [
            {"name": "key", "type": "STRING", "mode": "REQUIRED"},
        ]});
        let context = Context {
            resolve_method: ResolveMethod::Drop,
            normalize_case: false,
            force_nullable: false,
            tuple_struct: false,
            allow_maps_without_value: true,
            ..Default::default()
        };
        assert_eq!(expect, transform_tag_with_context(data, &context));
    }

    #[test]
    fn test_from_ast_datetime() {
        let data = json!({
            "type": {"atom": "datetime"},
            "nullable": true
        });
        let expect = json!({
           "type": "TIMESTAMP",
           "mode": "NULLABLE",
        });
        assert_eq!(expect, transform_tag(data));
    }

    #[test]
    fn test_from_ast_bytes() {
        let data = json!({
            "type": {"atom": "bytes"},
            "nullable": true
        });
        let expect = json!({
           "type": "BYTES",
           "mode": "NULLABLE",
        });
        assert_eq!(expect, transform_tag(data));
    }

    #[test]
    fn test_schema_from_ast_atom() {
        // Nameless tags are top-level fields that should be rooted by default
        let data = json!({"type": {"atom": "integer"}});
        let expect = json!([{
            "name": DEFAULT_COLUMN,
            "mode": "REQUIRED",
            "type": "INT64"
        }]);
        assert_eq!(expect, transform_schema(data));
    }

    #[test]
    fn test_schema_from_ast_object() {
        // The single column is extracted
        let data = json!({
        "type": {
            "object": {
                "required": ["test-object"],
                "fields": {
                    "test-object": {"type": {
                        "object": {
                            "required": ["test-nested-atom"],
                            "fields": {
                                "test-nested-atom": {"type": {"atom": "boolean"}}
                            }}}}}}}});
        let expect = json!([{
            "name": "test_object",
            "mode": "REQUIRED",
            "type": "RECORD",
            "fields": [
                {
                    "name": "test_nested_atom",
                    "mode": "REQUIRED",
                    "type": "BOOL"
                }
            ]
        }]);
        assert_eq!(expect, transform_schema(data));
    }

    #[test]
    fn test_schema_from_ast_map() {
        let data = json!({
        "type": {
            "map": {
                "key": {"type": {"atom": "string"}},
                "value": {"type": {"atom": "integer"}}
        }}});
        let expect = json!([{
            "name": "root",
            "type": "RECORD",
            "mode": "REPEATED",
            "fields": [
                {"name": "key", "type": "STRING", "mode": "REQUIRED"},
                {"name": "value", "type": "INT64", "mode": "REQUIRED"},
            ]}]
        );
        assert_eq!(expect, transform_schema(data));
    }
}