Skip to content

Commit

Permalink
feat(core): expose spel evaluator in spinnaker/core (#4972)
Browse files Browse the repository at this point in the history
  • Loading branch information
anotherchrisberry committed Mar 8, 2018
1 parent 5a7eed5 commit 873cb70
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import * as spel2js from 'spel2js';
import { parseSpelExpressions } from '../spel2js.templateParser';
import { BindAll } from 'lodash-decorators';
import { truncate } from 'lodash';

Expand Down Expand Up @@ -66,7 +66,7 @@ export class ExpressionInput extends React.Component<IExpressionInputProps, IExp
};

try {
const exprs = spel2js.TemplateParser.parse(value);
const exprs = parseSpelExpressions(value);
const results = exprs.map(expr => expr.eval(context, locals));
this.setState({ spelError: null, spelPreview: results.join(''), value });

Expand Down
2 changes: 1 addition & 1 deletion app/scripts/modules/core/src/presentation/forms/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
///<reference path="./spel2js.d.ts" />

export * from './components';
import './spel2js.templateParser';
export * from './spel2js.templateParser';
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
import * as spel2js from 'spel2js';
import { SpelExpression } from 'spel2js';

(spel2js as any).TemplateParser = {
parse(template: string): SpelExpression[] {
const spelExpressions = new TemplateAwareExpressionParser().parseExpressions(template);

// A Monkey patch which adds the current context when an exception occurs
spelExpressions.forEach(expr => {
const getValue = expr._compiledExpression.getValue;
expr._compiledExpression.getValue = function() {
const state = arguments[0];
try {
return getValue.apply(expr._compiledExpression, arguments);
} catch (err) {
err.state = state;
throw err;
}
export function parseSpelExpressions(template: string): SpelExpression[] {
const spelExpressions = new TemplateAwareExpressionParser().parseExpressions(template);

// A Monkey patch which adds the current context when an exception occurs
spelExpressions.forEach(expr => {
const getValue = expr._compiledExpression.getValue;
expr._compiledExpression.getValue = function() {
const state = arguments[0];
try {
return getValue.apply(expr._compiledExpression, arguments);
} catch (err) {
err.state = state;
throw err;
}
});
}
});

return spelExpressions;
}
};
return spelExpressions;
}

const literalExpression = (literalString: string) =>
spel2js.SpelExpressionEvaluator.compile(`'${literalString.replace(/'/g, "''")}'`);
Expand Down

0 comments on commit 873cb70

Please sign in to comment.