Skip to content

Commit 7dbda7d

Browse files
authored
Fix params API to match agreed spec (#1268)
* Fix params API to match agreed spec * Fixes & changelog
1 parent 879c5c2 commit 7dbda7d

File tree

5 files changed

+18
-15
lines changed

5 files changed

+18
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Correct the function BooleanExpression#then to BooleanExpression#thenElse (#1268)

spec/fixtures/sources/commonjs-params/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ const params = require("../../../../src/params");
44

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

10-
params.defineInt("AN_INT", { default: bar.equals("qux").then(0, 1) });
10+
params.defineInt("AN_INT", { default: bar.equals("qux").thenElse(0, 1) });
1111
params.defineInt("ANOTHER_INT", {
1212
input: {
1313
select: {

spec/params/params.spec.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ describe("Params spec extraction", () => {
55
it("converts Expressions in the param default to strings", () => {
66
const bar = params.defineInt("BAR");
77
expect(
8-
params.defineString("FOO", { default: bar.notEquals(22).then("asdf", "jkl;") }).toSpec()
8+
params.defineString("FOO", { default: bar.notEquals(22).thenElse("asdf", "jkl;") }).toSpec()
99
.default
1010
).to.equal(`{{ params.BAR != 22 ? "asdf" : "jkl;" }}`);
1111
});
@@ -174,13 +174,13 @@ describe("Params value extraction", () => {
174174

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

181181
const twentytwo = params.defineInt("DIFF_INT");
182-
expect(trueExpr.then(twentytwo, 0).value()).to.equal(22);
183-
expect(falseExpr.then(1, twentytwo).value()).to.equal(22);
182+
expect(trueExpr.thenElse(twentytwo, 0).value()).to.equal(22);
183+
expect(falseExpr.thenElse(1, twentytwo).value()).to.equal(22);
184184
});
185185
});
186186

@@ -280,13 +280,15 @@ describe("Params as CEL", () => {
280280
expect(
281281
booleanExpr.then(params.defineString("FOO"), params.defineString("BAR")).toCEL()
282282
).to.equal("{{ params.BOOL ? params.FOO : params.BAR }}");
283-
expect(cmpExpr.then("asdf", "jkl;").toCEL()).to.equal(
283+
expect(cmpExpr.thenElse("asdf", "jkl;").toCEL()).to.equal(
284284
'{{ params.A != params.B ? "asdf" : "jkl;" }}'
285285
);
286-
expect(cmpExpr.then(-11, 22).toCEL()).to.equal("{{ params.A != params.B ? -11 : 22 }}");
287-
expect(cmpExpr.then(false, true).toCEL()).to.equal("{{ params.A != params.B ? false : true }}");
288-
expect(cmpExpr.then(params.defineString("FOO"), params.defineString("BAR")).toCEL()).to.equal(
289-
"{{ params.A != params.B ? params.FOO : params.BAR }}"
286+
expect(cmpExpr.thenElse(-11, 22).toCEL()).to.equal("{{ params.A != params.B ? -11 : 22 }}");
287+
expect(cmpExpr.thenElse(false, true).toCEL()).to.equal(
288+
"{{ params.A != params.B ? false : true }}"
290289
);
290+
expect(
291+
cmpExpr.thenElse(params.defineString("FOO"), params.defineString("BAR")).toCEL()
292+
).to.equal("{{ params.A != params.B ? params.FOO : params.BAR }}");
291293
});
292294
});

spec/runtime/manifest.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,14 @@ describe("stackToWire", () => {
127127
labels: {},
128128
httpsTrigger: {},
129129
concurrency: intParam,
130-
maxInstances: intParam.equals(24).then(-1, 1),
130+
maxInstances: intParam.equals(24).thenElse(-1, 1),
131131
},
132132
v2schedule: {
133133
platform: "gcfv2",
134134
entryPoint: "v2callable",
135135
labels: {},
136136
scheduleTrigger: {
137-
schedule: stringParam.equals("America/Mexico_City").then("mexico", "usa"),
137+
schedule: stringParam.equals("America/Mexico_City").thenElse("mexico", "usa"),
138138
timeZone: stringParam,
139139
},
140140
},

src/params/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export class CompareExpression<
145145
}
146146

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

0 commit comments

Comments
 (0)