Last active
January 19, 2019 13:06
-
-
Save feloy/3a3f1ab5f40624dbceceb1f668ecc58d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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