feat: Pass confs to spell interpret()

This commit is contained in:
1ambda 2017-02-07 11:23:37 +09:00
parent 115191ee7e
commit 70ebe29fee
4 changed files with 48 additions and 26 deletions

View file

@ -218,7 +218,7 @@ export default function HeliumCtrl($scope, $rootScope, $sce,
return;
}
heliumService.getSinglePackageConfig(pkgName, pkgVersion)
heliumService.getSinglePackageConfigs(pkgName, pkgVersion)
.then(conf => {
$scope.defaultPackageConfigs[pkgName] = conf;
pkgSearchResult.configOpened = true;

View file

@ -41,12 +41,12 @@ export function mergePersistedConfWithSpec(persisted, spec) {
return confs;
}
export function createSinglePackageConfig(pkg, persistedConf) {
export function createSinglePackageConfigs(pkg, persistedConf) {
const spec = pkg.config;
if (!spec) { return; }
if (!spec) { return []; }
const version = pkg.version;
if (!version) { return; }
if (!version) { return []; }
if (!persistedConf) { persistedConf = {}; }
@ -98,8 +98,8 @@ export function parseConfigValue(type, stringified) {
* persist key-value only
* since other info (e.g type, desc) can be provided by default config
*/
export function createPersistableConfig(currentConf) {
const filtered = currentConf.reduce((acc, c) => {
export function createPersistableConfig(currentConfs) {
const filtered = currentConfs.reduce((acc, c) => {
acc[c.name] = parseConfigValue(c.type, c.value);
return acc;
}, {});

View file

@ -56,7 +56,8 @@ export function createDefaultPackages(pkgSearchResults, sce) {
export function findPackageByMagic(defaultPackages, magic) {
for (let name in defaultPackages) {
const pkgSearchResult = defaultPackages[name];
if (pkgSearchResult.pkg.type === HeliumType.SPELL &&
if (pkgSearchResult.enabled &&
pkgSearchResult.pkg.type === HeliumType.SPELL &&
pkgSearchResult.pkg.spell.magic === magic) {
return pkgSearchResult;
}

View file

@ -15,7 +15,7 @@
import { HeliumType, } from './helium-type';
import {
createAllPackageConfigs,
createSinglePackageConfig,
createSinglePackageConfigs,
createPersistableConfig,
} from './helium-conf';
import {
@ -68,23 +68,36 @@ export default function heliumService($http, $sce, baseUrlSrv) {
return spellPerMagic[magic];
};
/**
*/
this.executeSpell = function(magic, textWithoutMagic) {
const spell = this.getSpellByMagic(magic);
const spellResult = spell.interpret(textWithoutMagic);
const parsed = spellResult.getAllParsedDataWithTypes(
spellPerMagic, magic, textWithoutMagic);
const promisedConf = this.getSinglePackageConfigUsingMagic(magic)
.then(confs => {
return createPersistableConfig(confs);
});
return parsed;
return promisedConf.then(conf => {
const spell = this.getSpellByMagic(magic);
const spellResult = spell.interpret(textWithoutMagic, conf);
const parsed = spellResult.getAllParsedDataWithTypes(
spellPerMagic, magic, textWithoutMagic);
return parsed;
});
};
this.executeSpellAsDisplaySystem = function(type, data) {
const spell = this.getSpellByMagic(type);
const spellResult = spell.interpret(data.trim());
const parsed = spellResult.getAllParsedDataWithTypes(spellPerMagic);
const promisedConf = this.getSinglePackageConfigUsingMagic(type)
.then(confs => {
return createPersistableConfig(confs);
});
return parsed;
return promisedConf.then(conf => {
const spell = this.getSpellByMagic(type);
const spellResult = spell.interpret(data.trim(), conf);
const parsed = spellResult.getAllParsedDataWithTypes(spellPerMagic);
return parsed;
});
};
this.getVisualizationBundles = function() {
@ -180,7 +193,7 @@ export default function heliumService($http, $sce, baseUrlSrv) {
/**
* get all package configs.
* @return Promise<{name, {version, {confKey, confVal}}}>
* @return { Promise<{name, Array<Object>}> }
*/
this.getAllPackageConfigs = function() {
const promisedDefaultPackages = this.getDefaultPackages();
@ -204,9 +217,9 @@ export default function heliumService($http, $sce, baseUrlSrv) {
/**
* get the package config which is persisted in server.
* @return Promise<{confKey, confVal}>
* @return { Promise<Array<Object>> }
*/
this.getSinglePackageConfig = function(pkgName, pkgVersion) {
this.getSinglePackageConfigs = function(pkgName, pkgVersion) {
const promisedPkgSearchResult = this.getSinglePackageInfo(pkgName, pkgVersion);
const confUrl = `${baseUrlSrv.getRestApiBase()}/helium/config/${pkgName}/${pkgVersion}`;
@ -220,7 +233,7 @@ export default function heliumService($http, $sce, baseUrlSrv) {
const pkgSearchResult = values[0];
const persistedConf = values[1];
const merged = createSinglePackageConfig(pkgSearchResult.pkg, persistedConf);
const merged = createSinglePackageConfigs(pkgSearchResult.pkg, persistedConf);
return merged;
})
.catch(function(error) {
@ -229,13 +242,21 @@ export default function heliumService($http, $sce, baseUrlSrv) {
};
this.getSinglePackageConfigUsingMagic = function(magic) {
this.getDefaultPackages()
const promised = this.getDefaultPackages()
.then(defaultPackages => {
const pkgSearchResult = findPackageByMagic(defaultPackages, magic)
// return empty conf if failed to find pkg
if (!pkgSearchResult) {
return {};
}
const pkgVersion = pkgSearchResult.pkg.version;
const pkgName = pkgSearchResult.pkg.name;
return this.getSinglePackageConfig(pkgName, pkgVersion);
})
return this.getSinglePackageConfigs(pkgName, pkgVersion);
});
return promised;
};
}