UNPKG

@freemework/common

Version:

Common library of the Freemework Project.

128 lines 5.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FConfigurationChain = void 0; const f_configuration_js_1 = require("./f_configuration.js"); const f_configuration_exception_js_1 = require("./f_configuration_exception.js"); const f_configuration_value_js_1 = require("./f_configuration_value.js"); const index_js_1 = require("../exception/index.js"); class FConfigurationChain extends f_configuration_js_1.FConfiguration { get sourceURI() { return this._sourceURI; } get namespaceFull() { return this._configurations[0].namespaceFull; } get namespaceParent() { return this._configurations[0].namespaceParent; } get keys() { const union = new Set(); for (const item of this._configurations) { for (const key of item.keys) { union.add(key); } } return Object.freeze([...union]); } constructor(...configurations) { super(); if (configurations.length === 0) { throw new index_js_1.FExceptionArgument("At least one configuration required for chain.", "configurations"); } const innerCommaSeparatedSourceURIs = configurations .map(s => s.sourceURI.toString()) .map(s => encodeURIComponent(s)) .join(","); const sourceURI = `configuration:chain?sources=${innerCommaSeparatedSourceURIs}`; this._sourceURI = new URL(sourceURI); this._configurations = Object.freeze(configurations); } getArray(key, indexesName = f_configuration_js_1.FConfiguration.DEFAULT_INDEXES_KEY) { const arrayNS = this.getNamespace(key); const arrayIndexes = arrayNS.get(indexesName).asString .split(" ") .filter(s => s !== ""); const arrayNamespaces = arrayIndexes.map(s => { return arrayNS.getNamespace(s); }); return arrayNamespaces; } getNamespace(namespaceFull) { const innerConfiguration = this.findNamespace(namespaceFull); if (innerConfiguration !== null) { return innerConfiguration; } // Force underlying config to raise error for (const configuration of this._configurations) { configuration.getNamespace(namespaceFull); } // just a guard, should not happens if underlying configuration is implemented correctly throw new f_configuration_exception_js_1.FConfigurationException(`Namespace was not found in the configuration.`, namespaceFull); } get(key, defaultData) { const foundValue = this.find(key); if (foundValue !== null) { return foundValue; } const namespaceFull = this.namespaceFull; const fullKey = namespaceFull !== null ? `${namespaceFull}.${key}` : key; if (defaultData !== undefined) { const value = f_configuration_value_js_1.FConfigurationValue.factory(fullKey, defaultData, null, null); return value; } else { throw new f_configuration_exception_js_1.FConfigurationException("Current configuration does not have such key. Check your configuration.", fullKey); } } findNamespace(namespaceFull) { const innerConfigurations = []; for (const configuration of this._configurations) { if (configuration.hasNamespace(namespaceFull)) { innerConfigurations.push(configuration.getNamespace(namespaceFull)); } } if (innerConfigurations.length === 0) { return null; } return new FConfigurationChain(...innerConfigurations); } find(key) { for (let itemIndex = 0; itemIndex < this._configurations.length; ++itemIndex) { const configurationItem = this._configurations[itemIndex]; if (configurationItem.has(key)) { return configurationItem.get(key); } } return null; } hasNamespace(namespaceFull) { for (const configuration of this._configurations) { if (configuration.hasNamespace(namespaceFull)) { return true; } else if (configuration.has(namespaceFull)) { const isMaskedNamespace = configuration.get(namespaceFull).isNull; if (isMaskedNamespace) { // This is masked namespace return false; } } } return false; } has(key) { for (const configuration of this._configurations) { if (configuration.has(key)) { return true; } } return false; // no one configurations contains key } _sourceURI; /** * List of inner configurations. Values from first more important! */ _configurations; } exports.FConfigurationChain = FConfigurationChain; //# sourceMappingURL=f_configuration_chain.js.map