Skip to content

Instantly share code, notes, and snippets.

@feloy
Created January 19, 2019 11:06
Show Gist options
  • Save feloy/4928c663cdfddf2a470679d9df122fd6 to your computer and use it in GitHub Desktop.
Save feloy/4928c663cdfddf2a470679d9df122fd6 to your computer and use it in GitHub Desktop.
// Package p contains a Firestore Cloud Function.
package p
import (
"context"
"fmt"
"log"
"cloud.google.com/go/functions/metadata"
)
// FirestoreEvent is the payload of a Firestore event.
// Please refer to the docs for additional information
// regarding Firestore events.
type FirestoreEvent struct {
OldValue FirestoreValue `json:"oldValue"`
Value FirestoreValue `json:"value"`
}
// FirestoreValue holds Firestore fields.
type FirestoreValue struct {
Fields interface{} `json:"fields"`
}
// OnNewScore is triggered by a change to a Firestore document.
func OnNewScore(ctx context.Context, e FirestoreEvent) error {
meta, err := metadata.FromContext(ctx)
if err != nil {
return fmt.Errorf("metadata.FromContext: %v", err)
}
log.Printf("Function triggered by change to: %v", meta.Resource)
log.Printf("%v", e)
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment