mig-bo4e 0.10.0

Declarative TOML-based MIG-tree to BO4E mapping engine
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
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
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
//! Static shape of the BO4E JSON the forward mapping emits for one PID.
//!
//! The forward mapping ([`MappingEngine::map_interchange`]) builds untyped JSON
//! from the mapping definitions. Code generators that emit typed views of that
//! JSON (the per-PID typed modules of `bo4e-edifact-types`) need to know every
//! shape it can take, not just what a validator requires. This module derives it
//! from the same definitions, code lookup and MIG the engine runs with, reusing
//! the engine's own path, enrichment and nesting rules:
//!
//! - each definition's `[fields]` targets become (dotted, nested) object fields;
//!   a `also_target` becomes a second field;
//! - a field is *enriched* when the engine would enrich it
//!   ([`CodeLookup::enrichment_codes`]); its values are the codes of the segment
//!   variant it reads ([`CodeLookup::field_codes`]) as written: the `enum_map`
//!   values, or the raw codes the `enum_map` does not cover;
//! - `[meta] parent_field` children become lists inside their parent object;
//! - definitions of the same entity merge (`deep_merge_insert`);
//! - an entity may be an array when any group on its source path (below the
//!   transaction root) repeats, or with `repeat_on_tag`;
//! - child entities may additionally appear inside their parent entity
//!   (`nest_child_entities_in_result`).
//!
//! [`MappingEngine::map_interchange`]: crate::MappingEngine::map_interchange

use std::collections::{BTreeMap, BTreeSet};

use mig_types::schema::mig::{MigSchema, MigSegmentGroup};

use crate::code_lists::CodeLists;
use crate::code_lookup::CodeLookup;
use crate::definition::{FieldMapping, MappingDefinition};
use crate::engine::{
    child_entity_nesting_pairs, is_nested_child_of, parse_tag_qualifier, strip_all_rep_indices,
    to_camel_case, MappingEngine, VariantCache,
};

/// Output shape of the message-level and transaction-level BO4E JSON of a PID.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct OutputShape {
    /// `Nachricht.stammdaten` (message-level entities).
    pub message: ScopeShape,
    /// One transaction's `stammdaten`.
    pub transaction: ScopeShape,
}

/// Entities of one scope, keyed by their JSON key (camelCase entity name).
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ScopeShape {
    pub entities: BTreeMap<String, EntityShape>,
}

/// One top-level entity of a scope.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct EntityShape {
    /// Entity name (`[meta] entity`, PascalCase).
    pub entity: String,
    /// Union of the objects all definitions of this entity produce.
    pub object: ObjectShape,
    /// Whether the value may be an array of objects instead of one object.
    pub repeats: bool,
    /// JSON keys of child entities that may be nested inside this entity's
    /// objects (they may also stay at the top level of the scope).
    pub children: BTreeSet<String>,
    /// The field carrying the group's entry qualifier, when every definition of
    /// the entity's top group maps it (candidate for map-keying).
    pub key: Option<KeyField>,
}

/// A JSON object: field name → field shape.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ObjectShape {
    pub fields: BTreeMap<String, FieldShape>,
}

/// Shape of one object field.
#[derive(Debug, Clone, PartialEq)]
pub enum FieldShape {
    /// A string, or (enriched) a `{code, meaning, enum}` object.
    Leaf(LeafShape),
    /// A nested object (dotted targets).
    Object(ObjectShape),
    /// A `[meta] parent_field` list of objects.
    List {
        /// BO4E type of the elements (`[meta] bo4e_type` of the child definition).
        item_type: String,
        item: ObjectShape,
    },
    /// Different definitions write different kinds of value at this field.
    Mixed,
}

/// A string-valued field.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct LeafShape {
    /// Values the engine can write, with the raw EDIFACT codes producing them.
    /// `None` when the value is free text or not enumerable.
    pub values: Option<BTreeMap<String, WrittenValue>>,
    /// Whether code enrichment can turn the value into a `{code, meaning, enum}`
    /// object (whose `code` is the written value).
    pub enriched: bool,
}

/// One value a code field can hold.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct WrittenValue {
    /// Raw EDIFACT codes the engine writes as this value.
    pub raw_codes: BTreeSet<String>,
    /// Meaning of the (first) raw code, if known.
    pub meaning: String,
    /// Curated enum name of the (first) raw code, if known.
    pub enum_key: Option<String>,
}

/// The qualifier field of an entity whose group entry segment carries the
/// qualifier as a mapped field (e.g. NAD 3035 → `partnerrolle`).
#[derive(Debug, Clone, Default, PartialEq)]
pub struct KeyField {
    /// JSON field the qualifier is written to.
    pub field: String,
    /// Second field of a dual decomposition (`also_target`), if any.
    pub also_field: Option<String>,
    /// Raw code → (value written to `field`, value written to `also_field`).
    pub codes: BTreeMap<String, (String, Option<String>)>,
    /// Schema group node(s) of the entity (source paths of its top definitions).
    pub source_paths: BTreeSet<String>,
}

impl OutputShape {
    /// Shape of what `Mapper::from_edifact` emits for `pid` of this variant.
    ///
    /// With the PID `schema` JSON, whether a group repeats follows the PID's
    /// schema (AHB-narrowed `max_reps`); without it, the MIG.
    pub fn for_pid(vc: &VariantCache, pid: &str, schema: Option<&serde_json::Value>) -> Self {
        let key = format!("pid_{pid}");
        let tx_group = vc.tx_group(pid).unwrap_or("");
        let mig = vc.filtered_mig(pid);
        let lookup = vc.code_lookups.get(&key);
        let tx_defs = vc
            .transaction_defs
            .get(&key)
            .map(Vec::as_slice)
            .unwrap_or_default();
        let sources = Sources {
            lookup,
            mig: mig.as_ref(),
            schema,
            code_lists: Some(&vc.code_lists),
        };
        Self::from_parts(&vc.message_defs, tx_defs, tx_group, &sources)
    }

    /// Shape from the message engine's and the transaction engine's definitions.
    /// `tx_group` is the transaction group id (empty for message-only variants).
    pub fn from_parts(
        message_defs: &[MappingDefinition],
        transaction_defs: &[MappingDefinition],
        tx_group: &str,
        sources: &Sources<'_>,
    ) -> Self {
        let message = ScopeShape::build(message_defs, sources, None);
        let transaction = if tx_group.is_empty() {
            ScopeShape::default()
        } else {
            ScopeShape::build(transaction_defs, sources, Some(tx_group))
        };
        Self {
            message,
            transaction,
        }
    }
}

/// What the shape is derived from besides the definitions.
#[derive(Debug, Clone, Copy, Default)]
pub struct Sources<'a> {
    /// The PID's code lookup (decides code fields and their codes).
    pub lookup: Option<&'a CodeLookup>,
    /// The PID-filtered MIG (entry segments; repetition without a schema).
    pub mig: Option<&'a MigSchema>,
    /// The PID schema JSON (repetition).
    pub schema: Option<&'a serde_json::Value>,
    /// The shared tables a rule's `code_list` / `also_code_list` name. Without
    /// them a rule that names its table instead of spelling it inline looks
    /// untranslated, and its field seems to carry raw codes.
    pub code_lists: Option<&'a CodeLists>,
}

impl ScopeShape {
    fn build(defs: &[MappingDefinition], sources: &Sources<'_>, tx_group: Option<&str>) -> Self {
        let Sources {
            lookup,
            mig,
            code_lists,
            ..
        } = *sources;
        let mut entities: BTreeMap<String, EntityShape> = BTreeMap::new();
        for def in defs.iter().filter(|d| d.meta.parent_field.is_none()) {
            let key = to_camel_case(&def.meta.entity);
            let object = object_shape(def, defs, lookup, code_lists);
            let repeats = def_repeats(def, sources, tx_group);
            let entry = entities.entry(key).or_insert_with(|| EntityShape {
                entity: def.meta.entity.clone(),
                ..Default::default()
            });
            entry.object.merge(object);
            entry.repeats |= repeats;
        }

        for (_, parent, child, _) in child_entity_nesting_pairs(defs, tx_group) {
            let child_key = to_camel_case(&child);
            if !entities.contains_key(&child_key) {
                continue;
            }
            if let Some(p) = entities.get_mut(&to_camel_case(&parent)) {
                p.children.insert(child_key);
            }
        }

        for (key, entity) in entities.iter_mut() {
            entity.key = key_field(key, defs, lookup, mig, code_lists);
        }

        Self { entities }
    }
}

impl ObjectShape {
    /// Union with `other` (`deep_merge_insert` semantics: fields of both).
    pub fn merge(&mut self, other: ObjectShape) {
        for (name, field) in other.fields {
            match self.fields.remove(&name) {
                None => {
                    self.fields.insert(name, field);
                }
                Some(existing) => {
                    self.fields.insert(name, existing.merged(field));
                }
            }
        }
    }

    fn insert_path(&mut self, path: &str, leaf: LeafShape) {
        match path.split_once('.') {
            None => {
                let merged = match self.fields.remove(path) {
                    None => FieldShape::Leaf(leaf),
                    Some(existing) => existing.merged(FieldShape::Leaf(leaf)),
                };
                self.fields.insert(path.to_string(), merged);
            }
            Some((head, rest)) => {
                let field = self
                    .fields
                    .entry(head.to_string())
                    .or_insert_with(|| FieldShape::Object(ObjectShape::default()));
                match field {
                    FieldShape::Object(obj) => obj.insert_path(rest, leaf),
                    other => *other = FieldShape::Mixed,
                }
            }
        }
    }
}

impl FieldShape {
    fn merged(self, other: FieldShape) -> FieldShape {
        match (self, other) {
            (FieldShape::Leaf(mut a), FieldShape::Leaf(b)) => {
                a.merge(b);
                FieldShape::Leaf(a)
            }
            (FieldShape::Object(mut a), FieldShape::Object(b)) => {
                a.merge(b);
                FieldShape::Object(a)
            }
            (
                FieldShape::List {
                    item_type,
                    item: mut a,
                },
                FieldShape::List { item: b, .. },
            ) => {
                a.merge(b);
                FieldShape::List { item_type, item: a }
            }
            _ => FieldShape::Mixed,
        }
    }
}

impl LeafShape {
    fn merge(&mut self, other: LeafShape) {
        self.enriched |= other.enriched;
        self.values = match (self.values.take(), other.values) {
            (Some(mut a), Some(b)) => {
                for (value, w) in b {
                    let entry = a.entry(value).or_default();
                    if entry.meaning.is_empty() {
                        entry.meaning = w.meaning;
                    }
                    if entry.enum_key.is_none() {
                        entry.enum_key = w.enum_key;
                    }
                    entry.raw_codes.extend(w.raw_codes);
                }
                Some(a)
            }
            _ => None,
        };
    }

    /// Whether the engine can write `value` here.
    pub fn may_be(&self, value: &str) -> bool {
        self.values.as_ref().map_or(true, |v| v.contains_key(value))
    }
}

/// `(element, component)` of a numeric field path like `nad.3.0`, as the engine
/// parses it.
fn path_position(path: &str) -> (String, Option<String>, (usize, usize)) {
    let parts: Vec<&str> = path.split('.').collect();
    let (tag, qualifier, _) = parse_tag_qualifier(parts[0]);
    let position = MappingEngine::parse_element_component(&parts[1..]);
    (tag, qualifier.map(String::from), position)
}

/// The object one definition produces (`extract_fields_from_instance`).
fn object_shape(
    def: &MappingDefinition,
    defs: &[MappingDefinition],
    lookup: Option<&CodeLookup>,
    code_lists: Option<&CodeLists>,
) -> ObjectShape {
    let mut obj = ObjectShape::default();
    for (path, mapping) in &def.fields {
        let (target, enum_map, also) = match mapping {
            FieldMapping::Simple(t) => (t.as_str(), None, None),
            FieldMapping::Structured(s) => (
                s.target.as_str(),
                table(code_lists, s.enum_map.as_ref(), s.code_list.as_deref()),
                s.also_target.as_deref().zip(table(
                    code_lists,
                    s.also_enum_map.as_ref(),
                    s.also_code_list.as_deref(),
                )),
            ),
            FieldMapping::Nested(_) => continue,
        };
        if target.is_empty() {
            continue;
        }
        let (tag, path_qualifier, (element, component)) = path_position(path);
        let (codes, enriched) = match (lookup, def.meta.source_path.as_deref()) {
            (Some(lookup), Some(sp)) => {
                let disc_qualifier = MappingEngine::discriminator_qualifier_for_tag(def, &tag);
                let (pq, dq) = (path_qualifier.as_deref(), disc_qualifier.as_deref());
                let enriched = lookup
                    .enrichment_codes(sp, &tag, pq, dq, element, component)
                    .is_some();
                // Codes of the segment variant the field reads (path qualifier or
                // same-tag discriminator); without one, of every variant.
                (
                    lookup.field_codes(sp, &tag, pq, dq, element, component),
                    enriched,
                )
            }
            _ => (None, false),
        };
        let values = codes.map(|codes| {
            let mut values: BTreeMap<String, WrittenValue> = BTreeMap::new();
            let raw = codes
                .iter()
                .map(|(code, e)| (code.clone(), e.meaning.clone(), e.enum_key.clone()))
                .chain(
                    enum_map
                        .into_iter()
                        .flatten()
                        .filter(|(code, _)| !codes.contains_key(*code))
                        .map(|(code, _)| (code.clone(), String::new(), None)),
                );
            for (code, meaning, enum_key) in raw {
                let written = enum_map
                    .and_then(|m| m.get(&code))
                    .cloned()
                    .unwrap_or_else(|| code.clone());
                let entry = values.entry(written).or_default();
                if entry.raw_codes.is_empty() {
                    entry.meaning = meaning;
                    entry.enum_key = enum_key;
                }
                entry.raw_codes.insert(code);
            }
            values
        });
        obj.insert_path(target, LeafShape { values, enriched });
        if let Some((also_target, also_map)) = also {
            let values = also_map
                .values()
                .map(|v| (v.clone(), WrittenValue::default()))
                .collect();
            obj.insert_path(
                also_target,
                LeafShape {
                    values: Some(values),
                    enriched: false,
                },
            );
        }
    }

    for child in defs.iter().filter(|c| is_nested_child_of(c, def)) {
        let field = child.meta.parent_field.clone().unwrap_or_default();
        let list = FieldShape::List {
            item_type: child.meta.bo4e_type.clone(),
            item: object_shape(child, defs, lookup, code_lists),
        };
        let merged = match obj.fields.remove(&field) {
            None => list,
            Some(existing) => existing.merged(list),
        };
        obj.fields.insert(field, merged);
    }
    obj
}

/// The table a rule translates through, as the engine resolves it
/// ([`CodeLists::resolve`]). Without the shared lists only an inline table counts.
fn table<'a>(
    code_lists: Option<&'a CodeLists>,
    inline: Option<&'a BTreeMap<String, String>>,
    named: Option<&str>,
) -> Option<&'a BTreeMap<String, String>> {
    match code_lists {
        Some(lists) => lists.resolve(inline, named),
        None => inline,
    }
}

/// The MIG group at `source_group` (rep indices stripped), with the groups on
/// the way there.
fn group_chain<'a>(mig: &'a MigSchema, source_group: &str) -> Option<Vec<&'a MigSegmentGroup>> {
    let mut chain = Vec::new();
    let mut groups = &mig.segment_groups;
    for part in strip_all_rep_indices(source_group).split('.') {
        let group = groups.iter().find(|g| g.id.eq_ignore_ascii_case(part))?;
        chain.push(group);
        groups = &group.nested_groups;
    }
    Some(chain)
}

/// Whether one definition can produce several objects for one scope instance.
fn def_repeats(def: &MappingDefinition, sources: &Sources<'_>, tx_group: Option<&str>) -> bool {
    if def.meta.source_group.is_empty() {
        return false;
    }
    if def.meta.repeat_on_tag.is_some() {
        return true;
    }
    let disc = MappingEngine::discriminator_qualifier(def);
    let mig_repeats = || {
        let Some(chain) = sources
            .mig
            .and_then(|m| group_chain(m, &def.meta.source_group))
        else {
            return true;
        };
        chain.iter().enumerate().any(|(i, g)| {
            let is_tx_root = i == 0 && tx_group.is_some_and(|tx| g.id.eq_ignore_ascii_case(tx));
            !is_tx_root && g.max_rep_std.max(g.max_rep_spec) > 1
        })
    };
    let (Some(schema), Some(sp)) = (sources.schema, def.meta.source_path.as_deref()) else {
        return mig_repeats();
    };
    match schema_path_repeats(schema, sp, tx_group, disc.as_deref()) {
        // A group the PID schema lacks is absent from the PID-filtered tree, so
        // the definition produces nothing.
        None => false,
        Some(true) => true,
        // A discriminator selects one AHB group variant, whose repetition the
        // schema states. Without one, the definition takes every repetition the
        // MIG allows (schema nodes can merge several AHB occurrences).
        Some(false) => disc.is_none() && mig_repeats(),
    }
}

/// Whether any PID schema group on `source_path` (below the transaction root)
/// allows more than one repetition. An unqualified part (`sg12`) stands for all
/// of its qualified variants (`sg12_z04`, ...). `None` if the path is not in
/// the schema.
fn schema_path_repeats(
    schema: &serde_json::Value,
    source_path: &str,
    tx_group: Option<&str>,
    discriminator: Option<&str>,
) -> Option<bool> {
    let mut level: Vec<&serde_json::Value> = vec![schema.get("fields")?];
    let mut repeats = false;
    let parts: Vec<&str> = source_path.split('.').collect();
    for (i, part) in parts.iter().copied().enumerate() {
        let mut next: Vec<&serde_json::Value> = map_names(&level, part)
            .into_iter()
            .map(|(_, node)| node)
            .collect();
        if next.is_empty() {
            return None;
        }
        // The last part of a discriminated definition only selects the variant
        // carrying its qualifier.
        if i == parts.len() - 1 && !part.contains('_') {
            if let Some(q) = discriminator.map(|q| format!("_{}", q.to_lowercase())) {
                let selected: Vec<&serde_json::Value> = map_names(&level, part)
                    .into_iter()
                    .filter(|(name, _)| name.to_lowercase().ends_with(&q))
                    .map(|(_, node)| node)
                    .collect();
                if !selected.is_empty() {
                    next = selected;
                }
            }
        }
        let is_tx_root = i == 0 && tx_group.is_some_and(|tx| part.eq_ignore_ascii_case(tx));
        if !is_tx_root {
            // Several variants of an unqualified group each contribute instances.
            repeats |= next.len() > 1;
            let last_discriminated = i == parts.len() - 1 && discriminator.is_some();
            repeats |= next.iter().any(|node| {
                node.get("max_reps")
                    .and_then(|v| v.as_u64())
                    .map_or(true, |max| max > 1)
                    // A node merging several AHB group occurrences lists their
                    // entry segments side by side; each is its own repetition.
                    || (!last_discriminated && entry_segment_count(node) > 1)
            });
        }
        level = next
            .iter()
            .filter_map(|node| node.get("children"))
            .collect();
    }
    Some(repeats)
}

/// Number of entry segments (segments with the first segment's id) a schema
/// group node lists.
fn entry_segment_count(node: &serde_json::Value) -> usize {
    let Some(segments) = node.get("segments").and_then(|v| v.as_array()) else {
        return 0;
    };
    let entry = segments.first().and_then(|s| s.get("id"));
    segments.iter().filter(|s| s.get("id") == entry).count()
}

/// Schema group nodes of `containers` matching a source path part: the node of
/// that name, or for an unqualified part (`sg12`) all its variants (`sg12_z04`).
fn map_names<'a>(
    containers: &[&'a serde_json::Value],
    part: &str,
) -> Vec<(&'a String, &'a serde_json::Value)> {
    let prefix = format!("{part}_");
    containers
        .iter()
        .filter_map(|c| c.as_object())
        .flatten()
        .filter(|(name, _)| {
            name.eq_ignore_ascii_case(part) || (!part.contains('_') && name.starts_with(&prefix))
        })
        .collect()
}

/// Qualifier field of `entity_key`: every definition at the entity's top group
/// maps the entry segment's qualifier (element 0, component 0) to the same field.
fn key_field(
    entity_key: &str,
    defs: &[MappingDefinition],
    lookup: Option<&CodeLookup>,
    mig: Option<&MigSchema>,
    code_lists: Option<&CodeLists>,
) -> Option<KeyField> {
    let mig = mig?;
    let entity_defs: Vec<&MappingDefinition> = defs
        .iter()
        .filter(|d| d.meta.parent_field.is_none() && to_camel_case(&d.meta.entity) == entity_key)
        .collect();
    let depth = |d: &MappingDefinition| d.meta.source_group.split('.').count();
    let top = entity_defs.iter().map(|d| depth(d)).min()?;
    let mut key: Option<KeyField> = None;
    for def in entity_defs.iter().filter(|d| depth(d) == top) {
        if def.meta.source_group.is_empty() || def.meta.discriminator.is_some() {
            return None;
        }
        let chain = group_chain(mig, &def.meta.source_group)?;
        let entry_tag = chain.last()?.segments.first()?.id.to_uppercase();
        let (field, enum_map, also) = def.fields.iter().find_map(|(path, mapping)| {
            let (tag, qualifier, position) = path_position(path);
            let numeric = path.split('.').skip(1).all(|p| p.parse::<usize>().is_ok());
            if tag != entry_tag || qualifier.is_some() || !numeric || position != (0, 0) {
                return None;
            }
            match mapping {
                FieldMapping::Simple(t) if !t.is_empty() => Some((t.clone(), None, None)),
                FieldMapping::Structured(s) if !s.target.is_empty() => Some((
                    s.target.clone(),
                    table(code_lists, s.enum_map.as_ref(), s.code_list.as_deref()),
                    s.also_target.clone().zip(table(
                        code_lists,
                        s.also_enum_map.as_ref(),
                        s.also_code_list.as_deref(),
                    )),
                )),
                _ => None,
            }
        })?;
        let also_field = also.as_ref().map(|(f, _)| f.clone());
        let entry = key.get_or_insert_with(|| KeyField {
            field: field.clone(),
            also_field: also_field.clone(),
            ..Default::default()
        });
        if entry.field != field || entry.also_field != also_field {
            return None;
        }
        let mut raw: BTreeSet<String> = enum_map
            .into_iter()
            .flatten()
            .map(|(c, _)| c.clone())
            .collect();
        if let (Some(lookup), Some(sp)) = (lookup, def.meta.source_path.as_deref()) {
            if let Some(codes) = lookup.codes_q(sp, &entry_tag, None, 0, 0) {
                raw.extend(codes.keys().cloned());
            }
            entry.source_paths.insert(sp.to_string());
        }
        for code in raw {
            let written = enum_map
                .and_then(|m| m.get(&code))
                .cloned()
                .unwrap_or_else(|| code.clone());
            let also_value = also.as_ref().and_then(|(_, m)| m.get(&code).cloned());
            let value = (written, also_value);
            match entry.codes.get(&code) {
                Some(existing) if *existing != value => return None,
                _ => {
                    entry.codes.insert(code, value);
                }
            }
        }
    }
    key.filter(|k| !k.codes.is_empty())
}

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

    fn def(toml: &str) -> MappingDefinition {
        MappingDefinition::from_toml_str(toml).unwrap()
    }

    fn nad_node(code: &str, max_reps: u64) -> serde_json::Value {
        json!({
            "max_reps": max_reps,
            "discriminator": {"segment": "NAD", "element": "3035", "values": [code]},
            "segments": [{"id": "NAD", "elements": [
                {"index": 0, "id": "3035", "type": "code",
                 "codes": [{"value": code, "name": format!("Rolle {code}")}]}
            ]}]
        })
    }

    fn schema(sg12_max_reps: u64) -> serde_json::Value {
        json!({"fields": {
            "sg2": {"max_reps": 2, "segments": [], "children": {}},
            "sg4": {"max_reps": 99999, "segments": [], "children": {
                "sg5_z16": {"max_reps": 999999, "segments": []},
                "sg6": {"max_reps": 1, "segments": []},
                "sg12_z07": nad_node("Z07", sg12_max_reps),
                "sg12_z39": nad_node("Z39", sg12_max_reps),
                "sg12_z40": nad_node("Z40", sg12_max_reps),
            }}
        }})
    }

    fn mig() -> MigSchema {
        let segment = |id: &str| {
            json!({"id": id, "name": id, "description": null, "counter": null, "level": 1,
                   "number": null, "max_rep_std": 1, "max_rep_spec": 1, "status_std": null,
                   "status_spec": null, "example": null, "data_elements": [], "composites": []})
        };
        let group = |id: &str, entry: &str, nested: Vec<serde_json::Value>| {
            json!({"id": id, "name": id, "description": null, "counter": null, "level": 1,
                   "max_rep_std": 99, "max_rep_spec": 99, "status_std": null, "status_spec": null,
                   "segments": [segment(entry)], "nested_groups": nested})
        };
        serde_json::from_value(json!({
            "message_type": "UTILMD", "variant": null, "version": "", "publication_date": "",
            "author": "", "format_version": "FV2604", "source_file": "", "segments": [],
            "segment_groups": [group("SG4", "IDE", vec![
                group("SG12", "NAD", vec![group("SG13", "CTA", vec![])]),
            ])]
        }))
        .unwrap()
    }

    const GESCHAEFTSPARTNER: &str = r#"
[meta]
entity = "Geschaeftspartner"
bo4e_type = "Geschaeftspartner"
source_group = "SG4.SG12"
source_path = "sg4.sg12"

[fields]
"nad.3.0" = "name1"
"nad.0" = { target = "partnerrolle", enum_map = { "Z07" = "kundeMsb", "Z39" = "kundeLf", "Z40" = "kundeLf" }, also_target = "datenqualitaet", also_enum_map = { "Z39" = "base", "Z40" = "informative" } }
"#;

    const KONTAKTWEG: &str = r#"
[meta]
entity = "Geschaeftspartner"
bo4e_type = "Kontaktweg"
source_group = "SG4.SG12.SG13"
source_path = "sg4.sg12.sg13"
parent_field = "kontaktwege"

[fields]
"com.0.0" = "kontaktwert"
"#;

    fn shape(sg12_max_reps: u64) -> OutputShape {
        let schema = schema(sg12_max_reps);
        let lookup = CodeLookup::from_schema_value(&schema);
        let mig = mig();
        let defs = vec![def(GESCHAEFTSPARTNER), def(KONTAKTWEG)];
        OutputShape::from_parts(
            &[],
            &defs,
            "SG4",
            &Sources {
                lookup: Some(&lookup),
                mig: Some(&mig),
                schema: Some(&schema),
                code_lists: None,
            },
        )
    }

    #[test]
    fn code_field_values_are_written_values_with_raw_code_aliases() {
        let shape = shape(1);
        let gp = &shape.transaction.entities["geschaeftspartner"];
        let Some(FieldShape::Leaf(rolle)) = gp.object.fields.get("partnerrolle") else {
            panic!("partnerrolle leaf: {gp:#?}");
        };
        assert!(rolle.enriched, "a schema code field can be enriched");
        let values = rolle.values.as_ref().unwrap();
        let raw = |v: &str| values[v].raw_codes.iter().cloned().collect::<Vec<_>>();
        assert_eq!(values.keys().collect::<Vec<_>>(), ["kundeLf", "kundeMsb"]);
        assert_eq!(raw("kundeLf"), ["Z39", "Z40"]);
        assert_eq!(raw("kundeMsb"), ["Z07"]);
        assert_eq!(values["kundeMsb"].meaning, "Rolle Z07");

        let Some(FieldShape::Leaf(dq)) = gp.object.fields.get("datenqualitaet") else {
            panic!("datenqualitaet leaf");
        };
        assert!(!dq.enriched, "also_target values are never enriched");
        let dq_values: Vec<&String> = dq.values.as_ref().unwrap().keys().collect();
        assert_eq!(dq_values, ["base", "informative"]);

        assert!(
            matches!(gp.object.fields.get("name1"), Some(FieldShape::Leaf(l)) if l.values.is_none())
        );
        assert!(matches!(
            gp.object.fields.get("kontaktwege"),
            Some(FieldShape::List { item_type, item }) if item_type == "Kontaktweg"
                && item.fields.contains_key("kontaktwert")
        ));
    }

    #[test]
    fn key_field_records_joint_qualifier_decomposition() {
        let shape = shape(1);
        let key = shape.transaction.entities["geschaeftspartner"]
            .key
            .clone()
            .expect("key field");
        assert_eq!(key.field, "partnerrolle");
        assert_eq!(key.also_field.as_deref(), Some("datenqualitaet"));
        let codes: Vec<(&str, &str, Option<&str>)> = key
            .codes
            .iter()
            .map(|(c, (w, a))| (c.as_str(), w.as_str(), a.as_deref()))
            .collect();
        assert_eq!(
            codes,
            [
                ("Z07", "kundeMsb", None),
                ("Z39", "kundeLf", Some("base")),
                ("Z40", "kundeLf", Some("informative")),
            ]
        );
    }

    /// IFTSTA 21037 SG15: RFF+Z13 carries the PID as its only code, RFF+ACW and
    /// RFF+ACE a free reference; two CAV variants with different code lists.
    fn status_schema() -> serde_json::Value {
        let rff = |qual: &str, id: serde_json::Value| {
            json!({"id": "RFF", "elements": [{"index": 0, "composite": "C506", "components": [
                {"sub_index": 0, "id": "1153", "type": "code", "codes": [{"value": qual, "name": qual}]},
                id,
            ]}]})
        };
        let data = json!({"sub_index": 1, "id": "1154", "type": "data"});
        let cav = |qual: &str, codes: serde_json::Value| {
            json!({"id": "CAV", "elements": [{"index": 0, "composite": "C889", "components": [
                {"sub_index": 0, "id": "7111", "type": "code", "codes": [{"value": qual, "name": qual}]},
                {"sub_index": 1, "id": "7110", "type": "code", "codes": codes},
            ]}]})
        };
        json!({"fields": {"sg14": {"max_reps": 1, "segments": [], "children": {"sg15": {"max_reps": 1, "segments": [
            rff("Z13", json!({"sub_index": 1, "id": "1154", "type": "code",
                              "codes": [{"value": "21037", "name": "RD / NB-Bewertung"}]})),
            rff("ACW", data.clone()),
            rff("ACE", data),
            cav("Z91", json!([{"value": "A", "name": "Alpha"}, {"value": "B", "name": "Beta"}])),
            cav("ZF0", json!([{"value": "C", "name": "Gamma"}])),
        ]}}}}})
    }

    #[test]
    fn qualified_field_paths_take_codes_of_their_own_segment_variant() {
        let schema = status_schema();
        let lookup = CodeLookup::from_schema_value(&schema);
        let status = def(r#"
[meta]
entity = "Status"
bo4e_type = "Status"
source_group = "SG14.SG15"
source_path = "sg14.sg15"

[fields]
"rff[Z13].0.1" = "pruefidentifikator"
"rff[ACW].0.0" = "acwQualifier"
"rff[ACW].0.1" = "referenz"
"rff[ACE].0.1" = "gegenvorschlagReferenz"
"cav[Z91].0.1" = "z91Wert"
"cav.0.1" = "wert"
"#);
        let obj = object_shape(&status, &[], Some(&lookup), None);
        let leaf = |name: &str| match obj.fields.get(name) {
            Some(FieldShape::Leaf(l)) => l.clone(),
            other => panic!("{name}: {other:?}"),
        };
        let values = |name: &str| {
            leaf(name)
                .values
                .map(|v| v.keys().cloned().collect::<Vec<_>>())
        };
        for free_reference in ["referenz", "gegenvorschlagReferenz"] {
            assert_eq!(
                values(free_reference),
                None,
                "{free_reference} is RFF+ACW/ACE data, not RFF+Z13's PID code"
            );
            assert!(!leaf(free_reference).enriched);
        }
        assert_eq!(
            values("pruefidentifikator"),
            Some(vec!["21037".to_string()])
        );
        assert_eq!(values("acwQualifier"), Some(vec!["ACW".to_string()]));
        assert_eq!(
            values("z91Wert"),
            Some(vec!["A".to_string(), "B".to_string()])
        );
        assert_eq!(
            values("wert"),
            Some(vec!["A".to_string(), "B".to_string(), "C".to_string()]),
            "an unqualified path reads any CAV variant"
        );
    }

    /// A rule naming a shared code list (#165) emits the same names as one
    /// spelling the table inline, so its shape must be the same too. It was
    /// not: only the inline `enum_map` was read, and FV2504 — which names its
    /// lists almost everywhere — got typed variants for raw codes the engine
    /// never writes (#170: `partnerrolle` "kundeDesLf" unknown).
    #[test]
    fn a_named_code_list_shapes_like_the_inline_table() {
        let schema = status_schema();
        let lookup = CodeLookup::from_schema_value(&schema);
        let rule = |mapping: &str| {
            def(&format!(
                r#"
[meta]
entity = "Status"
bo4e_type = "Status"
source_group = "SG14.SG15"
source_path = "sg14.sg15"

[fields]
"cav[Z91].0.1" = {mapping}
"#
            ))
        };
        let inline = rule(r#"{ target = "z91Wert", enum_map = { "A" = "alpha", "B" = "beta" } }"#);
        let named = rule(r#"{ target = "z91Wert", code_list = "z91" }"#);
        let lists = crate::code_lists::CodeLists::from_toml_str(
            "[z91]\n\"A\" = \"alpha\"\n\"B\" = \"beta\"\n",
        )
        .unwrap();

        let values = |obj: &ObjectShape| match obj.fields.get("z91Wert") {
            Some(FieldShape::Leaf(l)) => l
                .values
                .as_ref()
                .map(|v| v.keys().cloned().collect::<Vec<_>>()),
            other => panic!("{other:?}"),
        };
        let expected = values(&object_shape(&inline, &[], Some(&lookup), None));
        assert_eq!(
            expected,
            Some(vec!["alpha".to_string(), "beta".to_string()])
        );
        assert_eq!(
            values(&object_shape(&named, &[], Some(&lookup), Some(&lists))),
            expected
        );
    }

    #[test]
    fn several_variants_of_an_unqualified_group_repeat() {
        assert!(shape(1).transaction.entities["geschaeftspartner"].repeats);
    }

    #[test]
    fn repetition_follows_the_pid_schema() {
        let schema = schema(1);
        let mig = mig();
        let sources = Sources {
            schema: Some(&schema),
            mig: Some(&mig),
            lookup: None,
            code_lists: None,
        };
        let repeats = |toml: &str, tx: Option<&str>| def_repeats(&def(toml), &sources, tx);
        let entity = |sg: &str, sp: &str, extra: &str| {
            format!(
                "[meta]\nentity = \"E\"\nbo4e_type = \"E\"\nsource_group = \"{sg}\"\n\
                 source_path = \"{sp}\"\n{extra}\n[fields]\n\"x.0\" = \"x\"\n"
            )
        };
        assert!(repeats(&entity("SG4.SG5", "sg4.sg5_z16", ""), Some("SG4")));
        assert!(
            !repeats(
                &entity("SG4.SG6", "sg4.sg6", "discriminator = \"RFF.0.0=Z13\""),
                Some("SG4")
            ),
            "a discriminated variant repeats as the schema says"
        );
        assert!(
            repeats(&entity("SG4.SG6", "sg4.sg6", ""), Some("SG4")),
            "without a discriminator the MIG's repetition counts (unknown here)"
        );
        assert!(!repeats(&entity("SG4", "sg4", ""), Some("SG4")), "tx root");
        assert!(repeats(&entity("SG2", "sg2", ""), None));
        assert!(
            !repeats(
                &entity("SG4.SG12", "sg4.sg12", "discriminator = \"NAD.0.0=Z07\""),
                Some("SG4")
            ),
            "a discriminator selects one variant"
        );
        assert!(
            !repeats(&entity("SG4.SG9", "sg4.sg9", ""), Some("SG4")),
            "absent group"
        );
        assert!(repeats(
            &entity("SG4.SG6", "sg4.sg6", "repeat_on_tag = \"FTX\""),
            Some("SG4")
        ));
    }
}