Skip to content

Commit

Permalink
feat(@gabliam/core): use expression parser for @value
Browse files Browse the repository at this point in the history
  • Loading branch information
eyolas committed Nov 23, 2017
1 parent 003c93b commit 208f496
Show file tree
Hide file tree
Showing 8 changed files with 536 additions and 22 deletions.
1 change: 1 addition & 0 deletions packages/core/package.json
Expand Up @@ -13,6 +13,7 @@
"src"
],
"dependencies": {
"@gabliam/expression": "^6.1.0",
"@types/joi": "^13.0.0",
"caller": "^1.0.1",
"debug": "^3.0.0",
Expand Down
14 changes: 12 additions & 2 deletions packages/core/src/utils.ts
Expand Up @@ -4,6 +4,7 @@ import { ValueValidator, ValueExtractor } from './interfaces';
import { ValueValidationError } from './errors';
import { APP_CONFIG } from './constants';
import { Container } from './container';
import { ExpressionParser } from '@gabliam/expression';

/**
* Validate a value
Expand Down Expand Up @@ -45,9 +46,18 @@ export function configureValueExtractor(container: Container): ValueExtractor {
defaultValue: any,
validator?: ValueValidator | null
): any => {
let value = undefined;
const config = container.get<object>(APP_CONFIG);
try {
const expression = new ExpressionParser(config).parseExpression(path);
value = expression.getValue();
} catch {}

if (value === undefined) {
value = _.get(config, path, defaultValue);
}

try {
const config = container.get<object>(APP_CONFIG);
let value = _.get(config, path, defaultValue);
if (validator) {
value = valueValidator(path, value, validator);
}
Expand Down
@@ -0,0 +1,77 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`expression evaluator compile should compile an expression an return an evaluator 1`] = `1234`;

exports[`expression evaluator locals should refer to the root context 1`] = `
Object {
"myString": "global context",
}
`;

exports[`expression evaluator lookups should look up a doubly nested primitive in the context using dot notation 1`] = `true`;

exports[`expression evaluator lookups should look up a doubly nested primitive in the context using dot notation 2`] = `"boolean"`;

exports[`expression evaluator lookups should look up a nested primitive in the context using bracket notation 1`] = `undefined`;

exports[`expression evaluator lookups should look up a nested primitive in the context using bracket notation 2`] = `"undefined"`;

exports[`expression evaluator lookups should look up a nested primitive in the context using bracket notation literal 1`] = `"hi"`;

exports[`expression evaluator lookups should look up a nested primitive in the context using bracket notation literal 2`] = `"string"`;

exports[`expression evaluator lookups should look up a nested primitive in the context using dot notation 1`] = `"hi"`;

exports[`expression evaluator lookups should look up a nested primitive in the context using dot notation 2`] = `"string"`;

exports[`expression evaluator lookups should look up a primitive in the context 1`] = `1`;

exports[`expression evaluator lookups should look up a primitive in the context 2`] = `"number"`;

exports[`expression evaluator lookups should look up a really nested primitive in the context using bracket notation 1`] = `"bye"`;

exports[`expression evaluator lookups should look up a really nested primitive in the context using bracket notation 2`] = `"string"`;

exports[`expression evaluator lookups should return undefined 1`] = `undefined`;

exports[`expression evaluator lookups should return undefined 2`] = `"undefined"`;

exports[`expression evaluator parse primitives should evaluate a boolean 1`] = `true`;

exports[`expression evaluator parse primitives should evaluate a boolean 2`] = `"boolean"`;

exports[`expression evaluator parse primitives should evaluate a boolean 3`] = `false`;

exports[`expression evaluator parse primitives should evaluate a boolean 4`] = `"boolean"`;

exports[`expression evaluator parse primitives should evaluate a number 1`] = `123`;

exports[`expression evaluator parse primitives should evaluate a number 2`] = `"number"`;

exports[`expression evaluator parse primitives should evaluate a number 3`] = `123.4`;

exports[`expression evaluator parse primitives should evaluate a number 4`] = `"number"`;

exports[`expression evaluator parse primitives should evaluate a string 1`] = `"hello world!"`;

exports[`expression evaluator parse primitives should evaluate a string 2`] = `"string"`;

exports[`expression evaluator parse primitives should evaluate a string 3`] = `"hello world!"`;

exports[`expression evaluator parse primitives should evaluate a string 4`] = `"string"`;

exports[`expression evaluator parse primitives should evaluate a string with embedded escaped double quotes 1`] = `"hello world!"`;

exports[`expression evaluator parse primitives should evaluate a string with embedded escaped double quotes 2`] = `"string"`;

exports[`expression evaluator parse primitives should evaluate a string with embedded escaped double quotes 3`] = `"hello world!"`;

exports[`expression evaluator parse primitives should evaluate a string with embedded escaped double quotes 4`] = `"string"`;

exports[`expression evaluator parse primitives should evaluate a string with embedded escaped single quotes 1`] = `"hello world !"`;

exports[`expression evaluator parse primitives should evaluate a string with embedded escaped single quotes 2`] = `"string"`;

exports[`expression evaluator parse primitives should evaluate a string with embedded escaped single quotes 3`] = `"hello world !"`;

exports[`expression evaluator parse primitives should evaluate a string with embedded escaped single quotes 4`] = `"string"`;

0 comments on commit 208f496

Please sign in to comment.