From 16af0484921639ed5676c54459bf83670537774e Mon Sep 17 00:00:00 2001 From: Thomas Bouldin Date: Tue, 18 Oct 2022 08:42:31 -0400 Subject: [PATCH 1/2] Fix params API to match agreed spec --- spec/params/params.spec.ts | 18 +++++++++--------- spec/runtime/manifest.spec.ts | 4 ++-- src/params/index.ts | 8 ++++---- src/params/types.ts | 2 +- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/spec/params/params.spec.ts b/spec/params/params.spec.ts index 9148d5483..b4fa20785 100644 --- a/spec/params/params.spec.ts +++ b/spec/params/params.spec.ts @@ -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;" }}`); }); @@ -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); }); }); @@ -280,12 +280,12 @@ 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( + 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 }}" ); }); diff --git a/spec/runtime/manifest.spec.ts b/spec/runtime/manifest.spec.ts index e124f6190..22c5f8af1 100644 --- a/spec/runtime/manifest.spec.ts +++ b/spec/runtime/manifest.spec.ts @@ -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, }, }, diff --git a/src/params/index.ts b/src/params/index.ts index 5fb0b9c35..6e65fd43f 100644 --- a/src/params/index.ts +++ b/src/params/index.ts @@ -68,7 +68,7 @@ export function clearParams() { * A builtin param that resolves to the default RTDB database URL associated * with the project, without prompting the deployer. Empty string if none exists. */ -export const databaseURL: Param = new InternalExpression( +export const databaseURL: Expression = new InternalExpression( "DATABASE_URL", (env: NodeJS.ProcessEnv) => JSON.parse(env.FIREBASE_CONFIG)?.databaseURL || "" ); @@ -76,7 +76,7 @@ export const databaseURL: Param = new InternalExpression( * A builtin param that resolves to the Cloud project ID associated with * the project, without prompting the deployer. */ -export const projectID: Param = new InternalExpression( +export const projectID: Expression = new InternalExpression( "PROJECT_ID", (env: NodeJS.ProcessEnv) => JSON.parse(env.FIREBASE_CONFIG)?.projectId || "" ); @@ -84,7 +84,7 @@ export const projectID: Param = new InternalExpression( * A builtin param that resolves to the Cloud project ID, without prompting * the deployer. */ -export const gcloudProject: Param = new InternalExpression( +export const gcloudProject: Expression = new InternalExpression( "GCLOUD_PROJECT", (env: NodeJS.ProcessEnv) => JSON.parse(env.FIREBASE_CONFIG)?.projectId || "" ); @@ -93,7 +93,7 @@ export const gcloudProject: Param = new InternalExpression( * with the function, without prompting the deployer. Empty string if not * defined. */ -export const storageBucket: Param = new InternalExpression( +export const storageBucket: Expression = new InternalExpression( "STORAGE_BUCKET", (env: NodeJS.ProcessEnv) => JSON.parse(env.FIREBASE_CONFIG)?.storageBucket || "" ); diff --git a/src/params/types.ts b/src/params/types.ts index fccdcbf86..7dd9b99bc 100644 --- a/src/params/types.ts +++ b/src/params/types.ts @@ -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( + thenElse( ifTrue: retT | Expression, ifFalse: retT | Expression ) { From f6a7eef8735b84b5558bc876b0cb97975e0a9421 Mon Sep 17 00:00:00 2001 From: Thomas Bouldin Date: Tue, 18 Oct 2022 09:05:40 -0400 Subject: [PATCH 2/2] Fixes & changelog --- CHANGELOG.md | 1 + spec/fixtures/sources/commonjs-params/index.js | 4 ++-- spec/params/params.spec.ts | 8 +++++--- src/params/index.ts | 8 ++++---- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e69de29bb..0062ea300 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -0,0 +1 @@ +Correct the function BooleanExpression#then to BooleanExpression#thenElse (#1268) diff --git a/spec/fixtures/sources/commonjs-params/index.js b/spec/fixtures/sources/commonjs-params/index.js index 5f982fcaa..2418d32a9 100644 --- a/spec/fixtures/sources/commonjs-params/index.js +++ b/spec/fixtures/sources/commonjs-params/index.js @@ -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: { diff --git a/spec/params/params.spec.ts b/spec/params/params.spec.ts index b4fa20785..fad4f00b5 100644 --- a/spec/params/params.spec.ts +++ b/spec/params/params.spec.ts @@ -284,9 +284,11 @@ describe("Params as CEL", () => { '{{ params.A != params.B ? "asdf" : "jkl;" }}' ); 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 }}" + 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 }}"); }); }); diff --git a/src/params/index.ts b/src/params/index.ts index 6e65fd43f..5fb0b9c35 100644 --- a/src/params/index.ts +++ b/src/params/index.ts @@ -68,7 +68,7 @@ export function clearParams() { * A builtin param that resolves to the default RTDB database URL associated * with the project, without prompting the deployer. Empty string if none exists. */ -export const databaseURL: Expression = new InternalExpression( +export const databaseURL: Param = new InternalExpression( "DATABASE_URL", (env: NodeJS.ProcessEnv) => JSON.parse(env.FIREBASE_CONFIG)?.databaseURL || "" ); @@ -76,7 +76,7 @@ export const databaseURL: Expression = new InternalExpression( * A builtin param that resolves to the Cloud project ID associated with * the project, without prompting the deployer. */ -export const projectID: Expression = new InternalExpression( +export const projectID: Param = new InternalExpression( "PROJECT_ID", (env: NodeJS.ProcessEnv) => JSON.parse(env.FIREBASE_CONFIG)?.projectId || "" ); @@ -84,7 +84,7 @@ export const projectID: Expression = new InternalExpression( * A builtin param that resolves to the Cloud project ID, without prompting * the deployer. */ -export const gcloudProject: Expression = new InternalExpression( +export const gcloudProject: Param = new InternalExpression( "GCLOUD_PROJECT", (env: NodeJS.ProcessEnv) => JSON.parse(env.FIREBASE_CONFIG)?.projectId || "" ); @@ -93,7 +93,7 @@ export const gcloudProject: Expression = new InternalExpression( * with the function, without prompting the deployer. Empty string if not * defined. */ -export const storageBucket: Expression = new InternalExpression( +export const storageBucket: Param = new InternalExpression( "STORAGE_BUCKET", (env: NodeJS.ProcessEnv) => JSON.parse(env.FIREBASE_CONFIG)?.storageBucket || "" );