From 2e7ac472d3309f25ff69acd1c84ee9c6bf1208ad Mon Sep 17 00:00:00 2001 From: stew-r <32523712+stew-r@users.noreply.github.com> Date: Fri, 20 Jul 2018 15:10:51 -0700 Subject: [PATCH 1/2] Make docstrings consistent; add aliasing --- functions/node8/index.js | 52 +++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/functions/node8/index.js b/functions/node8/index.js index a43e5e5c22..e21b81da5a 100644 --- a/functions/node8/index.js +++ b/functions/node8/index.js @@ -18,8 +18,8 @@ * Background Cloud Function that only executes within a certain time * period after the triggering event to avoid infinite retry loops. * - * @param {object} event The Cloud Functions event payload. - * @param {function} context Information about the event. + * @param {object} data The event payload. + * @param {object} context The event metadata. */ exports.avoidInfiniteRetries = (data, context) => { const eventAge = Date.now() - Date.parse(context.timestamp); @@ -41,8 +41,8 @@ exports.avoidInfiniteRetries = (data, context) => { * Background Cloud Function that demonstrates * how to toggle retries using a promise * - * @param {object} data The Cloud Functions event payload. - * @param {function} context The Cloud Functions metadata. + * @param {object} data The event payload. + * @param {object} context The event metadata. */ exports.retryPromise = (data, context) => { const tryAgain = !!data.retry; @@ -72,13 +72,14 @@ exports.helloBackground = (data) => { * This function is exported by index.js, and executed when * the trigger topic receives a message. * - * @param {object} data The Cloud Functions event payload. - * @param {object} context The Cloud Functions metadata. + * @param {object} data The event payload. + * @param {object} context The event metadata. */ -exports.helloPubSub = (data) => { - const name = data.data ? Buffer.from(data.data, 'base64').toString() : 'World'; - - console.log(`Hello, ${name}!`); +exports.helloPubSub = (data, context) => { + const pubSubMessage = data; + const name = pubSubMessage.data ? Buffer.from(pubSubMessage.data, 'base64').toString() : 'World'; + + console.log(`Hello, ${name}!`); }; // [END functions_helloworld_pubsub_node8] @@ -86,17 +87,19 @@ exports.helloPubSub = (data) => { /** * Background Cloud Function to be triggered by Cloud Storage. * - * @param {object} data The Cloud Functions event payload. + * @param {object} data The event payload. + * @param {object} context The event metadata. */ -exports.helloGCS = (data) => { - if (data.resourceState === 'not_exists') { - console.log(`File ${data.name} deleted.`); - } else if (data.metageneration === '1') { +exports.helloGCS = (data, context) => { + const file = data; + if (file.resourceState === 'not_exists') { + console.log(`File ${file.name} deleted.`); + } else if (file.metageneration === '1') { // metageneration attribute is updated on metadata changes. // on create value is 1 - console.log(`File ${data.name} uploaded.`); + console.log(`File ${file.name} uploaded.`); } else { - console.log(`File ${data.name} metadata updated.`); + console.log(`File ${file.name} metadata updated.`); } }; // [END functions_helloworld_storage_node8] @@ -105,16 +108,17 @@ exports.helloGCS = (data) => { /** * Generic background Cloud Function to be triggered by Cloud Storage. * - * @param {object} data The Cloud Functions event payload. - * @param {object} context The Cloud Functions event metadata. + * @param {object} data The event payload. + * @param {object} context The event metadata. */ exports.helloGCSGeneric = (data, context) => { + const file = data; console.log(` Event ${context.eventId}`); console.log(` Event Type: ${context.eventType}`); - console.log(` Bucket: ${data.bucket}`); - console.log(` File: ${data.name}`); - console.log(` Metageneration: ${data.metageneration}`); - console.log(` Created: ${data.timeCreated}`); - console.log(` Updated: ${data.updated}`); + console.log(` Bucket: ${file.bucket}`); + console.log(` File: ${file.name}`); + console.log(` Metageneration: ${file.metageneration}`); + console.log(` Created: ${file.timeCreated}`); + console.log(` Updated: ${file.updated}`); }; // [END functions_helloworld_storage_generic_node8] From 43cf271ebbb50273763f2d8322f41f52d28fae8a Mon Sep 17 00:00:00 2001 From: stew-r <32523712+stew-r@users.noreply.github.com> Date: Mon, 23 Jul 2018 14:32:03 -0700 Subject: [PATCH 2/2] Fix spacing issues, add `context` parameter to helloBackground --- functions/node8/index.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/functions/node8/index.js b/functions/node8/index.js index e21b81da5a..44cae23d57 100644 --- a/functions/node8/index.js +++ b/functions/node8/index.js @@ -59,9 +59,10 @@ exports.retryPromise = (data, context) => { /** * Background Cloud Function. * - * @param {object} data Data passed to the Cloud Function. + * @param {object} data The event payload. + * @param {object} context The event metadata. */ -exports.helloBackground = (data) => { +exports.helloBackground = (data, context) => { return `Hello ${data.name || 'World'}!`; }; // [END functions_helloworld_background_node8] @@ -76,10 +77,10 @@ exports.helloBackground = (data) => { * @param {object} context The event metadata. */ exports.helloPubSub = (data, context) => { - const pubSubMessage = data; - const name = pubSubMessage.data ? Buffer.from(pubSubMessage.data, 'base64').toString() : 'World'; - - console.log(`Hello, ${name}!`); + const pubSubMessage = data; + const name = pubSubMessage.data ? Buffer.from(pubSubMessage.data, 'base64').toString() : 'World'; + + console.log(`Hello, ${name}!`); }; // [END functions_helloworld_pubsub_node8]