Skip to content

Commit d592847

Browse files
authored
Insert Expression and Field types into GlobalOptions and inheriting types (#1171)
This will plumb Param definitions and Expression types in Options to the CLI.
1 parent 7057c4d commit d592847

File tree

22 files changed

+506
-482
lines changed

22 files changed

+506
-482
lines changed

package-lock.json

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
const functions = require("../../../../src/index");
22
const functionsv2 = require("../../../../src/v2/index");
3-
const { defineString } = require("../../../../src/v2/params");
3+
const params = require("../../../../src/v2/params");
44

5-
defineString("FOO");
5+
params.defineString("BORING");
6+
params.defineString("FOO", {input: {text: {validationRegex:"\w+"}}});
7+
params.defineString("BAR", {default: "{{ params.FOO }}", label: "asdf"});
8+
params.defineString("BAZ", {input: {select: {options: [{value: "a"}, {value: "b"}]}}});
9+
10+
params.defineInt("AN_INT", {default: 22});
11+
params.defineInt("ANOTHER_INT", {input: {select: {options:[{label: "a", value: -2}, {"label": "b", value: 2}]}}});
12+
13+
params.defineSecret("SUPER_SECRET_FLAG")
614

715
exports.v1http = functions.https.onRequest((req, resp) => {
816
resp.status(200).send("PASS");

spec/runtime/loader.spec.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
ManifestRequiredAPI,
99
ManifestStack,
1010
} from '../../src/runtime/manifest';
11+
import { clearParams } from '../../src/v2/params';
1112

1213
describe('extractStack', () => {
1314
const httpFn = functions.https.onRequest(() => {});
@@ -104,6 +105,7 @@ describe('extractStack', () => {
104105

105106
afterEach(() => {
106107
process.env.GCLOUD_PROJECT = prev;
108+
clearParams();
107109
});
108110

109111
it('extracts stack from a simple module', () => {
@@ -313,7 +315,44 @@ describe('loadStack', () => {
313315
{
314316
name: 'has params',
315317
modulePath: './spec/fixtures/sources/commonjs-params',
316-
expected: { ...expected, params: [{ name: 'FOO', type: 'string' }] },
318+
expected: {
319+
...expected,
320+
params: [
321+
{ name: 'BORING', type: 'string' },
322+
{
323+
name: 'FOO',
324+
type: 'string',
325+
input: { text: { validationRegex: 'w+' } },
326+
},
327+
{
328+
name: 'BAR',
329+
type: 'string',
330+
default: '{{ params.FOO }}',
331+
label: 'asdf',
332+
},
333+
{
334+
name: 'BAZ',
335+
type: 'string',
336+
input: {
337+
select: { options: [{ value: 'a' }, { value: 'b' }] },
338+
},
339+
},
340+
{ name: 'AN_INT', type: 'int', default: 22 },
341+
{
342+
name: 'ANOTHER_INT',
343+
type: 'int',
344+
input: {
345+
select: {
346+
options: [
347+
{ label: 'a', value: -2 },
348+
{ label: 'b', value: 2 },
349+
],
350+
},
351+
},
352+
},
353+
{ name: 'SUPER_SECRET_FLAG', type: 'secret' },
354+
],
355+
},
317356
},
318357
];
319358

spec/runtime/manifest.spec.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { stackToWire, ManifestStack } from '../../src/runtime/manifest';
2+
import { expect } from 'chai';
3+
import * as params from '../../src/v2/params';
4+
5+
describe('stackToWire', () => {
6+
afterEach(() => {
7+
params.clearParams();
8+
});
9+
10+
it('converts Expression types in endpoint options to CEL', () => {
11+
const intParam = params.defineInt('foo', { default: 11 });
12+
const stringParam = params.defineString('bar', {
13+
default: 'America/Los_Angeles',
14+
});
15+
16+
const stack: ManifestStack = {
17+
endpoints: {
18+
v2http: {
19+
platform: 'gcfv2',
20+
entryPoint: 'v2http',
21+
labels: {},
22+
httpsTrigger: {},
23+
concurrency: intParam,
24+
maxInstances: intParam.equals(24).then(-1, 1),
25+
},
26+
v2schedule: {
27+
platform: 'gcfv2',
28+
entryPoint: 'v2callable',
29+
labels: {},
30+
scheduleTrigger: {
31+
schedule: stringParam
32+
.equals('America/Mexico_City')
33+
.then('mexico', 'usa'),
34+
timeZone: stringParam,
35+
},
36+
},
37+
},
38+
requiredAPIs: [],
39+
specVersion: 'v1alpha1',
40+
};
41+
const expected = {
42+
endpoints: {
43+
v2http: {
44+
platform: 'gcfv2',
45+
entryPoint: 'v2http',
46+
labels: {},
47+
httpsTrigger: {},
48+
concurrency: '{{ params.foo }}',
49+
maxInstances: '{{ params.foo == 24 ? -1 : 1 }}',
50+
},
51+
v2schedule: {
52+
platform: 'gcfv2',
53+
entryPoint: 'v2callable',
54+
labels: {},
55+
scheduleTrigger: {
56+
schedule:
57+
'{{ params.bar == "America/Mexico_City" ? "mexico" : "usa" }}',
58+
timeZone: '{{ params.bar }}',
59+
},
60+
},
61+
},
62+
requiredAPIs: [],
63+
specVersion: 'v1alpha1',
64+
};
65+
expect(stackToWire(stack)).to.deep.equal(expected);
66+
});
67+
});

0 commit comments

Comments
 (0)