All files / src/utils mapping-serializer.ts

87.88% Statements 515/586
73.44% Branches 271/369
96.97% Functions 32/33
87.37% Lines 436/499

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312                                7x                                     7x                   7x       7x           7x         7x               7x       7x 7x 7x     7x 7x       7x 7x 7x 7x   7x 7x   165x     83x 83x 105x 127x   105x 105x           90x 90x     15x       15x                         83x       83x   83x   83x   83x         83x                               83x     7x       111x   111x           110x             110x         110x         48x 48x 25x         40x 40x         40x 22x               18x               62x       18x       12x 11x   12x 12x     56x 1x                                                                 1x                 55x                 96x 1x 1x 1x     96x     7x 15x 15x 15x 15x 15x           4x                 15x     7x         15x 12x   15x         7x   15x             15x 1x   15x 15x 15x     15x     15x 2x                 7x 15x 15x                         7x 15x 4x   11x       7x         66x 66x 5x           61x         7x       43x 43x 43x 43x 43x 43x   43x     43x       4x 4x     39x 26x   13x   13x 26x 13x     13x       5x   13x 13x       39x 15x 15x 15x 15x     49x 49x   39x   39x         39x     7x         26x       26x           26x   26x         26x   26x       10x       10x 10x   10x                 10x 10x   10x         10x           7x                                       7x           11x             11x 11x   11x     7x       83x 209x 209x                   209x     209x     209x 16x 16x 16x 16x 8x             16x     16x 193x 47x 47x     47x     209x   83x     7x 83x 83x   17x           83x     7x 166x 166x 10x                 166x     7x     83x 83x 83x 81x     4x 2x 4x 4x           4x     2x       2x   2x                       7x         3x   3x           3x                 7x       87x 87x     87x 77x           77x   77x 63x                       63x       73x     7x       221x   221x 221x 221x 221x   442x 350x 43x     307x 307x                 307x         307x 36x 17x   19x       307x       25x 282x       16x     307x 8x 8x 8x 8x       299x 14x 14x 285x                           307x 307x   307x   162x     307x 2x     307x     307x 41x   41x           27x 3x           24x 24x       13x 7x               6x     6x   7x       7x 1x       1x 1x             1x   1x     6x           266x 266x     220x                   7x         307x 37x   44x 44x 44x 44x     37x 37x         7x       87x 73x 73x     7x       15x 15x     64x 64x 64x 64x 64x 64x 64x 64x 64x 15x 15x 15x 15x 15x 15x 15x 15x     64x     64x   15x     7x       15x   15x     43x     43x 43x                     43x               15x                         7x         4x 2x 2x           2x       2x 1x 1x 1x 1x 1x           1x               5x 5x   6x 6x   4x     7x     15x 15x     15x 12x 12x 12x 12x 12x 12x 12x 12x   15x     7x         30x 30x 4x       26x 14x       7x   7x 7x 7x 7x 7x 7x 7x 7x   26x     7x     15x 15x           1x   14x 2x 2x 4x 4x 4x 4x 4x 4x 4x   2x   14x                       7x             34x       39x 39x 39x             39x     39x                     39x     39x             39x             7x         7x   32x         7x           134x 11x 10x   1x   123x 6x 6x 5x   1x   6x       117x                       134x 134x 134x 134x 34x   134x     7x 49x 47x     2x             7x 2x 2x     2x 2x 2x 2x 2x 1x       1x 1x 1x 1x 1x 1x     2x         7x 47x 47x 90x 90x 47x     43x 43x 43x 43x 43x   47x   7x  
/*
    Copyright (C) 2017 Red Hat, Inc.
 
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
 
            http://www.apache.org/licenses/LICENSE-2.0
 
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/
 
import {
  ATLAS_MAPPING_JSON_TYPE,
  COLLECTION_JSON_TYPE,
  CONSTANT_FIELD_JSON_TYPE,
  FIELD_GROUP_JSON_TYPE,
  IAtlasMappingContainer,
  ICollection,
  IConstant,
  IFieldGroup,
  IJsonDataSource,
  ILookupEntry,
  ILookupTable,
  IMapping,
  IProperty,
  IPropertyField,
  MAPPING_JSON_TYPE,
  MappingType,
  PROPERTY_FIELD_JSON_TYPE,
} from '../contracts/mapping';
import {
  DATA_SOURCE_JSON_TYPE,
  DataSourceType,
  DocumentType,
  FIELD_PATH_SEPARATOR,
  FieldType,
  IDataSource,
  IField,
  IFieldAction,
} from '../contracts/common';
import {
  DocumentDefinition,
  NamespaceModel,
} from '../models/document-definition.model';
import {
  ErrorInfo,
  ErrorLevel,
  ErrorScope,
  ErrorType,
} from '../models/error.model';
import {
  FieldAction,
  FieldActionArgument,
  FieldActionArgumentValue,
} from '../models/field-action.model';
import {
  IXmlDataSource,
  IXmlField,
  IXmlNamespace,
  XML_DATA_SOURCE_JSON_TYPE,
  XML_ENUM_FIELD_JSON_TYPE,
  XML_MODEL_PACKAGE_PREFIX,
} from '../contracts/documents/xml';
import {
  JSON_DATA_SOURCE_JSON_TYPE,
  JSON_ENUM_FIELD_JSON_TYPE,
} from '../contracts/documents/json';
import { LookupTable, LookupTableEntry } from '../models/lookup-table.model';
import { MappedField, MappingModel } from '../models/mapping.model';
import { TransitionMode, TransitionModel } from '../models/transition.model';
 
import { ConfigModel } from '../models/config.model';
import { ExpressionModel } from '../models/expression.model';
import { Field } from '../models/field.model';
import { IAudits } from '../contracts/mapping-preview';
import { ICsvField } from '../contracts/documents/csv';
import { IJavaField } from '../contracts/documents/java';
import { JAVA_ENUM_FIELD_JSON_TYPE } from '../contracts/documents/java';
import { MappingDefinition } from '../models/mapping-definition.model';
import { MappingUtil } from './mapping-util';
import { Multiplicity } from '../contracts/field-action';
 
export class MappingSerializer {
  static serializeMappings(
    cfg: ConfigModel,
    ignoreValue: boolean = true
  ): IAtlasMappingContainer {
    // TODO: check this non null operator
    const mappingDefinition: MappingDefinition = cfg.mappings!;
    let jsonMappings: IMapping[] = [];
    for (const mapping of mappingDefinition.mappings.filter((m) =>
      m.isFullyMapped()
    )) {
      try {
        const serializedFieldMapping = MappingSerializer.serializeFieldMapping(
          cfg,
          mapping,
          mapping.uuid,
          ignoreValue
        );
        Eif (serializedFieldMapping) {
          jsonMappings = jsonMappings.concat(serializedFieldMapping);
        }
      } catch (e) {
        const input: any = {
          mapping: mapping,
          'mapping def': mappingDefinition,
        };
        cfg.errorService.addError(
          new ErrorInfo({
            message:
              'Caught exception while attempting to serialize mapping, skipping. ',
            level: ErrorLevel.ERROR,
            scope: ErrorScope.APPLICATION,
            type: ErrorType.INTERNAL,
            object: { input: input, error: e },
          })
        );
      }
    }
 
    const serializedLookupTables = MappingSerializer.serializeLookupTables(
      cfg.mappings!
    );
    const constantDescriptions: IConstant[] =
      MappingSerializer.serializeConstants(cfg.constantDoc);
    const sourcePropertyDescriptions: IProperty[] =
      MappingSerializer.serializeProperties(cfg.sourcePropertyDoc);
    const targetPropertyDescriptions: IProperty[] =
      MappingSerializer.serializeProperties(cfg.targetPropertyDoc);
    const serializedDataSources: IDataSource[] =
      MappingSerializer.serializeDocuments(
        cfg.sourceDocs.concat(cfg.targetDocs),
        mappingDefinition
      );
 
    const payload: IAtlasMappingContainer = {
      AtlasMapping: {
        jsonType: ATLAS_MAPPING_JSON_TYPE,
        dataSource: serializedDataSources,
        mappings: { mapping: jsonMappings },
        name: cfg.mappings!.name ? cfg.mappings!.name : undefined,
        version: cfg.mappings!.version!,
        lookupTables: { lookupTable: serializedLookupTables },
        constants: { constant: constantDescriptions },
        properties: {
          property: sourcePropertyDescriptions.concat(
            targetPropertyDescriptions
          ),
        },
      },
    };
    return payload;
  }
 
  static serializeFieldMapping(
    cfg: ConfigModel,
    mapping: MappingModel,
    id: string,
    IignoreValue: boolean = true
  ): IMapping {
    const serializedInputFields = MappingSerializer.serializeFields(
      mapping,
      true,
      cfg,
      ignoreValue
    );
    const serializedOutputFields = MappingSerializer.serializeFields(
      mapping,
      false,
      cfg,
      ignoreValue
    );
    let jsonMapping: IMapping;
    const mappingExpression = cfg.expressionService.getMappingExpressionStr(
      false,
      mapping
    );
 
    if (
      mapping.transition.isManyToOneMode() ||
      mapping.transition.isForEachMode() ||
      mapping.transition.isExpressionMode()
    ) {
      const actions = [];
      if (mapping.transition.transitionFieldAction) {
        actions[0] = this.serializeAction(
          mapping.transition.transitionFieldAction,
          cfg
        );
      }
      const field = serializedInputFields;
      const inputFieldGroup: IFieldGroup = {
        jsonType: FIELD_GROUP_JSON_TYPE,
        actions,
        field,
      };
      if (mappingExpression.length > 0) {
        jsonMapping = {
          jsonType: MAPPING_JSON_TYPE,
          id: id,
          expression: mappingExpression,
          inputFieldGroup,
          outputField: serializedOutputFields,
        };
      } else {
        jsonMapping = {
          jsonType: MAPPING_JSON_TYPE,
          id: id,
          inputFieldGroup,
          outputField: serializedOutputFields,
        };
      }
    } else {
      if (
        mapping.transition.isOneToManyMode() &&
        mapping.transition.transitionFieldAction
      ) {
        const mappingAction = this.serializeAction(
          mapping.transition.transitionFieldAction,
          cfg
        );
        if (!serializedInputFields[0].actions) {
          serializedInputFields[0].actions = [];
        }
        Eif (mappingAction) {
          serializedInputFields[0].actions.unshift(mappingAction);
        }
      }
      if (mappingExpression.length > 0) {
        Iif (serializedInputFields[0].jsonType?.includes('FieldGroup')) {
          let serializedInputFieldGroup =
            serializedInputFields[0] as IFieldGroup;
          for (let i = 1; i < serializedInputFields.length; i++) {
            if (serializedInputFields[i].jsonType?.includes('FieldGroup')) {
              const serializedFieldGroup0 =
                serializedInputFields[0] as IFieldGroup;
              const serializedFieldGroupi = serializedInputFields[
                i
              ] as IFieldGroup;
              if (
                serializedFieldGroupi.field &&
                serializedFieldGroup0.field &&
                serializedFieldGroupi.field[0].path ===
                  serializedFieldGroup0.field[0].path
              ) {
                serializedInputFieldGroup.field?.push(
                  serializedFieldGroupi.field[0]
                );
              }
            }
            // TODO - Support input fields from different complex parent fields
            // in the same complex conditional expression.
          }
 
          jsonMapping = {
            jsonType: MAPPING_JSON_TYPE,
            id: id,
            expression: mappingExpression,
            inputFieldGroup: serializedInputFieldGroup,
            outputField: serializedOutputFields,
          };
        } else {
          jsonMapping = {
            jsonType: MAPPING_JSON_TYPE,
            id: id,
            expression: mappingExpression,
            inputField: serializedInputFields,
            outputField: serializedOutputFields,
          };
        }
      } else {
        jsonMapping = {
          jsonType: MAPPING_JSON_TYPE,
          id: id,
          inputField: serializedInputFields,
          outputField: serializedOutputFields,
        };
      }
    }
 
    if (mapping.transition.isEnumerationMode()) {
      jsonMapping.mappingType = MappingType.LOOKUP; /* @deprecated */
      Eif (mapping.transition.lookupTableName) {
        jsonMapping.lookupTableName = mapping.transition.lookupTableName;
      }
    }
    return jsonMapping;
  }
 
  static extractCheckVersion(json: IAtlasMappingContainer, cfg: ConfigModel) {
    const currentVersion = cfg.initializationService.getUIVersion();
    const mappingVersion = this.deserializeAtlasMappingVersion(json);
    const mappingVersionComps = mappingVersion.split('.');
    const currentVersionComps = currentVersion.split('.');
    if (
      (mappingVersion.length > 0 &&
        (currentVersionComps.length < 2 || mappingVersionComps.length < 2)) ||
      +currentVersionComps[0] < +mappingVersionComps[0] ||
      +currentVersionComps[1] < +mappingVersionComps[1]
    ) {
      cfg.errorService.addError(
        new ErrorInfo({
          message: `Mappings file version mismatch.  Expected ${currentVersion}, detected ${mappingVersion}`,
          level: ErrorLevel.ERROR,
          scope: ErrorScope.APPLICATION,
          type: ErrorType.USER,
        })
      );
    }
    cfg.mappings!.version = currentVersion;
  }
 
  static async deserializeMappingServiceJSON(
    json: IAtlasMappingContainer,
    cfg: ConfigModel
  ) {
    // Process constants and properties before mappings.
    for (const field of MappingSerializer.deserializeConstants(json)) {
      cfg.constantDoc.addField(field);
    }
    for (const field of MappingSerializer.deserializeProperties(
      cfg,
      json,
      true
    )) {
      cfg.sourcePropertyDoc.addField(field);
    }
    for (const field of MappingSerializer.deserializeProperties(
      cfg,
      json,
      false
    )) {
      cfg.targetPropertyDoc.addField(field);
    }
    if (!cfg.mappings) {
      cfg.mappings = new MappingDefinition();
    }
    cfg.mappings.name = this.deserializeAtlasMappingName(json);
    this.extractCheckVersion(json, cfg);
    cfg.mappings.parsedDocs = cfg.mappings.parsedDocs.concat(
      MappingSerializer.deserializeDocs(json, cfg.mappings)!
    ); // TODO: check this non null operator
    cfg.mappings.mappings = cfg.mappings.mappings.concat(
      MappingSerializer.deserializeMappings(json, cfg)
    );
    for (const lookupTable of MappingSerializer.deserializeLookupTables(json)) {
      cfg.mappings.addTable(lookupTable);
    }
  }
 
  /**
   * Return the AtlasMap mappings file name from the specified JSON buffer or an empty string.
   *
   * @param json
   */
  static deserializeAtlasMappingName(json: IAtlasMappingContainer): string {
    Eif (json?.AtlasMapping?.name) {
      return json.AtlasMapping.name;
    } else {
      return '';
    }
  }
 
  /**
   * Return the AtlasMap mappings version from the specified JSON buffer or an empty
   * string.
   *
   * @param json JSON buffer
   * @returns version string or empty string
   */
  static deserializeAtlasMappingVersion(json: IAtlasMappingContainer): string {
    if (json?.AtlasMapping?.version) {
      return json.AtlasMapping.version;
    } else {
      return '';
    }
  }
 
  static addInputFieldGroupFields(
    inputField: IField[],
    mapping: MappingModel,
    cfg: ConfigModel
  ) {
    for (const field of inputField) {
      if (field.fieldType === 'COMPLEX') {
        MappingSerializer.addInputFieldGroupFields(
          (field as IFieldGroup).field!,
          mapping,
          cfg
        );
      } else {
        MappingSerializer.deserializeMappedField(mapping, field, true, cfg);
      }
    }
  }
 
  static deserializeFieldMapping(
    mappingJson: IMapping,
    cfg: ConfigModel
  ): MappingModel {
    const mapping = new MappingModel();
    mapping.uuid = mappingJson.id;
    mapping.sourceFields = [];
    mapping.targetFields = [];
    mapping.referenceFields = [];
    mapping.transition.mode = TransitionMode.ONE_TO_ONE;
    const isLookupMapping =
      mappingJson.mappingType === 'LOOKUP' ||
      mappingJson.lookupTableName != null;
 
    if (
      mappingJson.mappingType &&
      mappingJson.mappingType !== MappingType.NONE
    ) {
      this.deserializeFieldMappingFromType(mapping, mappingJson, cfg);
      return mapping;
    }
 
    if (mappingJson.inputFieldGroup) {
      MappingSerializer.deserializeInputFieldGroup(mappingJson, mapping, cfg);
    } else {
      const inputField = mappingJson.inputField;
 
      Eif (inputField) {
        for (const field of inputField) {
          MappingSerializer.deserializeMappedField(mapping, field, true, cfg);
        }
      }
      if (
        mappingJson.outputField?.length &&
        mappingJson.outputField.length > 1
      ) {
        mapping.transition.mode = TransitionMode.ONE_TO_MANY;
      }
      Eif (cfg.mappings) {
        MappingUtil.updateMappedFieldsFromDocuments(mapping, cfg, true);
      }
    }
 
    if (mappingJson.expression && mappingJson.expression.length > 0) {
      mapping.transition.enableExpression = true;
      mapping.transition.mode = TransitionMode.EXPRESSION;
      mapping.transition.expression = new ExpressionModel(mapping, cfg);
      mapping.transition.expression.insertText(mappingJson.expression);
    }
 
    for (const field of mappingJson.outputField) {
      MappingSerializer.deserializeMappedField(mapping, field, false, cfg);
    }
    MappingUtil.updateMappedFieldsFromDocuments(mapping, cfg, false);
 
    Iif (isLookupMapping) {
      mapping.transition.lookupTableName = mappingJson.lookupTableName!;
      mapping.transition.mode = TransitionMode.ENUM;
    }
 
    return mapping;
  }
 
  private static deserializeInputFieldGroup(
    mappingJson: IMapping,
    mapping: MappingModel,
    cfg: ConfigModel
  ) {
    Iif (!mappingJson.inputFieldGroup) {
      return;
    }
 
    Iif (
      mappingJson.expression &&
      mappingJson.inputFieldGroup!.fieldType === FieldType.COMPLEX
    ) {
      mapping.transition.expression.hasComplexField = true;
    }
    mapping.transition.mode = TransitionMode.MANY_TO_ONE;
 
    MappingSerializer.addInputFieldGroupFields(
      mappingJson.inputFieldGroup.field!,
      mapping,
      cfg
    );
    MappingUtil.updateMappedFieldsFromDocuments(mapping, cfg, true);
 
    if (
      mappingJson.inputFieldGroup.actions &&
      mappingJson.inputFieldGroup.actions[0]?.delimiter
    ) {
      mapping.transition.delimiter = TransitionModel.delimiterToModel(
        mappingJson.inputFieldGroup.actions[0]?.delimiter
      )?.delimiter;
      // Check for an InputFieldGroup containing a many-to-one action
      const firstAction = mappingJson.inputFieldGroup.actions[0];
      Eif (firstAction) {
        // @deprecated Support legacy ADM files that have transformation-action-based expressions.
        Iif (firstAction.Expression || firstAction['@type'] === 'Expression') {
          mapping.transition.enableExpression = true;
          mapping.transition.mode = TransitionMode.EXPRESSION;
          mapping.transition.expression = new ExpressionModel(mapping, cfg);
          const expr = firstAction.Expression
            ? firstAction.Expression.expression
            : firstAction['expression'];
          mapping.transition.expression.insertText(expr);
        } else {
          mapping.transition.mode = TransitionMode.MANY_TO_ONE;
          const parsedAction = this.parseAction(firstAction);
          // TODO: check this non null operator
          parsedAction.definition =
            cfg.fieldActionService.getActionDefinitionForName(
              parsedAction.name,
              Multiplicity.MANY_TO_ONE
            )!;
          mapping.transition.transitionFieldAction = parsedAction;
        }
      }
    }
  }
 
  static deserializeAudits(audits: IAudits, errorType: ErrorType): ErrorInfo[] {
    const errors: ErrorInfo[] = [];
    if (!audits?.audit) {
      return errors;
    }
    for (const audit of audits.audit) {
      const msg = audit.status + '[' + audit.path + ']: ' + audit.message;
      errors.push(
        new ErrorInfo({
          message: msg,
          level: ErrorLevel[audit.status],
          scope: ErrorScope.MAPPING,
          type: errorType,
          object: audit.value,
        })
      );
    }
    return errors;
  }
 
  private static createInputFieldGroup(
    field: IField[],
    isComplex: boolean,
    docId?: string,
    path?: string
  ): IFieldGroup {
    const inputFieldGroup: IFieldGroup = {
      jsonType: FIELD_GROUP_JSON_TYPE,
      actions: [],
      docId: docId,
      path: path,
      field,
    };
    Eif (isComplex) {
      inputFieldGroup.fieldType = FieldType.COMPLEX;
    }
    return inputFieldGroup;
  }
 
  private static serializeDocuments(
    docs: DocumentDefinition[],
    mappingDefinition: MappingDefinition
  ): IDataSource[] {
    const serializedDocs: IDataSource[] = [];
    for (const doc of docs) {
      let serializedDoc: IDataSource = {
        jsonType: DATA_SOURCE_JSON_TYPE,
        id: doc.id,
        name: doc.name,
        description: doc.description,
        uri: doc.uri,
        dataSourceType: doc.isSource
          ? DataSourceType.SOURCE
          : DataSourceType.TARGET,
      };
      Iif (doc.characterEncoding != null) {
        serializedDoc.characterEncoding = doc.characterEncoding;
      }
      Iif (doc.locale != null) {
        serializedDoc.locale = doc.locale;
      }
      if (doc.type === DocumentType.XML || doc.type === DocumentType.XSD) {
        const xmlDoc = serializedDoc as IXmlDataSource;
        xmlDoc.jsonType = XML_DATA_SOURCE_JSON_TYPE;
        const namespaces: IXmlNamespace[] = [];
        for (const ns of doc.namespaces) {
          namespaces.push({
            alias: ns.alias,
            uri: ns.uri,
            locationUri: ns.locationUri,
            targetNamespace: ns.isTarget,
          });
        }
        Iif (!doc.isSource && mappingDefinition.templateText) {
          xmlDoc.template = mappingDefinition.templateText;
        }
        xmlDoc.xmlNamespaces = { xmlNamespace: namespaces };
      } else if (doc.type === DocumentType.JSON) {
        const jsonDoc = serializedDoc as IJsonDataSource;
        Iif (!doc.isSource && mappingDefinition.templateText) {
          jsonDoc.template = mappingDefinition.templateText;
        }
        jsonDoc.jsonType = JSON_DATA_SOURCE_JSON_TYPE;
      }
 
      serializedDocs.push(serializedDoc);
    }
    return serializedDocs;
  }
 
  private static serializeConstants(docDef: DocumentDefinition): IConstant[] {
    const constantDescriptions: IConstant[] = [];
    for (const field of docDef.fields) {
      // Use the constant value for the name.
      constantDescriptions.push({
        name: field.name,
        value: field.value,
        fieldType: field.type,
      });
    }
    return constantDescriptions;
  }
 
  private static serializeProperties(docDef: DocumentDefinition): IProperty[] {
    const propertyDescriptions: IProperty[] = [];
    for (const field of docDef.fields) {
      propertyDescriptions.push({
        name: field.name,
        fieldType: field.type,
        scope: field.scope,
        dataSourceType: docDef.isSource
          ? DataSourceType.SOURCE
          : DataSourceType.TARGET,
      });
    }
    return propertyDescriptions;
  }
 
  private static serializeLookupTables(
    mappingDefinition: MappingDefinition
  ): ILookupTable[] {
    const serializedTables: ILookupTable[] = [];
    const tables = mappingDefinition.getTables();
    if (!tables || !tables.length) {
      return serializedTables;
    }
 
    for (const table of tables) {
      const lookupEntries: ILookupEntry[] = [];
      for (const entry of table.lookupEntry) {
        const serializedEntry: ILookupEntry = {
          sourceValue: entry.sourceValue,
          sourceType: entry.sourceType,
          targetValue: entry.targetValue,
          targetType: entry.targetType,
        };
        lookupEntries.push(serializedEntry);
      }
 
      const serializedTable: ILookupTable = {
        lookupEntry: lookupEntries,
        name: table.name,
      };
      serializedTables.push(serializedTable);
    }
    return serializedTables;
  }
 
  /**
   * Generate serialized meta-data representing a direct-reference instance collection preview.
   *
   * @param cfg
   * @param mapping
   * @param field
   * @param serializedField
   * @param fieldsJson
   */
  private static processCollectionPreview(
    field: Field,
    serializedField: IField,
    fieldsJson: IField[]
  ) {
    serializedField.path = field.path.replace('<>', '<0>');
    const collectionInstanceInputFieldGroup =
      MappingSerializer.createInputFieldGroup(
        [serializedField],
        true,
        field.docDef.id,
        field.path
      );
    fieldsJson.push(collectionInstanceInputFieldGroup);
  }
 
  /**
   * Serialize field action arguments.
   *
   * @param action
   * @param cfg
   */
  private static processActionArguments(
    action: FieldAction,
    cfg: ConfigModel
  ): { [key: string]: string } {
    const actionArguments: { [key: string]: any } = {};
    Iif (action === null) {
      return actionArguments;
    }
    for (const argValue of action.argumentValues) {
      Iif (
        action.definition?.isCustom &&
        ['methodName', 'className', 'name'].includes(argValue.name)
      ) {
        continue;
      }
      actionArguments[argValue.name] = argValue.value;
      const argumentConfig: FieldActionArgument =
        action.definition!.getArgumentForName(argValue.name);
      Iif (argumentConfig == null) {
        cfg.errorService.addError(
          new ErrorInfo({
            message: `Cannot find action argument ${argValue.name}: ${argValue.value}`,
            level: ErrorLevel.ERROR,
            scope: ErrorScope.APPLICATION,
            type: ErrorType.INTERNAL,
            object: action,
          })
        );
        continue;
      }
      Iif (argumentConfig.type === 'INTEGER') {
        actionArguments[argValue.name] = parseInt(argValue.value, 10);
      }
    }
    return actionArguments;
  }
 
  private static serializeFields(
    mapping: MappingModel,
    isSource: boolean,
    cfg: ConfigModel,
    IignoreValue: boolean = false
  ): IField[] {
    let collectionInputFieldGroup = null;
    let collectionInstanceInputFieldGroup = null;
    const fields: MappedField[] = mapping.getMappedFields(isSource);
    const fieldsJson: IField[] = [];
 
    for (const mappedField of fields) {
      if (!mappedField.field || mappedField.isPadField()) {
        continue;
      }
 
      const field: Field = mappedField.field;
      const serializedField: IField = {
        jsonType: field.documentField.jsonType,
        name: field.name,
        path: field.path,
        fieldType: field.type,
        docId: field.docDef.id,
      };
 
      // The 'attribute' field only applies to XML.
      Iif (field.documentField.jsonType?.includes(XML_MODEL_PACKAGE_PREFIX)) {
        (serializedField as IXmlField).attribute = field.isAttribute;
      }
 
      // Only capture a value for preview mode and constants.
      if (!ignoreValue || field.isConstant()) {
        if (field.value) {
          serializedField.value = field.value;
        } else {
          serializedField.value = '';
        }
      }
 
      if (
        field.docDef.type === DocumentType.XML ||
        field.docDef.type === DocumentType.XSD
      ) {
        (serializedField as IXmlField).userCreated = field.userCreated;
      } else if (
        field.docDef.type === DocumentType.JAVA &&
        !field.isPrimitive
      ) {
        (serializedField as IJavaField).className = field.classIdentifier;
      }
 
      if (field.isProperty()) {
        serializedField.jsonType = PROPERTY_FIELD_JSON_TYPE;
        serializedField.name = field.name;
        (serializedField as IPropertyField).scope = field.scope;
        serializedField.path = cfg.documentService.getPropertyPath(
          field.scope,
          field.name
        );
      } else if (field.isConstant()) {
        serializedField.jsonType = CONSTANT_FIELD_JSON_TYPE;
        serializedField.name = field.name;
      } else Iif (field.enumeration) {
        if (field.docDef.type === DocumentType.JSON) {
          serializedField.jsonType = JSON_ENUM_FIELD_JSON_TYPE;
        } else if (
          field.docDef.type === DocumentType.XML ||
          field.docDef.type === DocumentType.XSD
        ) {
          serializedField.jsonType = XML_ENUM_FIELD_JSON_TYPE;
        } else {
          serializedField.jsonType = JAVA_ENUM_FIELD_JSON_TYPE;
        }
      }
 
      let includeIndexes: boolean =
        mapping.getMappedFields(isSource).length > 1;
      includeIndexes =
        includeIndexes || (mapping.transition.isExpressionMode() && isSource);
      if (includeIndexes) {
        // TODO: check this non null operator
        serializedField.index =
          mapping.getIndexForMappedField(mappedField)! - 1;
      }
      if (field.docDef.type === DocumentType.CSV) {
        (serializedField as ICsvField).column = field.column;
      }
 
      this.serializeActions(cfg, mappedField, serializedField);
 
      // Check for collection field references.
      if (isSource && field.isInCollection()) {
        const collectionParentField = field.getCollectionParentField();
 
        if (
          !mapping.referenceFieldExists(
            collectionParentField.docDef.id,
            collectionParentField.path
          )
        ) {
          if (!ignoreValue) {
            MappingSerializer.processCollectionPreview(
              field,
              serializedField,
              fieldsJson
            );
          } else {
            fieldsJson.push(serializedField);
            collectionInputFieldGroup = null;
          }
        } else {
          // Establish/add to the inner reference field group.
          if (collectionInstanceInputFieldGroup === null) {
            collectionInstanceInputFieldGroup =
              MappingSerializer.createInputFieldGroup(
                [serializedField],
                true,
                collectionParentField.docDef.id,
                collectionParentField.path
              );
          } else {
            (collectionInstanceInputFieldGroup as IFieldGroup)!.field!.push(
              serializedField
            );
            continue;
          }
          collectionInstanceInputFieldGroup.fieldType =
            collectionParentField.documentField.fieldType;
 
          // Preview-mode uses element/ item instance <0>.
          if (!ignoreValue) {
            collectionInstanceInputFieldGroup.path =
              collectionParentField.path.replace('<>', '<0>');
 
            // Establish one outer input field group for the preview collection.
            Eif (collectionInputFieldGroup === null) {
              collectionInputFieldGroup =
                MappingSerializer.createInputFieldGroup(
                  [collectionInstanceInputFieldGroup],
                  true,
                  collectionParentField.docDef.id,
                  collectionParentField.path
                );
              collectionInstanceInputFieldGroup.fieldType =
                collectionParentField.documentField.fieldType;
              fieldsJson.push(collectionInputFieldGroup);
            }
          } else {
            fieldsJson.push(collectionInstanceInputFieldGroup);
          }
        }
 
        // Non-aggregate field reference.
      } else {
        fieldsJson.push(serializedField);
        collectionInputFieldGroup = null;
      }
    }
    return fieldsJson;
  }
 
  /**
   * Walk the list of actions associated with the specified mapped field and serialize them into JSON.
   *
   * @param cfg
   * @param mappedField
   * @param serializedField
   */
  private static serializeActions(
    cfg: ConfigModel,
    mappedField: MappedField,
    serializedField: IField
  ): void {
    if (mappedField.actions.length) {
      const actions: IFieldAction[] = [];
 
      for (const action of mappedField.actions) {
        const actionJson = this.serializeAction(action, cfg);
        Eif (actionJson) {
          actions.push(actionJson);
        }
      }
      Eif (actions.length > 0) {
        serializedField.actions = actions;
      }
    }
  }
 
  private static serializeAction(
    action: FieldAction,
    cfg: ConfigModel
  ): { [key: string]: any } {
    let actionJson = MappingSerializer.processActionArguments(action, cfg);
    actionJson['@type'] = action.definition!.name;
    return actionJson;
  }
 
  private static deserializeDocs(
    json: IAtlasMappingContainer,
    mappingDefinition: MappingDefinition
  ): DocumentDefinition[] | null {
    const docs: DocumentDefinition[] = [];
    Iif (!json || !json.AtlasMapping || !json.AtlasMapping.dataSource) {
      return null;
    }
    for (const docRef of json.AtlasMapping.dataSource) {
      const doc: DocumentDefinition = new DocumentDefinition();
      doc.isSource = docRef.dataSourceType === 'SOURCE';
      doc.uri = docRef.uri;
      doc.id = docRef.id;
      doc.name = docRef.name ? docRef.name : '';
      doc.description = docRef.description;
      const xmlDocRef = docRef as IXmlDataSource;
      if (xmlDocRef.xmlNamespaces && xmlDocRef.xmlNamespaces.xmlNamespace) {
        for (const svcNS of xmlDocRef.xmlNamespaces.xmlNamespace) {
          const ns: NamespaceModel = new NamespaceModel();
          ns.alias = svcNS.alias;
          ns.uri = svcNS.uri;
          ns.locationUri = svcNS.locationUri;
          ns.isTarget = svcNS.targetNamespace ? svcNS.targetNamespace : false;
          ns.createdByUser = true;
          doc.namespaces.push(ns);
        }
      }
      Iif (xmlDocRef.template) {
        mappingDefinition.templateText = xmlDocRef.template;
      }
      docs.push(doc);
    }
    return docs;
  }
 
  private static deserializeMappings(
    json: IAtlasMappingContainer,
    cfg: ConfigModel
  ): MappingModel[] {
    const mappings: MappingModel[] = [];
 
    Iif (!json.AtlasMapping.mappings?.mapping) {
      return mappings;
    }
    for (const fieldMapping of json.AtlasMapping.mappings.mapping) {
      // for backward compatibility
      const isCollectionMapping =
        fieldMapping.jsonType === COLLECTION_JSON_TYPE;
      Iif (isCollectionMapping) {
        const collection = fieldMapping as ICollection;
        for (const innerFieldMapping of collection.mappings.mapping) {
          mappings.push(
            MappingSerializer.deserializeFieldMapping(
              innerFieldMapping as IMapping,
              cfg
            )
          );
        }
      } else {
        mappings.push(
          MappingSerializer.deserializeFieldMapping(
            fieldMapping as IMapping,
            cfg
          )
        );
      }
    }
    return mappings;
  }
 
  /**
   * @deprecated Deserialize a field mapping based on its mapping type.  This is deprecated in favor of
   * concatenate/ split actions.
   *
   * @param mapping
   * @param fieldMapping
   * @param docRefs
   * @param cfg
   * @param ignoreValue
   */
  private static deserializeFieldMappingFromType(
    mapping: MappingModel,
    fieldMapping: IMapping,
    cfg: ConfigModel
  ): void {
    if (fieldMapping.mappingType === 'SEPARATE') {
      mapping.transition.mode = TransitionMode.ONE_TO_MANY;
      mapping.transition.transitionFieldAction = FieldAction.create(
        cfg.fieldActionService.getActionDefinitionForName(
          'Split',
          Multiplicity.ONE_TO_MANY
        )!
      ); // TODO: check this non null operator
      mapping.transition.transitionFieldAction.setArgumentValue(
        'delimiter',
        fieldMapping.delimiter!
      );
    } else if (fieldMapping.mappingType === 'LOOKUP') {
      mapping.transition.mode = TransitionMode.ENUM;
      mapping.transition.lookupTableName = fieldMapping.lookupTableName!;
    } else Eif (fieldMapping.mappingType === 'COMBINE') {
      mapping.transition.mode = TransitionMode.MANY_TO_ONE;
      mapping.transition.transitionFieldAction = FieldAction.create(
        cfg.fieldActionService.getActionDefinitionForName(
          'Concatenate',
          Multiplicity.MANY_TO_ONE
        )!
      ); // TODO: check this non null operator
      mapping.transition.transitionFieldAction.setArgumentValue(
        'delimiter',
        fieldMapping.delimiter!
      );
    } else {
      mapping.transition.mode = TransitionMode.ONE_TO_ONE;
    }
 
    for (const field of fieldMapping.inputField!) {
      MappingSerializer.deserializeMappedField(mapping, field, true, cfg);
    }
    for (const field of fieldMapping.outputField) {
      MappingSerializer.deserializeMappedField(mapping, field, false, cfg);
    }
    MappingUtil.updateMappedFieldsFromDocuments(mapping, cfg, true);
  }
 
  private static deserializeConstants(
    jsonMapping: IAtlasMappingContainer
  ): Field[] {
    const fields: Field[] = [];
    Iif (!jsonMapping?.AtlasMapping?.constants?.constant) {
      return fields;
    }
    for (const constant of jsonMapping.AtlasMapping.constants.constant) {
      const field: Field = new Field();
      field.name = constant.name;
      field.path = FIELD_PATH_SEPARATOR + field.name;
      field.value = constant.value;
      field.type = constant.fieldType;
      field.userCreated = true;
      field.isAttribute = false;
      fields.push(field);
    }
    return fields;
  }
 
  private static deserializeProperties(
    cfg: ConfigModel,
    jsonMapping: IAtlasMappingContainer,
    isSource: boolean
  ): Field[] {
    const fields: Field[] = [];
    if (!jsonMapping?.AtlasMapping?.properties?.property) {
      return fields;
    }
 
    // Source and target properties are mixed in the 'property' JSON array.
    for (const property of jsonMapping.AtlasMapping.properties.property) {
      if (
        (isSource && property.dataSourceType === DataSourceType.TARGET) ||
        (!isSource && property.dataSourceType !== DataSourceType.TARGET)
      ) {
        continue;
      }
      const field: Field = new Field();
      field.name = property.name;
      field.type = property.fieldType;
      field.scope = property.scope;
      field.path = cfg.documentService.getPropertyPath(field.scope, field.name);
      field.userCreated = true;
      field.isAttribute = false;
      fields.push(field);
    }
    return fields;
  }
 
  private static deserializeLookupTables(
    jsonMapping: IAtlasMappingContainer
  ): LookupTable[] {
    const tables: LookupTable[] = [];
    if (
      !jsonMapping ||
      !jsonMapping.AtlasMapping ||
      !jsonMapping.AtlasMapping.lookupTables ||
      !jsonMapping.AtlasMapping.lookupTables.lookupTable
    ) {
      return tables;
    }
    for (const table of jsonMapping.AtlasMapping.lookupTables.lookupTable) {
      const parsedTable: LookupTable = new LookupTable();
      parsedTable.name = table.name;
      for (const entry of table.lookupEntry) {
        const parsedEntry: LookupTableEntry = new LookupTableEntry();
        parsedEntry.sourceValue = entry.sourceValue;
        parsedEntry.sourceType = entry.sourceType;
        parsedEntry.targetValue = entry.targetValue;
        parsedEntry.targetType = entry.targetType;
        parsedTable.lookupEntry.push(parsedEntry);
      }
      tables.push(parsedTable);
    }
    return tables;
  }
 
  /**
   * Walk the list of field actions found in the parsed data and restore them to the live mapping.
   *
   * @param field
   * @param mappedField
   * @param mapping
   * @param cfg
   * @param isSource
   */
  private static deserializeFieldActions(
    field: IField,
    mappedField: MappedField,
    mapping: MappingModel,
    cfg: ConfigModel,
    isSource: boolean
  ): void {
    Iif (!field.actions) {
      return;
    }
 
    for (const action of field.actions) {
      const parsedAction = this.parseAction(action);
      Iif (action.name === 'CustomAction') {
        parsedAction.definition =
          cfg.fieldActionService.getActionDefinitionForName(
            action.argumentValues[0].value,
            Multiplicity.ONE_TO_ONE
          );
      } else {
        parsedAction.definition =
          cfg.fieldActionService.getActionDefinitionForName(parsedAction.name)!;
      }
      Iif (parsedAction.definition == null) {
        cfg.errorService.addError(
          new ErrorInfo({
            message: `Could not find field action definition for action '${action.name}'`,
            level: ErrorLevel.ERROR,
            scope: ErrorScope.APPLICATION,
            type: ErrorType.INTERNAL,
          })
        );
        continue;
      }
      parsedAction.definition.populateFieldAction(parsedAction);
 
      /** @deprecated Support old-style transformation-action-based expressions. */
      Iif (isSource && (action.Expression || action['@type'] === 'Expression')) {
        mapping.transition.enableExpression = true;
        mapping.transition.expression = new ExpressionModel(mapping, cfg);
        const expr = action.Expression
          ? action.Expression.expression
          : action['expression'];
        mapping.transition.expression.insertText(expr);
      } else if (
        isSource &&
        parsedAction.definition &&
        [Multiplicity.ONE_TO_MANY, Multiplicity.MANY_TO_ONE].includes(
          parsedAction.definition.multiplicity
        )
      ) {
        Iif (mapping.transition.transitionFieldAction) {
          cfg.logger!
            .warn(`Duplicated multiplicity transformations were detected: \
              ${mapping.transition.transitionFieldAction.name} is being overwritten by ${parsedAction.name} ...`);
        }
        mapping.transition.transitionFieldAction = parsedAction;
      } else {
        mappedField.actions.push(parsedAction);
      }
    }
  }
 
  private static deserializeMappedField(
    mapping: MappingModel,
    field: IField,
    isSource: boolean,
    cfg: ConfigModel
  ): MappedField | null {
    if (MappingUtil.isConstantField(field)) {
      if (field.docId) {
        cfg.constantDoc.id = field.docId;
      } else {
        field.docId = cfg.constantDoc.id;
      }
    } else if (MappingUtil.isPropertyField(field)) {
      const doc = isSource ? cfg.sourcePropertyDoc : cfg.targetPropertyDoc;
      if (field.docId) {
        doc.id = field.docId;
      } else {
        field.docId = doc.id;
      }
      field.path = cfg.documentService.getPropertyPath(
        (field as IPropertyField).scope,
        field.name!
      );
    } else Iif (!field.docId) {
      cfg.errorService.addError(
        new ErrorInfo({
          message: 'Parsed mapping field does not have document id, dropping.',
          level: ErrorLevel.ERROR,
          scope: ErrorScope.APPLICATION,
          type: ErrorType.INTERNAL,
          object: field,
        })
      );
      return null;
    }
    const mappedField: MappedField = new MappedField();
    mappedField.mappingField = field;
    mapping.addMappedField(mappedField, isSource);
    if (field.actions) {
      this.deserializeFieldActions(field, mappedField, mapping, cfg, isSource);
    }
    return mappedField;
  }
 
  private static parseAction(action: IFieldAction): FieldAction {
    if (action['@type']) {
      return MappingSerializer.parseNewAction(action);
    } else {
      // TODO: check this non null operator
      return MappingSerializer.parseOldAction(action)!;
    }
  }
 
  /**
   * @deprecated actionName: {param:...} style has been deprecated. Use {`@type`: actionName} style action description.
   */
  private static parseOldAction(action: IFieldAction): FieldAction | null {
    for (const actionName of Object.keys(action)) {
      Iif (!action.hasOwnProperty(actionName)) {
        return null;
      }
      const parsedAction: FieldAction = new FieldAction();
      parsedAction.name = actionName;
      const actionParams: any = action[actionName];
      Eif (actionParams) {
        for (const paramName of Object.keys(actionParams)) {
          Iif (!actionParams.hasOwnProperty(paramName)) {
            return null;
          }
          const parsedArgumentValue: FieldActionArgumentValue =
            new FieldActionArgumentValue();
          parsedArgumentValue.name = paramName;
          let value = actionParams[paramName];
          value = value == null ? null : value.toString();
          parsedArgumentValue.value = value;
          parsedAction.argumentValues.push(parsedArgumentValue);
        }
      }
      return parsedAction;
    }
    return null;
  }
 
  private static parseNewAction(action: IFieldAction): FieldAction {
    const parsedAction: FieldAction = new FieldAction();
    parsedAction.name = action['@type']!;
    for (const [key, value] of Object.entries(action)) {
      if ('@type' === key) {
        continue;
      }
      const parsedArgumentValue: FieldActionArgumentValue =
        new FieldActionArgumentValue();
      parsedArgumentValue.name = key;
      const valueString = value == null ? null : (value as any).toString();
      parsedArgumentValue.value = valueString;
      parsedAction.argumentValues.push(parsedArgumentValue);
    }
    return parsedAction;
  }
}