Fix recursive render

This commit is contained in:
Gregory Schier 2017-01-13 11:21:03 -08:00
parent 0c66afbf4b
commit 3b8a73231e
3 changed files with 45 additions and 25 deletions

View File

@ -62,39 +62,56 @@ describe('buildRenderContext()', () => {
});
});
it('rendered recursive should not infinite loop', () => {
const ancestors = [{
// Sub Environment
type: models.requestGroup.type,
environment: {recursive: '{{ recursive }}/hello'}
}];
const context = renderUtils.buildRenderContext(ancestors);
expect(context).toEqual({recursive: '/hello'});
});
it('cascades properly and renders', () => {
const ancestors = [
{
type: models.requestGroup.type,
environment: {bar: '{{ foo }} parent', recursive: '{{ recursive }}', ancestor: true}
environment: {
base_url: '{{ base_url }}/resource',
ancestor: true,
winner: 'folder parent'
}
},
{
type: models.requestGroup.type,
environment: {bar: '{{ foo }} grandparent', ancestor: true}
environment: {
ancestor: true,
winner: 'folder grandparent'
}
}
];
const rootEnvironment = {
type: models.environment.type,
data: {foo: 'root', root: true}
data: {winner: 'root', root: true}
};
const subEnvironment = {
type: models.environment.type,
data: {foo: 'sub', sub: true}
data: {winner: 'sub', sub: true, base_url: 'https://insomnia.rest'}
};
const context = renderUtils.buildRenderContext(
ancestors,
const context = renderUtils.buildRenderContext(ancestors,
rootEnvironment,
subEnvironment
);
expect(context).toEqual({
foo: 'sub',
bar: 'sub parent',
recursive: '{{ recursive }}',
base_url: 'https://insomnia.rest/resource',
ancestor: true,
winner: 'folder parent',
root: true,
sub: true
});

View File

@ -60,33 +60,35 @@ export function render (template, context = {}) {
}
export function buildRenderContext (ancestors, rootEnvironment, subEnvironment) {
const renderContext = {};
if (rootEnvironment) {
Object.assign(renderContext, rootEnvironment.data);
}
if (subEnvironment) {
Object.assign(renderContext, subEnvironment.data);
}
if (!Array.isArray(ancestors)) {
ancestors = [];
}
const environments = [];
if (rootEnvironment) {
environments.push(rootEnvironment.data);
}
if (subEnvironment) {
environments.push(subEnvironment.data);
}
// Merge all environments. Note that we're reversing ancestors because we want to merge
// from top-down (closest ancestor should win)
for (let doc of ancestors.reverse()) {
for (const doc of ancestors.reverse()) {
if (!doc.environment) {
continue;
}
Object.assign(renderContext, doc.environment);
environments.push(doc.environment);
}
// Now we're going to render the renderContext with itself.
// This is to support templating inside environments
return recursiveRender(renderContext, renderContext);
const renderContext = {};
for (const environment of environments) {
Object.assign(renderContext, recursiveRender(environment, renderContext));
}
return renderContext;
}
export function recursiveRender (obj, context) {

View File

@ -355,6 +355,7 @@ class App extends Component {
}
// Force refresh if environment changes
// TODO: Only do this for environments in this workspace (not easy because they're nested)
if (doc.type === models.environment.type) {
console.log('[App] Forcing update from environment change', change);
this._wrapper.forceRequestPaneRefresh();