Skip to content

Instantly share code, notes, and snippets.

@feloy
Last active January 19, 2019 13:06
Show Gist options
  • Save feloy/3a3f1ab5f40624dbceceb1f668ecc58d to your computer and use it in GitHub Desktop.
Save feloy/3a3f1ab5f40624dbceceb1f668ecc58d 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"`
}
// 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)
// Get data from Score
score, err := NewFromFirestoreValue(e.Value)
if err != nil {
return err
}
log.Printf("%+v", score)
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment