Giter VIP home page Giter VIP logo

Comments (6)

iangudger avatar iangudger commented on June 6, 2024

@ssttevee, can you take a look?

from cloud-functions-go.

nelsonpina avatar nelsonpina commented on June 6, 2024

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.

ssttevee avatar ssttevee commented on June 6, 2024

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.

ssttevee avatar ssttevee commented on June 6, 2024

@iangudger, should the documentation be consider truth or should this be accounted for in the events package?

from cloud-functions-go.

nelsonpina avatar nelsonpina commented on June 6, 2024

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.

iangudger avatar iangudger commented on June 6, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.