fsevent

package module
v0.0.0-...-52de662 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2021 License: MIT Imports: 7 Imported by: 0

README

fsevent

Eazy handling Cloud Firestore events on Google Cloud Functions Go Runtime.

Description

Cloud Functions events triggered by Cloud Firestore are somewhat complex to handle.
GCP Documentation: Google Cloud Firestore Triggers
Receive fsevent as an event and you can use the same type definition for Cloud Firestore.

features
  • get event types - TypeCreate TypeUpdate or TypeDelete
  • reflect value in event to struct you defined for firestore

Installation

Minimum Go version: Go 1.11

Use go get to install and update:

$ go get -u github.com/ya5u/fsevent

Usage

Example Cloud Functions Code is below

package handler

import (
  "context"
  "time"

  "github.com/ya5u/fsevent"
)

// FsData is defined type for firestore as you like
type FsData struct {
  Name     string     `firestore:"name"`
  Age      int64      `firestore:"age"`
  Birthday *time.Time `firestore:"birthday"`
}

// Handler is the entry point of Cloud Functions triggered by firestore event
func Handler(ctx context.Context, e fsevent.FirestoreEvent) error {
  // get event type
  eventType := e.Type()

  // reflect updated value to struct you defined
  var updated FsData
  err := e.Value.DataTo(&updated)
  if err != nil {
    // error handling
  }

  // reflect old value to struct you defined
  var old FsData
  err = e.OldValue.DataTo(&old)
  if err != nil {
    // error handling
  }
}

TODO

  • support primitive pointer types
  • support Arrays
  • support Maps
  • support References
  • implement method of Value type like firestore.DocumentSnapshot.Data

Licence

MIT

Author

ya5u

Documentation

Index

Constants

View Source
const (
	TypeCreate = "CREATE" // on a document created
	TypeUpdate = "UPDATE" // on a document updated
	TypeDelete = "DELETE" // on a document deleted
)

event types

Variables

This section is empty.

Functions

This section is empty.

Types

type FirestoreEvent

type FirestoreEvent struct {
	OldValue   *Value `json:"oldValue"`
	Value      *Value `json:"value"`
	UpdateMask *struct {
		FieldPaths []string `json:"fieldPaths"`
	} `json:"updateMask"`
}

FirestoreEvent is the payload of a Firestore event.

func (*FirestoreEvent) Type

func (e *FirestoreEvent) Type() string

Type returns event type, which is one of following constants:

  • TypeCreate
  • TypeUpdate
  • TypeDelete

type Value

type Value struct {
	CreateTime time.Time                         `json:"createTime"`
	Fields     map[string]map[string]interface{} `json:"fields"`
	Name       string                            `json:"name"`
	UpdateTime time.Time                         `json:"updateTime"`
}

Value holds Firestore fields.

func (*Value) DataTo

func (v *Value) DataTo(p interface{}) error

DataTo uses the document's fields to populate p, which can be a pointer to a map[string]interface{} or a pointer to a struct.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL