Comments (6)
@ssttevee, can you take a look?
from cloud-functions-go.
I dug a bit deeper and found out the following:
the error with decoding the event can be avoided if instead of trying to decode the event
we decode event.Data
.
@ events.go / line: 152
var event Event
if err := json.NewDecoder(r.Body).Decode(&event.Data); err != nil {
nodego.ErrorLogger.Print("Failed to decode event: ", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
the event object seems still to be interpreted differently locally and in the cloud.
With the above fix, if my POST body looks like this:
{
"data":"dGVzdA=="
}
the output of the Your message:
infoLogger in the main
bellow is:
func main() {
flag.Parse()
http.HandleFunc(nodego.PubSubTrigger, events.Handler(func(event *events.Event) error {
nodego.InfoLogger.Printf("PubSub triggered Go function!")
nodego.InfoLogger.Printf("Your message: %s", event.Data)
msg, err := event.PubSubMessage()
if err != nil {
return err
}
nodego.InfoLogger.Printf("Your message: %s", msg.Data)
return nil
}))
nodego.TakeOver()
}
locally
Your message: {
"data":"dGVzdA=="
}
deployed cloud function
Your message: {
"data": {
"data": "dGVzdA=="
},
"context": {
"eventId": "3b8ddd8e-82e1-4c48-aec6-6519bcf7c98a",
"resource": {
"service": "pubsub.googleapis.com",
"name": "projects/infrasnukture/topics/apexes-telemetry"
},
"eventType": "google.pubsub.topic.publish",
"timestamp": "2018-07-27T11:05:26.462Z"
}
}
Which then fails to retrieve the PubSub message at msg, err := event.PubSubMessage()
.
any idea why we have different behaviour between local and deployed envs?
from cloud-functions-go.
There seems to be a disparity between what's documented on google's website and what is actually being sent: https://cloud.google.com/functions/docs/writing/background#function_parameters. It says that the event.context.resource
value is supposed to be a string, but the resource
value in the output you've pasted above is not quite a string.
You might also run into another issue if you're using the testing tab of the deployed function page because google likes wrapping your test message with some context information. When testing locally, if your actual message is:
{
"data": "dGVzdA=="
}
you'll have to wrap it up like this:
{
"data": {
"data": "dGVzdA=="
}
}
to simulate the context wrapper.
from cloud-functions-go.
@iangudger, should the documentation be consider truth or should this be accounted for in the events
package?
from cloud-functions-go.
FYI, these are the changes I have made to get it going for PubSub events:
@ events.go / line: 90
func (e *Event) PubSubMessage() (*PubSubMessage, error) {
type EventData struct {
Data json.RawMessage `json:"data"`
}
var eventData EventData
if err := json.Unmarshal(e.Data, &eventData); err != nil {
return nil, err
}
var msg pubsub.PubsubMessage
if err := json.Unmarshal(eventData.Data, &msg); err != nil {
return nil, err
}
....
@ events.go / line: 152
if err := json.NewDecoder(r.Body).Decode(&event.Data); err != nil {
....
I had to decode events.Data
to get it to pass HTTP body decoding, and then I added a new structure to retrieve "data:"
from the event payload to be able to unmarshal the event.Data
.
@ssttevee the documentation regarding the event
object is indeed a bit confusing and it doesn't seem consistent.
Note: the changes I made may break the event handling for different event types, (HTTP, etc..)
from cloud-functions-go.
@ssttevee It doesn't surprise me too much that the documentation isn't fully correct. Feel free to send a PR to fix this case.
from cloud-functions-go.
Related Issues (20)
- about function-go HOT 6
- How to log to GCP Stackdriver? HOT 18
- currently only allow one main.go HOT 3
- cross projects permissions HOT 6
- there are any update? HOT 6
- Function always dies when issuing HTTP(S)-Requests with nodego.OverrideLogger() HOT 3
- Deployment failure: Operation interrupted. HOT 24
- Error: could not handle the request HOT 10
- NodeJS, PassportJS or JWT in Google Cloud Functions HOT 1
- Official Golang support plans? HOT 6
- Can't deploy function to Google Cloud Platform HOT 6
- How to deploy function to local emulator? HOT 2
- Can't get this package. HOT 3
- How to use with a web server framework HOT 4
- Deployment issues HOT 9
- Official Go support is in public beta
- Why vendor folder is ignored (go project) HOT 1
- Deployment failure without invisibility! HOT 2
- Use Real Cloud Functions Go Runtime, Not Fake One HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cloud-functions-go.