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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix params API to match agreed spec #1268

Merged
merged 2 commits into from Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -0,0 +1 @@
Correct the function BooleanExpression#then to BooleanExpression#thenElse (#1268)
4 changes: 2 additions & 2 deletions spec/fixtures/sources/commonjs-params/index.js
Expand Up @@ -4,10 +4,10 @@ const params = require("../../../../src/params");

params.defineString("BORING");
const foo = params.defineString("FOO", { input: { text: { validationRegex: "w+" } } });
const bar = params.defineString("BAR", { default: foo , label: "asdf" });
const bar = params.defineString("BAR", { default: foo, label: "asdf" });
params.defineString("BAZ", { input: { select: { options: [{ value: "a" }, { value: "b" }] } } });

params.defineInt("AN_INT", { default: bar.equals("qux").then(0, 1) });
params.defineInt("AN_INT", { default: bar.equals("qux").thenElse(0, 1) });
params.defineInt("ANOTHER_INT", {
input: {
select: {
Expand Down
22 changes: 12 additions & 10 deletions spec/params/params.spec.ts
Expand Up @@ -5,7 +5,7 @@ describe("Params spec extraction", () => {
it("converts Expressions in the param default to strings", () => {
const bar = params.defineInt("BAR");
expect(
params.defineString("FOO", { default: bar.notEquals(22).then("asdf", "jkl;") }).toSpec()
params.defineString("FOO", { default: bar.notEquals(22).thenElse("asdf", "jkl;") }).toSpec()
.default
).to.equal(`{{ params.BAR != 22 ? "asdf" : "jkl;" }}`);
});
Expand Down Expand Up @@ -174,13 +174,13 @@ describe("Params value extraction", () => {

it("can select the output of a ternary expression based on the comparison", () => {
const trueExpr = params.defineString("A_STRING").equals(params.defineString("SAME_STRING"));
expect(trueExpr.then(1, 0).value()).to.equal(1);
expect(trueExpr.thenElse(1, 0).value()).to.equal(1);
const falseExpr = params.defineInt("AN_INT").equals(params.defineInt("DIFF_INT"));
expect(falseExpr.then(1, 0).value()).to.equal(0);
expect(falseExpr.thenElse(1, 0).value()).to.equal(0);

const twentytwo = params.defineInt("DIFF_INT");
expect(trueExpr.then(twentytwo, 0).value()).to.equal(22);
expect(falseExpr.then(1, twentytwo).value()).to.equal(22);
expect(trueExpr.thenElse(twentytwo, 0).value()).to.equal(22);
expect(falseExpr.thenElse(1, twentytwo).value()).to.equal(22);
});
});

Expand Down Expand Up @@ -280,13 +280,15 @@ describe("Params as CEL", () => {
expect(
booleanExpr.then(params.defineString("FOO"), params.defineString("BAR")).toCEL()
).to.equal("{{ params.BOOL ? params.FOO : params.BAR }}");
expect(cmpExpr.then("asdf", "jkl;").toCEL()).to.equal(
expect(cmpExpr.thenElse("asdf", "jkl;").toCEL()).to.equal(
'{{ params.A != params.B ? "asdf" : "jkl;" }}'
);
expect(cmpExpr.then(-11, 22).toCEL()).to.equal("{{ params.A != params.B ? -11 : 22 }}");
expect(cmpExpr.then(false, true).toCEL()).to.equal("{{ params.A != params.B ? false : true }}");
expect(cmpExpr.then(params.defineString("FOO"), params.defineString("BAR")).toCEL()).to.equal(
"{{ params.A != params.B ? params.FOO : params.BAR }}"
expect(cmpExpr.thenElse(-11, 22).toCEL()).to.equal("{{ params.A != params.B ? -11 : 22 }}");
expect(cmpExpr.thenElse(false, true).toCEL()).to.equal(
"{{ params.A != params.B ? false : true }}"
);
expect(
cmpExpr.thenElse(params.defineString("FOO"), params.defineString("BAR")).toCEL()
).to.equal("{{ params.A != params.B ? params.FOO : params.BAR }}");
});
});
4 changes: 2 additions & 2 deletions spec/runtime/manifest.spec.ts
Expand Up @@ -127,14 +127,14 @@ describe("stackToWire", () => {
labels: {},
httpsTrigger: {},
concurrency: intParam,
maxInstances: intParam.equals(24).then(-1, 1),
maxInstances: intParam.equals(24).thenElse(-1, 1),
},
v2schedule: {
platform: "gcfv2",
entryPoint: "v2callable",
labels: {},
scheduleTrigger: {
schedule: stringParam.equals("America/Mexico_City").then("mexico", "usa"),
schedule: stringParam.equals("America/Mexico_City").thenElse("mexico", "usa"),
timeZone: stringParam,
},
},
Expand Down
2 changes: 1 addition & 1 deletion src/params/types.ts
Expand Up @@ -145,7 +145,7 @@ export class CompareExpression<
}

/** Returns a TernaryExpression which can resolve to one of two values, based on the resolution of this comparison. */
then<retT extends string | number | boolean | string[]>(
thenElse<retT extends string | number | boolean | string[]>(
ifTrue: retT | Expression<retT>,
ifFalse: retT | Expression<retT>
) {
Expand Down