Skip to content

Print a warning when Expression.value() is invoked during discovery #1257

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 39 additions & 19 deletions src/params/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import * as logger from "../logger";

/*
* A CEL expression which can be evaluated during function deployment, and
* resolved to a value of the generic type parameter: i.e, you can pass
Expand All @@ -28,6 +30,20 @@
export abstract class Expression<T extends string | number | boolean | string[]> {
/** Returns the Expression's runtime value, based on the CLI's resolution of params. */
value(): T {
if (process.env.FUNCTIONS_CONTROL_API === "true") {
logger.warn(
`${this.toString()}.value() invoked during function deployment, instead of during runtime.`
);
logger.warn(
`This is usually a mistake. In configs, use Params directly without calling .value().`
);
logger.warn(`example: { memory: memoryParam } not { memory: memoryParam.value() }`);
}
return this.runtimeValue();
}

/** @internal */
runtimeValue(): T {
throw new Error("Not implemented");
}

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

function valueOf<T extends string | number | boolean | string[]>(arg: T | Expression<T>): T {
return arg instanceof Expression ? arg.value() : arg;
return arg instanceof Expression ? arg.runtimeValue() : arg;
}
function refOf<T extends string | number | boolean | string[]>(arg: T | Expression<T>): string {
return arg instanceof Expression ? arg.toString() : quoteIfString(arg).toString();
Expand All @@ -69,9 +85,9 @@ export class TernaryExpression<
this.ifFalse = ifFalse;
}

/** Returns the Expression's runtime value, based on the CLI's resolution of params. */
value(): T {
return this.test.value() ? valueOf(this.ifTrue) : valueOf(this.ifFalse);
/** @internal */
runtimeValue(): T {
return this.test.runtimeValue() ? valueOf(this.ifTrue) : valueOf(this.ifFalse);
Comment on lines +89 to +90
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

woah we are renaming value to runtimeValue()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not from users' perspective. They'll still call value(), which will use the implementation in the base Expression class, which will do the discovery check and then call runtimeValue()

}

toString() {
Expand Down Expand Up @@ -101,9 +117,9 @@ export class CompareExpression<
this.rhs = rhs;
}

/** Returns the Expression's runtime value, based on the CLI's resolution of params. */
value(): boolean {
const left = this.lhs.value();
/** @internal */
runtimeValue(): boolean {
const left = this.lhs.runtimeValue();
const right = valueOf(this.rhs);
switch (this.cmp) {
case "==":
Expand Down Expand Up @@ -239,8 +255,8 @@ export abstract class Param<T extends string | number | boolean | string[]> exte
super();
}

/** Returns the Expression's runtime value, based on the CLI's resolution of params. */
value(): T {
/** @internal */
runtimeValue(): T {
throw new Error("Not implemented");
}

Expand Down Expand Up @@ -321,8 +337,8 @@ export class SecretParam {
this.name = name;
}

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

Expand All @@ -340,7 +356,8 @@ export class SecretParam {
* if present, or prompted for by the CLI if missing.
*/
export class StringParam extends Param<string> {
value(): string {
/** @internal */
runtimeValue(): string {
return process.env[this.name] || "";
}
}
Expand All @@ -357,7 +374,8 @@ export class InternalExpression extends Param<string> {
super(name);
}

value(): string {
/** @internal */
runtimeValue(): string {
return this.getter(process.env) || "";
}

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

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

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

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

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

/** Returns the Expression's runtime value, based on the CLI's resolution of params. */
value(): string[] {
/** @internal */
runtimeValue(): string[] {
throw new Error("Not implemented");
}

Expand Down