|
| 1 | +/** |
| 2 | + * Stem-object → XMLBuilder-shape converters for VehicleType and its children. |
| 3 | + * |
| 4 | + * **Generated by** `netex-typescript-model/typescript/scripts/generate-hathor-helpers.ts` |
| 5 | + * from `makeInlinedToXmlShape()` in `to-xml-shape.ts`, driven by the base JSON Schema |
| 6 | + * (`generated-src/base/base.schema.json`). Re-run the generator to pick up schema changes. |
| 7 | + * |
| 8 | + * **Architecture:** `base()` / `baseSimple()` handle the generic transform (attr rename |
| 9 | + * `$`→`@_`, bool stringify, simpleContent `value`→`#text`). Per-entity functions add |
| 10 | + * override lines only for complex children that need recursive `toXmlShape` delegation. |
| 11 | + * `toXmlShapeDispatch` routes child names to the correct function. |
| 12 | + * |
| 13 | + * **Entry point:** `toXmlShape(obj)` — takes a VehicleType stem object, returns the |
| 14 | + * shape expected by fast-xml-parser `XMLBuilder`. |
| 15 | + */ |
| 16 | +// Auto-generated by generate-hathor-helpers.ts — do not edit |
| 17 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 18 | + |
| 19 | +type XmlShape = Record<string, any>; |
| 20 | +type ToXmlShapeFn = (name: string, obj: any) => any; |
| 21 | + |
| 22 | +function base(obj: Record<string, any>): XmlShape { |
| 23 | + const out: XmlShape = {}; |
| 24 | + for (const k of Object.keys(obj)) { |
| 25 | + const v = obj[k]; |
| 26 | + if (v === undefined) continue; |
| 27 | + if (k[0] === '$') out['@_' + k.slice(1)] = typeof v === 'boolean' ? String(v) : v; |
| 28 | + else out[k] = typeof v === 'boolean' ? String(v) : v; |
| 29 | + } |
| 30 | + return out; |
| 31 | +} |
| 32 | + |
| 33 | +function baseSimple(obj: Record<string, any>): XmlShape { |
| 34 | + const out: XmlShape = {}; |
| 35 | + for (const k of Object.keys(obj)) { |
| 36 | + const v = obj[k]; |
| 37 | + if (v === undefined) continue; |
| 38 | + if (k[0] === '$') out['@_' + k.slice(1)] = typeof v === 'boolean' ? String(v) : v; |
| 39 | + else if (k === 'value') out['#text'] = v; |
| 40 | + else out[k] = typeof v === 'boolean' ? String(v) : v; |
| 41 | + } |
| 42 | + return out; |
| 43 | +} |
| 44 | + |
| 45 | +export function textTypeToXmlShape(obj: Record<string, any>): XmlShape { |
| 46 | + const out = baseSimple(obj); |
| 47 | + return out; |
| 48 | +} |
| 49 | + |
| 50 | +export function privateCodeStructureToXmlShape(obj: Record<string, any>): XmlShape { |
| 51 | + const out = baseSimple(obj); |
| 52 | + return out; |
| 53 | +} |
| 54 | + |
| 55 | +export function keyValueStructureToXmlShape(obj: Record<string, any>): XmlShape { |
| 56 | + const out = baseSimple(obj); |
| 57 | + return out; |
| 58 | +} |
| 59 | + |
| 60 | +export function passengerCapacityStructureToXmlShape( |
| 61 | + obj: Record<string, any>, |
| 62 | + toXmlShape: ToXmlShapeFn |
| 63 | +): XmlShape { |
| 64 | + const out = base(obj); |
| 65 | + if (obj['ValidBetween'] !== undefined) |
| 66 | + out['ValidBetween'] = obj['ValidBetween'].map(function (item: any) { |
| 67 | + return toXmlShape('ValidBetween', item); |
| 68 | + }); |
| 69 | + if (obj['keyList'] !== undefined) out['keyList'] = toXmlShape('keyList', obj['keyList']); |
| 70 | + if (obj['privateCodes'] !== undefined) |
| 71 | + out['privateCodes'] = toXmlShape('privateCodes', obj['privateCodes']); |
| 72 | + if (obj['BrandingRef'] !== undefined) |
| 73 | + out['BrandingRef'] = toXmlShape('BrandingRef', obj['BrandingRef']); |
| 74 | + return out; |
| 75 | +} |
| 76 | + |
| 77 | +export function keyListToXmlShape(obj: Record<string, any>, toXmlShape: ToXmlShapeFn): XmlShape { |
| 78 | + const out = base(obj); |
| 79 | + if (obj['KeyValue'] !== undefined) |
| 80 | + out['KeyValue'] = obj['KeyValue'].map(function (item: any) { |
| 81 | + return toXmlShape('KeyValueStructure', item); |
| 82 | + }); |
| 83 | + return out; |
| 84 | +} |
| 85 | + |
| 86 | +export function privateCodesToXmlShape( |
| 87 | + obj: Record<string, any>, |
| 88 | + toXmlShape: ToXmlShapeFn |
| 89 | +): XmlShape { |
| 90 | + const out = base(obj); |
| 91 | + if (obj['PrivateCode'] !== undefined) |
| 92 | + out['PrivateCode'] = obj['PrivateCode'].map(function (item: any) { |
| 93 | + return toXmlShape('PrivateCode', item); |
| 94 | + }); |
| 95 | + return out; |
| 96 | +} |
| 97 | + |
| 98 | +export function vehicleTypeToXmlShape( |
| 99 | + obj: Record<string, any>, |
| 100 | + toXmlShape: ToXmlShapeFn |
| 101 | +): XmlShape { |
| 102 | + const out = base(obj); |
| 103 | + if (obj['ValidBetween'] !== undefined) |
| 104 | + out['ValidBetween'] = obj['ValidBetween'].map(function (item: any) { |
| 105 | + return toXmlShape('ValidBetween', item); |
| 106 | + }); |
| 107 | + if (obj['keyList'] !== undefined) out['keyList'] = toXmlShape('keyList', obj['keyList']); |
| 108 | + if (obj['privateCodes'] !== undefined) |
| 109 | + out['privateCodes'] = toXmlShape('privateCodes', obj['privateCodes']); |
| 110 | + if (obj['BrandingRef'] !== undefined) |
| 111 | + out['BrandingRef'] = toXmlShape('BrandingRef', obj['BrandingRef']); |
| 112 | + if (obj['Name'] !== undefined) |
| 113 | + out['Name'] = obj['Name'].map(function (item: any) { |
| 114 | + return toXmlShape('TextType', item); |
| 115 | + }); |
| 116 | + if (obj['ShortName'] !== undefined) |
| 117 | + out['ShortName'] = obj['ShortName'].map(function (item: any) { |
| 118 | + return toXmlShape('TextType', item); |
| 119 | + }); |
| 120 | + if (obj['Description'] !== undefined) |
| 121 | + out['Description'] = obj['Description'].map(function (item: any) { |
| 122 | + return toXmlShape('TextType', item); |
| 123 | + }); |
| 124 | + if (obj['PrivateCode'] !== undefined) |
| 125 | + out['PrivateCode'] = toXmlShape('PrivateCode', obj['PrivateCode']); |
| 126 | + if (obj['PassengerCapacity'] !== undefined) |
| 127 | + out['PassengerCapacity'] = toXmlShape('PassengerCapacityStructure', obj['PassengerCapacity']); |
| 128 | + if (obj['DeckPlanRef'] !== undefined) |
| 129 | + out['DeckPlanRef'] = toXmlShape('DeckPlanRef', obj['DeckPlanRef']); |
| 130 | + if (obj['IncludedIn'] !== undefined) |
| 131 | + out['IncludedIn'] = toXmlShape('VehicleTypeRefStructure', obj['IncludedIn']); |
| 132 | + if (obj['ClassifiedAsRef'] !== undefined) |
| 133 | + out['ClassifiedAsRef'] = toXmlShape('VehicleModelRefStructure', obj['ClassifiedAsRef']); |
| 134 | + return out; |
| 135 | +} |
| 136 | + |
| 137 | +// --- hand-written (not in schema) --- |
| 138 | +// TODO: VehicleManoeuvringRequirements is not in the NeTEx XSD. If it gets added |
| 139 | +// upstream, replace this stub with a generated function in TYPES[]. |
| 140 | +export function vehicleManoeuvringRequirementsToXmlShape(obj: Record<string, any>): XmlShape { |
| 141 | + return base(obj); |
| 142 | +} |
| 143 | + |
| 144 | +// TODO: Dispatch only covers types reachable from VehicleType. To support |
| 145 | +// Vehicle / DeckPlan XML export, add their leaf types to TYPES[] in the |
| 146 | +// generator and add cases here. |
| 147 | +// TODO: BrandingRef, DeckPlanRef, ValidBetween, VehicleTypeRefStructure, and |
| 148 | +// VehicleModelRefStructure hit the `default` pass-through — their nested |
| 149 | +// attrs ($ref, $version) won't get the @_ rename. Generate functions for |
| 150 | +// these ref types once Hathor needs valid XML for them. |
| 151 | +function toXmlShapeDispatch(name: string, obj: any): any { |
| 152 | + if (obj === undefined || obj === null) return obj; |
| 153 | + if (typeof obj !== 'object') return obj; |
| 154 | + const rec = obj as Record<string, any>; |
| 155 | + switch (name) { |
| 156 | + case 'TextType': |
| 157 | + return textTypeToXmlShape(rec); |
| 158 | + case 'PrivateCode': |
| 159 | + return privateCodeStructureToXmlShape(rec); |
| 160 | + case 'KeyValueStructure': |
| 161 | + return keyValueStructureToXmlShape(rec); |
| 162 | + case 'PassengerCapacityStructure': |
| 163 | + return passengerCapacityStructureToXmlShape(rec, toXmlShapeDispatch); |
| 164 | + case 'keyList': |
| 165 | + return keyListToXmlShape(rec, toXmlShapeDispatch); |
| 166 | + case 'privateCodes': |
| 167 | + return privateCodesToXmlShape(rec, toXmlShapeDispatch); |
| 168 | + default: |
| 169 | + return rec; |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +export function toXmlShape(obj: Record<string, any>): XmlShape { |
| 174 | + return vehicleTypeToXmlShape(obj, toXmlShapeDispatch); |
| 175 | +} |
0 commit comments