Add auto-wrapping of data for pubsub triggers (code included)
Description
When the trigger type is subsub, let's wrap the supplied JSON with the pubsub message structure. This will allow us to easily test functions with pubsub triggers. Should be as easy as adding the following code to src/service/rest-service.js:
if (new RegExp('cloud.pubsub').test(event.eventType)) {
// wrap input JSON in pubsub message structure
const json = event.resource.data;
event.resource = {
data: {
data: Buffer.from(JSON.stringify(json)).toString('base64')
}
}
}
Steps to reproduce show how we're currently sending JSON into emulated functions with pubsub triggers. Works perfectly. If this sounds reasonable, I'm happy to add tests and make a pull request.
Steps to reproduce
- Deploy a function with --trigger-topic set
- Call function with JSON file (e.g. sampleData.json) encoded:
functions call functionName --data="{ \"data\": \"$(echo $(cat sampleData.json) | base64 -w 0)\" }"
- Handling of pubsub message completes correctly
Thanks!!!
@briangranatir
The Emulator can't assume that the user is providing JSON as the message payload. And, it doesn't know if the user is providing something that has already been base64-encoded. For the call CLI command and its --data flag, the Emulator is going to continue mirroring the behavior of the gcloud beta functions call.
However, if you want to put together a different proposal that adds say, a non-standard Emulator-only --json flag to the call command, which does what you propose, then I would be open to that.
@jmdobry
Thanks for the quick reply, Jason. Makes perfect sense. I'll look to make a pull request for the --json flag. Great idea.
Also, why I have you... great work on the emulator. I'm using the heck out of this thing! :) Thank you
Thanks for the quick reply, Jason
Wasn't actually a quick reply, sorry about that.
I'm glad you find it useful!