1

In my Go Google Cloud Background Function I want to access params from my Firestore event.

https://cloud.google.com/functions/docs/writing/background#functions_background_parameters-go points out that two objects - ctx and Event are passed to the background function entrypoint. Event should be able to work with json.Unmarshal() of specific event that will be received.

With that knowledge, https://cloud.google.com/functions/docs/calling/cloud-firestore documents the event structure as

{
    "oldValue": { // Update and Delete operations only
        A Document object containing a pre-operation document snapshot
    },
    "updateMask": { // Update operations only
        A DocumentMask object that lists changed fields.
    },
    "value": {
        // A Document object containing a post-operation document snapshot
    }
}

but it also states that Wildcard matches are extracted from document paths and stored in event.params. You can define as many wildcards as you like to substitute explicit collection or document IDs.

so according to the above, I should probably add something like

Params map[string]string `json:"params"`

to my struct used for decoding the event. Unfortunately that does not work. Any hints on how to access these named params from firestore events in Golang background function would be very welcome.

1
  • Did you try this: Params map[string]interface{} `json:"params"` ? Commented Feb 13, 2020 at 12:56

1 Answer 1

0

You can get the event full path and then split it by '/documents' and split again to find the params values:

path := strings.Split(e.Value.Name, "/documents/")[1]
parts := strings.Split(path, "/")

The documentation is not very clear but it seems this is the only way to access params from golang.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.