Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
3ad62e2
wip: expr type
Berlioz Jul 19, 2022
5f8cccb
npm run prettier
Berlioz Jul 19, 2022
b7f879b
trigger fields
Berlioz Jul 20, 2022
48f2d9a
more detailed expr type
Berlioz Jul 21, 2022
bf6c420
Merge remote-tracking branch 'origin' into vf_expr_type
Berlioz Jul 21, 2022
b6d592e
Merge remote-tracking branch 'origin' into vf_expr_type
Berlioz Jul 28, 2022
e274fc7
wip: in-progress commit for providing params and param-valued fields …
Berlioz Aug 2, 2022
c00c2f7
wip: regexp validation
Berlioz Aug 2, 2022
66cffc9
resourceInput selectors
Berlioz Aug 9, 2022
156a21d
remove extraneous changes to triggerannotation which is no longer in use
Berlioz Aug 9, 2022
1089043
wip class-ification of expression type
Berlioz Aug 11, 2022
b0a9063
wip: split wire/manifest spec
Berlioz Aug 12, 2022
8942e5c
WIP: classification of Expressions
Berlioz Aug 15, 2022
3778deb
WIP: add value() semantics to params and expressions
Berlioz Aug 16, 2022
6379cc3
type system changes to conform with go/cf3-params-detailed
Berlioz Aug 17, 2022
27937eb
style fixes per review
Berlioz Aug 17, 2022
df07167
removed shotgun testing change
Berlioz Aug 18, 2022
aaf3dda
changes per review, most substantially making param inputs an optiona…
Berlioz Aug 25, 2022
d4bf770
merge experssions.ts with params.ts
Berlioz Aug 25, 2022
a114386
remove empty expressions.ts
Berlioz Aug 25, 2022
8c3447b
Merge remote-tracking branch 'origin' into vf_expr_type
Berlioz Aug 29, 2022
f63c895
nits per pr
Berlioz Aug 29, 2022
2c67693
typo
Berlioz Aug 29, 2022
251aa43
format fix
Berlioz Aug 29, 2022
47e2330
timezone -> timeZone, and fix SelectInput
Berlioz Aug 30, 2022
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/runtime/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ export interface ManifestEndpoint {
entryPoint?: string;
region?: string[];
platform?: string;
availableMemoryMb?: number;
maxInstances?: number;
minInstances?: number;
concurrency?: number;
availableMemoryMb?: number | string;
maxInstances?: number | string;
minInstances?: number | string;
concurrency?: number | string;
serviceAccountEmail?: string;
timeoutSeconds?: number;
timeoutSeconds?: number | string;
cpu?: number | 'gcf_gen1';
vpc?: {
connector: string;
Expand Down
11 changes: 6 additions & 5 deletions src/v2/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@

import { Change } from '../common/change';
import { ManifestEndpoint } from '../runtime/manifest';
import { Expression } from './expressions';

export { Change };

/** @internal */
export interface TriggerAnnotation {
platform?: string;
concurrency?: number;
minInstances?: number;
maxInstances?: number;
availableMemoryMb?: number;
concurrency?: number | Expression<number>;
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't use TriggerAnnotation anymore, so you can revert these changes

minInstances?: number | Expression<number>;
maxInstances?: number | Expression<number>;
availableMemoryMb?: number | Expression<number>;
eventTrigger?: {
eventType: string;
resource: string;
Expand All @@ -48,7 +49,7 @@ export interface TriggerAnnotation {
};
labels?: { [key: string]: string };
regions?: string[];
timeout?: string;
timeout?: string | Expression<number>;
vpcConnector?: string;
vpcConnectorEgressSettings?: string;
serviceAccountEmail?: string;
Expand Down
24 changes: 24 additions & 0 deletions src/v2/expressions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* A CEL expression which can be evaulated during function deployment, and
* resolved to a value of the generic type parameter: i.e, you can pass
* an Expression<number> as the value of an option that normally accepts numbers.
Copy link
Member

Choose a reason for hiding this comment

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

expressions are only used in the context of params. They should be in the same file. You can have a separate file for expressions but v2/params should export everything you need to handle expressions.

*/
export type Expression<T extends string | number | boolean> = string; // eslint-disable-line
Copy link
Member

Choose a reason for hiding this comment

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

In the Functions codebase an Expression is not the same thing as T. Expression is a base class of Param but can (eventually) do a few extra things:

  1. BooleanExpressions should subclass Expression and adds the ternary operator to create an Expression
  2. All (non-boolean?) expressions should support an equality condition to make a BooleanExpression

export type Field<T extends string | number | boolean> = T | Expression<T> | null;

/**
* Casts an Expression to its literal string value, for use by __getSpec()
* @internal
*/
export function ExprString<Expression>(arg: Expression): string {
return arg as unknown as string;
}

/**
* Sanity check on whether a CEL expression is actually evaluatable
* @internal
*/
export function IsValid<Expression>(arg: Expression): boolean {
return true;
}
2 changes: 2 additions & 0 deletions src/v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@ export {
export { Change } from '../common/change';

export { CloudFunction, CloudEvent } from './core';

export { Expression } from './expressions';
23 changes: 14 additions & 9 deletions src/v2/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { TriggerAnnotation } from './core';
import { declaredParams } from './params';
import { ParamSpec } from './params/types';
import { HttpsOptions } from './providers/https';
import { Expression, Field, ExprString } from './expressions';

/**
* List of all regions supported by Cloud Functions v2
Expand Down Expand Up @@ -76,6 +77,10 @@ const MemoryOptionToMB: Record<MemoryOption, number> = {
'32GiB': 32768,
};

function isMemoryOption(arg: MemoryOption | Expression<number>): arg is MemoryOption {
return (arg in MemoryOptionToMB);
}

/**
* List of available options for VpcConnectorEgressSettings.
*/
Expand Down Expand Up @@ -104,7 +109,7 @@ export interface GlobalOptions {
* Amount of memory to allocate to a function.
* A value of null restores the defaults of 256MB.
*/
memory?: MemoryOption | null;
memory?: MemoryOption | Expression<number> | null;
Copy link
Contributor

Choose a reason for hiding this comment

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

Aren't memoryOptions string?

export type MemoryOption =

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah I see. memory can either be a string or a number.

I think this is a bad footgun. For example, I would write something like this:

{ memory: "{{ isProd ? '1GB' : '256MB' }}" } 

And this totally wouldn't work no? It would have to be instead:

{ memory: "{{ isProd ? 1024 : 256 }}" } 

or something which feels surprising.


/**
* Timeout for the function in sections, possible values are 0 to 540.
Expand All @@ -116,21 +121,21 @@ export interface GlobalOptions {
* maximum timeout of 36,00s (1 hour). Task queue functions have a maximum
* timeout of 1,800s (30 minutes)
*/
timeoutSeconds?: number | null;
timeoutSeconds?: Field<number>;

/**
* Min number of actual instances to be running at a given time.
* Instances will be billed for memory allocation and 10% of CPU allocation
* while idle.
* A value of null restores the default min instances.
*/
minInstances?: number | null;
minInstances?: Field<number>;

/**
* Max number of instances to be running in parallel.
* A value of null restores the default max instances.
*/
maxInstances?: number | null;
maxInstances?: Field<number>;

/**
* Number of requests a function can serve at once.
Expand All @@ -139,7 +144,7 @@ export interface GlobalOptions {
* Concurrency cannot be set to any value other than 1 if `cpu` is less than 1.
* The maximum value for concurrency is 1,000.
*/
concurrency?: number | null;
concurrency?: Field<number>;

/**
* Fractional number of CPUs to allocate to a function.
Expand Down Expand Up @@ -246,8 +251,8 @@ export function optionsToTriggerAnnotations(
opts,
'availableMemoryMb',
'memory',
(mem: MemoryOption) => {
return MemoryOptionToMB[mem];
(mem: MemoryOption | Expression<number>): number | string => {
return isMemoryOption(mem) ? MemoryOptionToMB[mem] : ExprString(mem);
}
);
convertIfPresent(annotation, opts, 'regions', 'region', (region) => {
Expand Down Expand Up @@ -308,8 +313,8 @@ export function optionsToEndpoint(
convertIfPresent(vpc, opts, 'egressSettings', 'vpcConnectorEgressSettings');
endpoint.vpc = vpc;
}
convertIfPresent(endpoint, opts, 'availableMemoryMb', 'memory', (mem) => {
return MemoryOptionToMB[mem];
convertIfPresent(endpoint, opts, 'availableMemoryMb', 'memory', (mem: MemoryOption | Expression<number>): number | string => {
return isMemoryOption(mem) ? MemoryOptionToMB[mem] : ExprString(mem);
});
convertIfPresent(endpoint, opts, 'region', 'region', (region) => {
if (typeof region === 'string') {
Expand Down
11 changes: 6 additions & 5 deletions src/v2/providers/alerts/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import { ManifestEndpoint } from '../../../runtime/manifest';
import { CloudEvent, CloudFunction } from '../../core';
import * as options from '../../options';
import { Expression, Field } from '../../expressions';

/**
* The CloudEvent data emitted by Firebase Alerts.
Expand Down Expand Up @@ -89,7 +90,7 @@ export interface FirebaseAlertOptions extends options.EventHandlerOptions {
* Amount of memory to allocate to a function.
* A value of null restores the defaults of 256MB.
*/
memory?: options.MemoryOption | null;
memory?: options.MemoryOption | Expression<number> | null;

/**
* Timeout for the function in sections, possible values are 0 to 540.
Expand All @@ -101,21 +102,21 @@ export interface FirebaseAlertOptions extends options.EventHandlerOptions {
* maximum timeout of 36,00s (1 hour). Task queue functions have a maximum
* timeout of 1,800s (30 minutes)
*/
timeoutSeconds?: number | null;
timeoutSeconds?: Field<number>;

/**
* Min number of actual instances to be running at a given time.
* Instances will be billed for memory allocation and 10% of CPU allocation
* while idle.
* A value of null restores the default min instances.
*/
minInstances?: number | null;
minInstances?: Field<number>;

/**
* Max number of instances to be running in parallel.
* A value of null restores the default max instances.
*/
maxInstances?: number | null;
maxInstances?: Field<number>;

/**
* Number of requests a function can serve at once.
Expand All @@ -124,7 +125,7 @@ export interface FirebaseAlertOptions extends options.EventHandlerOptions {
* Concurrency cannot be set to any value other than 1 if `cpu` is less than 1.
* The maximum value for concurrency is 1,000.
*/
concurrency?: number | null;
concurrency?: Field<number>;

/**
* Fractional number of CPUs to allocate to a function.
Expand Down
11 changes: 6 additions & 5 deletions src/v2/providers/alerts/appDistribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import { CloudEvent, CloudFunction } from '../../core';
import * as options from '../../options';
import { FirebaseAlertData, getEndpointAnnotation } from './alerts';
import { Expression, Field } from '../../expressions';

/**
* The internal payload object for adding a new tester device to app distribution.
Expand Down Expand Up @@ -76,7 +77,7 @@ export interface AppDistributionOptions extends options.EventHandlerOptions {
* Amount of memory to allocate to a function.
* A value of null restores the defaults of 256MB.
*/
memory?: options.MemoryOption | null;
memory?: options.MemoryOption | Expression<number> | null;

/**
* Timeout for the function in sections, possible values are 0 to 540.
Expand All @@ -88,21 +89,21 @@ export interface AppDistributionOptions extends options.EventHandlerOptions {
* maximum timeout of 36,00s (1 hour). Task queue functions have a maximum
* timeout of 1,800s (30 minutes)
*/
timeoutSeconds?: number | null;
timeoutSeconds?: Field<number>;

/**
* Min number of actual instances to be running at a given time.
* Instances will be billed for memory allocation and 10% of CPU allocation
* while idle.
* A value of null restores the default min instances.
*/
minInstances?: number | null;
minInstances?: Field<number>;

/**
* Max number of instances to be running in parallel.
* A value of null restores the default max instances.
*/
maxInstances?: number | null;
maxInstances?: Field<number>;

/**
* Number of requests a function can serve at once.
Expand All @@ -111,7 +112,7 @@ export interface AppDistributionOptions extends options.EventHandlerOptions {
* Concurrency cannot be set to any value other than 1 if `cpu` is less than 1.
* The maximum value for concurrency is 1,000.
*/
concurrency?: number | null;
concurrency?: Field<number>;

/**
* Fractional number of CPUs to allocate to a function.
Expand Down
11 changes: 6 additions & 5 deletions src/v2/providers/alerts/crashlytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import { FirebaseAlertData, getEndpointAnnotation } from '.';
import { CloudEvent, CloudFunction } from '../../core';
import * as options from '../../options';
import { Expression, Field } from '../../expressions';

/** Generic Crashlytics issue interface */
export interface Issue {
Expand Down Expand Up @@ -182,7 +183,7 @@ export interface CrashlyticsOptions extends options.EventHandlerOptions {
* Amount of memory to allocate to a function.
* A value of null restores the defaults of 256MB.
*/
memory?: options.MemoryOption | null;
memory?: options.MemoryOption | Expression<number> | null;

/**
* Timeout for the function in sections, possible values are 0 to 540.
Expand All @@ -194,21 +195,21 @@ export interface CrashlyticsOptions extends options.EventHandlerOptions {
* maximum timeout of 36,00s (1 hour). Task queue functions have a maximum
* timeout of 1,800s (30 minutes)
*/
timeoutSeconds?: number | null;
timeoutSeconds?: Field<number>;

/**
* Min number of actual instances to be running at a given time.
* Instances will be billed for memory allocation and 10% of CPU allocation
* while idle.
* A value of null restores the default min instances.
*/
minInstances?: number | null;
minInstances?: Field<number>;

/**
* Max number of instances to be running in parallel.
* A value of null restores the default max instances.
*/
maxInstances?: number | null;
maxInstances?: Field<number>;

/**
* Number of requests a function can serve at once.
Expand All @@ -217,7 +218,7 @@ export interface CrashlyticsOptions extends options.EventHandlerOptions {
* Concurrency cannot be set to any value other than 1 if `cpu` is less than 1.
* The maximum value for concurrency is 1,000.
*/
concurrency?: number | null;
concurrency?: Field<number>;

/**
* Fractional number of CPUs to allocate to a function.
Expand Down
11 changes: 6 additions & 5 deletions src/v2/providers/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { PathPattern } from '../../utilities/path-pattern';
import { applyChange } from '../../utils';
import { CloudEvent, CloudFunction } from '../core';
import * as options from '../options';
import { Expression, Field } from '../expressions';

export { DataSnapshot };

Expand Down Expand Up @@ -102,7 +103,7 @@ export interface ReferenceOptions extends options.EventHandlerOptions {
* Amount of memory to allocate to a function.
* A value of null restores the defaults of 256MB.
*/
memory?: options.MemoryOption | null;
memory?: options.MemoryOption | Expression<number> | null;

/**
* Timeout for the function in sections, possible values are 0 to 540.
Expand All @@ -114,21 +115,21 @@ export interface ReferenceOptions extends options.EventHandlerOptions {
* maximum timeout of 36,00s (1 hour). Task queue functions have a maximum
* timeout of 1,800s (30 minutes)
*/
timeoutSeconds?: number | null;
timeoutSeconds?: Field<number>;

/**
* Min number of actual instances to be running at a given time.
* Instances will be billed for memory allocation and 10% of CPU allocation
* while idle.
* A value of null restores the default min instances.
*/
minInstances?: number | null;
minInstances?: Field<number>;

/**
* Max number of instances to be running in parallel.
* A value of null restores the default max instances.
*/
maxInstances?: number | null;
maxInstances?: Field<number>;

/**
* Number of requests a function can serve at once.
Expand All @@ -137,7 +138,7 @@ export interface ReferenceOptions extends options.EventHandlerOptions {
* Concurrency cannot be set to any value other than 1 if `cpu` is less than 1.
* The maximum value for concurrency is 1,000.
*/
concurrency?: number | null;
concurrency?: Field<number>;

/**
* Fractional number of CPUs to allocate to a function.
Expand Down
Loading