This repository has been archived by the owner on Mar 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
/
transaction.go
82 lines (70 loc) · 2.33 KB
/
transaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package firego
import (
"encoding/json"
"errors"
"fmt"
"net/http"
)
// TransactionFn is used to run a transaction on a Firebase reference.
// See Firebase.Transaction for more information.
type TransactionFn func(currentSnapshot interface{}) (result interface{}, err error)
func getTransactionParams(headers http.Header, body []byte) (etag string, snapshot interface{}, err error) {
etag = headers.Get("ETag")
if len(etag) == 0 {
return etag, snapshot, errors.New("no etag returned by Firebase")
}
if err := json.Unmarshal(body, &snapshot); err != nil {
return etag, snapshot, fmt.Errorf("failed to unmarshal Firebase response. %s", err)
}
return etag, snapshot, nil
}
// Transaction runs a transaction on the data at this location. The TransactionFn parameter
// will be called, possibly multiple times, with the current data at this location.
// It is responsible for inspecting that data and specifying either the desired new data
// at the location or that the transaction should be aborted.
//
// Since the provided function may be called repeatedly for the same transaction, be extremely careful of
// any side effects that may be triggered by this method.
//
// Best practices for this method are to rely only on the data that is passed in.
func (fb *Firebase) Transaction(fn TransactionFn) error {
// fetch etag and current value
headers, body, err := fb.doRequest("GET", nil, withHeader("X-Firebase-ETag", "true"))
if err != nil {
return err
}
etag, snapshot, err := getTransactionParams(headers, body)
if err != nil {
return err
}
// set the error value to something non-nil so that
// we step into the loop
tErr := errors.New("")
for i := 0; i < 25 && tErr != nil; i++ {
// run transaction
result, err := fn(snapshot)
if err != nil {
return nil
}
newBody, err := json.Marshal(result)
if err != nil {
return fmt.Errorf("failed to marshal transaction result. %s", err)
}
// attempt to update it
headers, body, tErr = fb.doRequest("PUT", newBody, withHeader("if-match", etag))
if tErr == nil {
// we're good, break the loop
break
}
// we failed to update, so grab the new snapshot/etag
e, s, tErr := getTransactionParams(headers, body)
if tErr != nil {
return tErr
}
etag, snapshot = e, s
}
if tErr != nil {
return fmt.Errorf("failed to run transaction. %s", tErr)
}
return nil
}