Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(helpers): add support for complex intermediate types #2550

Merged
merged 18 commits into from Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
216 changes: 216 additions & 0 deletions src/modules/helpers/eval.ts
@@ -0,0 +1,216 @@
import { FakerError } from '../../errors/faker-error';
import type { Faker } from '../../faker';

const REGEX_DOT_OR_BRACKET = /\.|\(/;

/**
* Resolves the given expression and returns its result. This method should only be used when using serialized expressions.
*
* This method is useful if you have to build a random string from a static, non-executable source
* (e.g. string coming from a developer, stored in a database or a file).
*
* It tries to resolve the expression on the given/default entrypoints:
*
* ```js
* const firstName = fakeEval('person.firstName', faker);
* const firstName2 = fakeEval('person.first_name', faker);
* ```
*
* Is equivalent to:
*
* ```js
* const firstName = faker.person.firstName();
* const firstName2 = faker.helpers.arrayElement(faker.rawDefinitions.person.first_name);
* ```
*
* You can provide parameters as well. At first, they will be parsed as json,
* and if that isn't possible, it will fall back to string:
*
* ```js
* const message = fakeEval('phone.number(+!# !## #### #####!)', faker);
* ```
*
* It is also possible to use multiple parameters (comma separated).
*
* ```js
* const pin = fakeEval('string.numeric(4, {"allowLeadingZeros": true})', faker);
* ```
*
* This method can resolve expressions with varying depths (dot separated parts).
*
* ```ts
* const airlineModule = fakeEval('airline', faker); // AirlineModule
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
* const airlineObject = fakeEval('airline.airline', faker); // { name: 'Etihad Airways', iataCode: 'EY' }
* const airlineCode = fakeEval('airline.airline.iataCode', faker); // 'EY'
* const airlineName = fakeEval('airline.airline().name', faker); // 'Etihad Airways'
* const airlineMethodName = fakeEval('airline.airline.name', faker); // 'bound airline'
* ```
*
* It is NOT possible to access any values not passed as entrypoints.
*
* This method will never return arrays, as it will pick a random element from them instead.
*
* @param expression The expression to evaluate on the entrypoints.
* @param faker The faker instance to resolve array elements.
* @param entrypoints The entrypoints to use when evaluating the expression.
*
* @see faker.helpers.fake() If you wish to have a string with multiple expressions.
*
* @example
* fakeEval('person.lastName', faker) // 'Barrows'
* fakeEval('helpers.arrayElement(["heads", "tails"])', faker) // 'tails'
* fakeEval('number.int(9999)', faker) // 4834
*
* @since 8.4.0
*/
export function fakeEval(
expression: string,
faker: Faker,
entrypoints: ReadonlyArray<unknown> = [faker, faker.rawDefinitions]
): unknown {
if (expression.length === 0) {
throw new FakerError('Eval expression cannot be empty.');
}

let current = entrypoints;
let remaining = expression;
do {
let index: number;
if (remaining.startsWith('(')) {
[index, current] = evalProcessFunction(remaining, current);
} else {
[index, current] = evalProcessExpression(remaining, current);
}

remaining = remaining.substring(index);

// Remove garbage and resolve array values
current = current
.filter((value) => value != null)
.map((value): unknown =>
Array.isArray(value) ? faker.helpers.arrayElement(value) : value
);
} while (remaining.length > 0 && current.length > 0);

if (current.length === 0) {
throw new FakerError(`Expression '${expression}' cannot be resolved.`);
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
}

const value = current[0];
return typeof value === 'function' ? value() : value;
}

/**
* Evaluates a function call and returns the remaining string and the mapped results.
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
*
* @param input The input string to parse.
* @param entrypoints The entrypoints to attempt the call on.
*/
function evalProcessFunction(
input: string,
entrypoints: ReadonlyArray<unknown>
): [remaining: number, mapped: unknown[]] {
const [index, params] = findParams(input);
const nextChar = input[index + 1];
switch (nextChar) {
case '.':
case '(':
xDivisionByZerox marked this conversation as resolved.
Show resolved Hide resolved
case undefined:
break; // valid
default:
throw new FakerError(
`Expected dot ('.'), open parenthesis ('('), or nothing after function call but got '${nextChar}'`
);
}

return [
index + (nextChar === '.' ? 2 : 1), // one for the closing bracket, one for the dot
entrypoints.map((entrypoint): unknown =>
typeof entrypoint === 'function' ? entrypoint(...params) : undefined
),
];
}

/**
* Tries to find the parameters of a function call.
*
* @param input The input string to parse.
*/
function findParams(input: string): [remaining: number, params: unknown[]] {
let index = input.indexOf(')');
while (index !== -1) {
const params = input.substring(1, index);
try {
// assuming that the params are valid JSON
return [index, JSON.parse(`[${params}]`) as unknown[]];
xDivisionByZerox marked this conversation as resolved.
Show resolved Hide resolved
} catch {
if (!params.includes("'") && !params.includes('"')) {
try {
// assuming that the params are a single unquoted string
return [index, JSON.parse(`["${params}"]`) as unknown[]];
} catch {
// try again with the next index
}

Check warning on line 153 in src/modules/helpers/eval.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/helpers/eval.ts#L152-L153

Added lines #L152 - L153 were not covered by tests
}
}

index = input.indexOf(')', index + 1);
}

throw new FakerError(
`Function parameters cannot be parsed as JSON or simple string: '${input}'`
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
);
}

Check warning on line 163 in src/modules/helpers/eval.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/helpers/eval.ts#L156-L163

Added lines #L156 - L163 were not covered by tests

/**
* Processes one expression part and returns the remaining index and the mapped results.
*
* @param input The input string to parse.
* @param entrypoints The entrypoints to resolve on.
*/
function evalProcessExpression(
input: string,
entrypoints: ReadonlyArray<unknown>
): [remaining: number, mapped: unknown[]] {
const result = REGEX_DOT_OR_BRACKET.exec(input);
const dotMatch = (result?.[0] ?? '') === '.';
const index = result?.index ?? input.length;
const key = input.substring(0, index);
if (key.length === 0) {
throw new FakerError(`Expression parts cannot be empty in '${input}'`);
}

Check warning on line 181 in src/modules/helpers/eval.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/helpers/eval.ts#L180-L181

Added lines #L180 - L181 were not covered by tests

return [
index + (dotMatch ? 1 : 0),
entrypoints.map((entrypoint) => resolveProperty(entrypoint, key)),
];
}

/**
* Resolves the given property on the given entrypoint.
*
* @param entrypoint The entrypoint to resolve the property on.
* @param key The property name to resolve.
*/
function resolveProperty(entrypoint: unknown, key: string): unknown {
switch (typeof entrypoint) {
case 'function': {
if (entrypoint[key as keyof typeof entrypoint] == null) {
try {
entrypoint = entrypoint();
} catch {
return undefined;
}

Check warning on line 203 in src/modules/helpers/eval.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/helpers/eval.ts#L202-L203

Added lines #L202 - L203 were not covered by tests
}

return entrypoint?.[key as keyof typeof entrypoint];
}
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved

case 'object': {
return entrypoint?.[key as keyof typeof entrypoint];
}

default:
return undefined;

Check warning on line 214 in src/modules/helpers/eval.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/helpers/eval.ts#L214

Added line #L214 was not covered by tests
}
}
61 changes: 6 additions & 55 deletions src/modules/helpers/index.ts
Expand Up @@ -2,6 +2,7 @@ import type { Faker, SimpleFaker } from '../..';
import { FakerError } from '../../errors/faker-error';
import { deprecated } from '../../internal/deprecated';
import { SimpleModuleBase } from '../../internal/module-base';
import { fakeEval } from './eval';
import { luhnCheckValue } from './luhn-check';
import type { RecordKey } from './unique';
import * as uniqueExec from './unique';
Expand Down Expand Up @@ -1417,66 +1418,16 @@ export class HelpersModule extends SimpleHelpersModule {
// extract method name from between the {{ }} that we found
// for example: {{person.firstName}}
const token = pattern.substring(start + 2, end + 2);
let method = token.replace('}}', '').replace('{{', '');

// extract method parameters
const regExp = /\(([^)]*)\)/;
const matches = regExp.exec(method);
let parameters = '';
if (matches) {
method = method.replace(regExp, '');
parameters = matches[1];
}

// split the method into module and function
const parts = method.split('.');

let currentModuleOrMethod: unknown = this.faker;
let currentDefinitions: unknown = this.faker.rawDefinitions;

// Search for the requested method or definition
for (const part of parts) {
currentModuleOrMethod =
currentModuleOrMethod?.[part as keyof typeof currentModuleOrMethod];
currentDefinitions =
currentDefinitions?.[part as keyof typeof currentDefinitions];
}

// Make method executable
let fn: (...args: unknown[]) => unknown;
if (typeof currentModuleOrMethod === 'function') {
fn = currentModuleOrMethod as (args?: unknown) => unknown;
} else if (Array.isArray(currentDefinitions)) {
fn = () =>
this.faker.helpers.arrayElement(currentDefinitions as unknown[]);
} else {
throw new FakerError(`Invalid module method or definition: ${method}
- faker.${method} is not a function
- faker.definitions.${method} is not an array`);
}

// assign the function from the module.function namespace
fn = fn.bind(this);

// If parameters are populated here, they are always going to be of string type
// since we might actually be dealing with an object or array,
// we always attempt to the parse the incoming parameters into JSON
let params: unknown[];
// Note: we experience a small performance hit here due to JSON.parse try / catch
// If anyone actually needs to optimize this specific code path, please open a support issue on github
try {
params = JSON.parse(`[${parameters}]`);
} catch {
// since JSON.parse threw an error, assume parameters was actually a string
params = [parameters];
}
const method = token.replace('}}', '').replace('{{', '');

const result = String(fn(...params));
const result = fakeEval(method, this.faker);
const stringified =
typeof result === 'object' ? JSON.stringify(result) : String(result);

// Replace the found tag with the returned fake value
// We cannot use string.replace here because the result might contain evaluated characters
const res =
pattern.substring(0, start) + result + pattern.substring(end + 2);
pattern.substring(0, start) + stringified + pattern.substring(end + 2);

// return the response recursively until we are done finding all tags
return this.fake(res);
Expand Down
2 changes: 1 addition & 1 deletion test/modules/__snapshots__/person.spec.ts.snap
Expand Up @@ -50,7 +50,7 @@ exports[`person > 42 > suffix > with sex 1`] = `"III"`;

exports[`person > 42 > zodiacSign 1`] = `"Gemini"`;

exports[`person > 1211 > bio 1`] = `"infrastructure supporter, photographer 🙆‍♀️"`;
exports[`person > 1211 > bio 1`] = `"teletype lover, dreamer 👄"`;
matthewmayer marked this conversation as resolved.
Show resolved Hide resolved

exports[`person > 1211 > firstName > noArgs 1`] = `"Tito"`;

Expand Down