Skip to content

Commit aecd894

Browse files
authored
fix(util-endpoints): skip evaluation for arg of type number (#3936)
1 parent 876330f commit aecd894

File tree

4 files changed

+16
-14
lines changed

4 files changed

+16
-14
lines changed

packages/util-endpoints/src/types/shared.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Logger } from "@aws-sdk/types";
33
export type ReferenceObject = { ref: string };
44

55
export type FunctionObject = { fn: string; argv: FunctionArgv };
6-
export type FunctionArgv = Array<string | boolean | ReferenceObject | FunctionObject>;
6+
export type FunctionArgv = Array<Expression | boolean | number>;
77
export type FunctionReturn = string | boolean | number | { [key: string]: FunctionReturn };
88

99
export type ConditionObject = FunctionObject & { assign?: string };

packages/util-endpoints/src/utils/callFunction.spec.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,21 @@ describe(callFunction.name, () => {
2222
jest.clearAllMocks();
2323
});
2424

25-
it("skips evaluateExpression for boolean arg", () => {
26-
const mockBooleanArg = true;
27-
const mockFn = { fn: mockFunctionName, argv: [mockBooleanArg] };
25+
it.each([
26+
["boolean", true],
27+
["boolean", false],
28+
["number", 1],
29+
["number", 0],
30+
])("skips evaluateExpression for %s arg: %s", (argType, mockNotExpressionArg) => {
31+
const mockFn = { fn: mockFunctionName, argv: [mockNotExpressionArg] };
2832
const result = callFunction(mockFn, mockOptions);
2933
expect(result).toBe(mockReturn);
3034
expect(evaluateExpression).not.toHaveBeenCalled();
31-
expect(lib[mockFunctionName]).toHaveBeenCalledWith(mockBooleanArg);
35+
expect(lib[mockFunctionName]).toHaveBeenCalledWith(mockNotExpressionArg);
3236
});
3337

3438
it.each(["string", { ref: "ref" }, { fn: "fn", argv: [] }])(
35-
"calls evaluateExpression for non-boolean arg: %s",
39+
"calls evaluateExpression for expression arg: %s",
3640
(arg) => {
3741
const mockFn = { fn: mockFunctionName, argv: [arg] };
3842

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import * as lib from "../lib";
2-
import { EvaluateOptions, FunctionObject, FunctionReturn } from "../types";
2+
import { EvaluateOptions, Expression, FunctionObject, FunctionReturn } from "../types";
33
import { evaluateExpression } from "./evaluateExpression";
44

55
export const callFunction = ({ fn, argv }: FunctionObject, options: EvaluateOptions): FunctionReturn => {
6-
const evaluatedArgs = argv.map((arg) => (typeof arg === "boolean" ? arg : evaluateExpression(arg, "arg", options)));
6+
const evaluatedArgs = argv.map((arg) =>
7+
["boolean", "number"].includes(typeof arg) ? arg : evaluateExpression(arg as Expression, "arg", options)
8+
);
79
// @ts-ignore Element implicitly has an 'any' type
810
return fn.split(".").reduce((acc, key) => acc[key], lib)(...evaluatedArgs);
911
};

packages/util-endpoints/src/utils/evaluateExpression.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
import { EndpointError, EvaluateOptions, FunctionObject, ReferenceObject } from "../types";
1+
import { EndpointError, EvaluateOptions, Expression, FunctionObject, ReferenceObject } from "../types";
22
import { callFunction } from "./callFunction";
33
import { evaluateTemplate } from "./evaluateTemplate";
44
import { getReferenceValue } from "./getReferenceValue";
55

6-
export const evaluateExpression = (
7-
obj: string | FunctionObject | ReferenceObject,
8-
keyName: string,
9-
options: EvaluateOptions
10-
) => {
6+
export const evaluateExpression = (obj: Expression, keyName: string, options: EvaluateOptions) => {
117
if (typeof obj === "string") {
128
return evaluateTemplate(obj, options);
139
} else if ((obj as FunctionObject)["fn"]) {

0 commit comments

Comments
 (0)