Guest User

Untitled

a guest
May 26th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. package firestore
  2.  
  3. import (
  4. "cloud.google.com/go/firestore"
  5. )
  6.  
  7. type TransactionDelta struct {
  8. tx *firestore.Transaction
  9. delta []func() error
  10. }
  11.  
  12. func (writes *TransactionDelta) Create(doc *firestore.DocumentRef, d interface{}) {
  13. writes.delta = append(writes.delta, func() error {
  14. return writes.tx.Create(doc, d)
  15. })
  16. }
  17.  
  18. func (writes *TransactionDelta) Set(doc *firestore.DocumentRef, d interface{}, opts... firestore.SetOption) {
  19. writes.delta = append(writes.delta, func() error {
  20. return writes.tx.Set(doc, d, opts...)
  21. })
  22. }
  23.  
  24. func (writes *TransactionDelta) Update(doc *firestore.DocumentRef, u []firestore.Update, precondition... firestore.Precondition) {
  25. writes.delta = append(writes.delta, func() error {
  26. return writes.tx.Update(doc, u, precondition...)
  27. })
  28. }
  29.  
  30. func (writes *TransactionDelta) Delete(doc *firestore.DocumentRef, precondition... firestore.Precondition) {
  31. writes.delta = append(writes.delta, func() error {
  32. return writes.tx.Delete(doc, precondition...)
  33. })
  34. }
  35.  
  36. func (writes *TransactionDelta) Apply() error {
  37. for _, fn := range writes.delta {
  38. err := fn()
  39. if err != nil {
  40. return err
  41. }
  42. }
  43. return nil
  44. }
  45.  
  46. func NewTransactionDelta(tx *firestore.Transaction) *TransactionDelta {
  47. return &TransactionDelta{
  48. tx: tx,
  49. delta: []func() error{},
  50. }
  51. }
Add Comment
Please, Sign In to add comment