Skip to content

Commit 9941668

Browse files
authored
Print a warning when Expression.value() is invoked during discovery (#1257)
1 parent b0acea3 commit 9941668

File tree

1 file changed

+39
-19
lines changed

1 file changed

+39
-19
lines changed

src/params/types.ts

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
2222

23+
import * as logger from "../logger";
24+
2325
/*
2426
* A CEL expression which can be evaluated during function deployment, and
2527
* resolved to a value of the generic type parameter: i.e, you can pass
@@ -28,6 +30,20 @@
2830
export abstract class Expression<T extends string | number | boolean | string[]> {
2931
/** Returns the Expression's runtime value, based on the CLI's resolution of params. */
3032
value(): T {
33+
if (process.env.FUNCTIONS_CONTROL_API === "true") {
34+
logger.warn(
35+
`${this.toString()}.value() invoked during function deployment, instead of during runtime.`
36+
);
37+
logger.warn(
38+
`This is usually a mistake. In configs, use Params directly without calling .value().`
39+
);
40+
logger.warn(`example: { memory: memoryParam } not { memory: memoryParam.value() }`);
41+
}
42+
return this.runtimeValue();
43+
}
44+
45+
/** @internal */
46+
runtimeValue(): T {
3147
throw new Error("Not implemented");
3248
}
3349

@@ -47,7 +63,7 @@ function quoteIfString<T extends string | number | boolean | string[]>(literal:
4763
}
4864

4965
function valueOf<T extends string | number | boolean | string[]>(arg: T | Expression<T>): T {
50-
return arg instanceof Expression ? arg.value() : arg;
66+
return arg instanceof Expression ? arg.runtimeValue() : arg;
5167
}
5268
function refOf<T extends string | number | boolean | string[]>(arg: T | Expression<T>): string {
5369
return arg instanceof Expression ? arg.toString() : quoteIfString(arg).toString();
@@ -69,9 +85,9 @@ export class TernaryExpression<
6985
this.ifFalse = ifFalse;
7086
}
7187

72-
/** Returns the Expression's runtime value, based on the CLI's resolution of params. */
73-
value(): T {
74-
return this.test.value() ? valueOf(this.ifTrue) : valueOf(this.ifFalse);
88+
/** @internal */
89+
runtimeValue(): T {
90+
return this.test.runtimeValue() ? valueOf(this.ifTrue) : valueOf(this.ifFalse);
7591
}
7692

7793
toString() {
@@ -101,9 +117,9 @@ export class CompareExpression<
101117
this.rhs = rhs;
102118
}
103119

104-
/** Returns the Expression's runtime value, based on the CLI's resolution of params. */
105-
value(): boolean {
106-
const left = this.lhs.value();
120+
/** @internal */
121+
runtimeValue(): boolean {
122+
const left = this.lhs.runtimeValue();
107123
const right = valueOf(this.rhs);
108124
switch (this.cmp) {
109125
case "==":
@@ -239,8 +255,8 @@ export abstract class Param<T extends string | number | boolean | string[]> exte
239255
super();
240256
}
241257

242-
/** Returns the Expression's runtime value, based on the CLI's resolution of params. */
243-
value(): T {
258+
/** @internal */
259+
runtimeValue(): T {
244260
throw new Error("Not implemented");
245261
}
246262

@@ -321,8 +337,8 @@ export class SecretParam {
321337
this.name = name;
322338
}
323339

324-
/** Returns the Expression's runtime value, based on the latest version of the Secret in Cloud Secret Manager */
325-
value(): string {
340+
/** @internal */
341+
runtimeValue(): string {
326342
return process.env[this.name] || "";
327343
}
328344

@@ -340,7 +356,8 @@ export class SecretParam {
340356
* if present, or prompted for by the CLI if missing.
341357
*/
342358
export class StringParam extends Param<string> {
343-
value(): string {
359+
/** @internal */
360+
runtimeValue(): string {
344361
return process.env[this.name] || "";
345362
}
346363
}
@@ -357,7 +374,8 @@ export class InternalExpression extends Param<string> {
357374
super(name);
358375
}
359376

360-
value(): string {
377+
/** @internal */
378+
runtimeValue(): string {
361379
return this.getter(process.env) || "";
362380
}
363381

@@ -373,7 +391,8 @@ export class InternalExpression extends Param<string> {
373391
export class IntParam extends Param<number> {
374392
static type: ParamValueType = "int";
375393

376-
value(): number {
394+
/** @internal */
395+
runtimeValue(): number {
377396
return parseInt(process.env[this.name] || "0", 10) || 0;
378397
}
379398
}
@@ -385,7 +404,8 @@ export class IntParam extends Param<number> {
385404
export class FloatParam extends Param<number> {
386405
static type: ParamValueType = "float";
387406

388-
value(): number {
407+
/** @internal */
408+
runtimeValue(): number {
389409
return parseFloat(process.env[this.name] || "0") || 0;
390410
}
391411
}
@@ -397,8 +417,8 @@ export class FloatParam extends Param<number> {
397417
export class BooleanParam extends Param<boolean> {
398418
static type: ParamValueType = "boolean";
399419

400-
/** Returns the Expression's runtime value, based on the CLI's resolution of params. */
401-
value(): boolean {
420+
/** @internal */
421+
runtimeValue(): boolean {
402422
return !!process.env[this.name] && process.env[this.name] === "true";
403423
}
404424

@@ -411,8 +431,8 @@ export class BooleanParam extends Param<boolean> {
411431
export class ListParam extends Param<string[]> {
412432
static type: ParamValueType = "list";
413433

414-
/** Returns the Expression's runtime value, based on the CLI's resolution of params. */
415-
value(): string[] {
434+
/** @internal */
435+
runtimeValue(): string[] {
416436
throw new Error("Not implemented");
417437
}
418438

0 commit comments

Comments
 (0)