UNPKG

jspurefix

Version:
254 lines 9.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Repository = void 0; const definition_1 = require("../../definition"); const contained_1 = require("../../contained"); const fix_versions_1 = require("../../fix-versions"); const fix_definition_source_1 = require("../../fix-definition-source"); const contained_set_type_1 = require("../../contained-set-type"); class Repository { constructor(version, getLogger) { this.version = version; this.getLogger = getLogger; this.groupLookup = new Map(); this.logger = getLogger('Repository'); this.includesAbbreviations = Repository.doesIncludeAbbreviations(version); this.definitions = new definition_1.FixDefinitions(fix_definition_source_1.FixDefinitionSource.FixRepo, version); } static doesIncludeAbbreviations(version) { let includesAbbreviations; switch (version) { case fix_versions_1.FixVersion.FIX44: case fix_versions_1.FixVersion.FIX50: case fix_versions_1.FixVersion.FIX50SP1: case fix_versions_1.FixVersion.FIX50SP2: includesAbbreviations = true; break; default: { includesAbbreviations = false; break; } } return includesAbbreviations; } assign(name, data) { switch (name) { case 'Fields': { this.Fields = data; break; } case 'Enums': { this.Enums = data; break; } case 'Datatypes': { this.DataTypes = data; break; } case 'Components': { this.Components = data; break; } case 'Messages': { this.Messages = data; break; } case 'MsgContents': { this.MsgContents = data; if (!this.includesAbbreviations) { this.toDefinitions(); } break; } case 'Abbreviations': { this.Abbreviations = data; this.toDefinitions(); break; } } } summarise() { const logger = this.logger; const definitions = this.definitions; logger.info(`definitions: ${definitions.simple.size} fields`); logger.info(`definitions: ${definitions.component.size} components`); logger.info(`definitions: ${definitions.message.size} messages`); } toDefinitions() { this.fields(); this.complex(); this.header(); this.trailer(); this.summarise(); } complex() { this.contentLookup = this.contents(); this.componentLookup = this.components(); this.Components.forEach((c) => this.resolve(c)); this.Messages.forEach((m) => { const msg = this.message(m); this.logger.debug(`${msg.toString()}`); this.definitions.addMessage(msg); }); } header() { const h = this.definitions.component.get('StandardHeader'); if (h) { this.definitions.component.set('header', h); } } trailer() { const t = this.definitions.component.get('StandardTrailer'); if (t) { this.definitions.component.set('trailer', t); } } static isNative(f) { return f.Type === 'Boolean' || f.Type === 'data' || f.Type === 'LocalMktDate' || f.Type === 'UTCDateOnly' || f.Type === 'UTCTimestamp'; } static makeSimple(f, type) { return new definition_1.SimpleFieldDefinition(f.Tag, f.Name, f.AbbrName ? f.AbbrName : f.Name, f.BaseCategory, f.BaseCategoryAbbrName, type.toUpperCase(), f.Description); } getType(f) { var _a; const types = this.dataTypeLookup; const nativeType = Repository.isNative(f); let type = f.Type; const mapped = !nativeType && types.get(type); if (f.Type !== 'Length' && mapped) { type = (_a = mapped.BaseType) !== null && _a !== void 0 ? _a : type; } return type; } fieldEnums() { const definitions = this.definitions; for (const e of this.Enums) { const field = definitions.tagToSimple[parseInt(e.Tag, 10)]; if (field == null) { continue; } if (e.Value && e.SymbolicName) { field.addEnum(e.Value, e.SymbolicName, e.Description); } } } fields() { this.dataTypeLookup = this.types(); const definitions = this.definitions; const types = this.dataTypeLookup; types.delete('boolean'); types.delete('data'); this.Fields.forEach((f) => { const type = this.getType(f); definitions.addSimpleFieldDef(Repository.makeSimple(f, type)); }); this.fieldEnums(); } contents() { return this.MsgContents.reduce((a, current) => { let content = a.get(current.ComponentID); if (!content) { content = []; a.set(current.ComponentID, content); } content[content.length] = current; return a; }, new Map()); } resolveToFieldSet(content, parentSet) { const builder = new contained_1.ContainedSetBuilder(parentSet); content.forEach((current) => { const required = current.Reqd === '1'; const tag = parseInt(current.TagText, 10); if (!isNaN(tag)) { const sf = this.definitions.tagToSimple[tag]; if (sf) { builder.add(new contained_1.ContainedSimpleField(sf, parentSet.fields.length, required, false)); } } else { let childSet = this.definitions.component.get(current.TagText); if (!childSet) { const cl = this.componentLookup.get(current.TagText); if (cl) { childSet = this.resolve(cl); } } if (childSet) { switch (childSet.type) { case contained_set_type_1.ContainedSetType.Component: { builder.add(new contained_1.ContainedComponentField(childSet, parentSet.fields.length, required)); break; } case contained_set_type_1.ContainedSetType.Group: { builder.add(new contained_1.ContainedGroupField(childSet, parentSet.fields.length, required)); break; } default: { throw new Error(`unknown set type ${childSet.type}`); } } } } return parentSet; }); } resolve(c) { var _a, _b; switch (c.ComponentType) { case 'ImplicitBlockRepeating': case 'BlockRepeating': { const content = (_a = this.contentLookup.get(c.ComponentID)) !== null && _a !== void 0 ? _a : []; const noField = this.definitions.tagToSimple[parseInt(content[0].TagText, 10)]; let def = this.groupLookup.get(c.ComponentID); if (!def) { def = new definition_1.GroupFieldDefinition(c.Name, c.AbbrName, c.CategoryID, noField, c.Description); this.resolveToFieldSet(content.slice(1), def); this.groupLookup.set(c.ComponentID, def); } return def; } default: { const content = (_b = this.contentLookup.get(c.ComponentID)) !== null && _b !== void 0 ? _b : []; let def = this.definitions.component.get(c.Name); if (!def) { def = new definition_1.ComponentFieldDefinition(c.Name, c.AbbrName, c.CategoryID, c.Description); this.resolveToFieldSet(content, def); this.definitions.addComponentFieldDef(def); } return def; } } } message(m) { var _a; const definitions = this.definitions; const content = (_a = this.contentLookup.get(m.ComponentID)) !== null && _a !== void 0 ? _a : []; let def = definitions.message.get(m.Name); if (!def) { def = new definition_1.MessageDefinition(m.Name, m.AbbrName, m.MsgType, m.CategoryID, m.Description); this.resolveToFieldSet(content, def); definitions.addComponentFieldDef(def); } return def; } components() { return this.Components.reduce((a, current) => { a.set(current.Name, current); a.set(current.ComponentID, current); return a; }, new Map()); } types() { return this.DataTypes.reduce((a, current) => { a.set(current.Name, current); return a; }, new Map()); } } exports.Repository = Repository; //# sourceMappingURL=repository.js.map