Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 1 | package model |
| 2 | |
| 3 | import "fmt" |
| 4 | |
| 5 | type Transaction struct { |
| 6 | proxy *Proxy |
| 7 | txid string |
| 8 | } |
| 9 | |
| 10 | func NewTransaction(proxy *Proxy, txid string) *Transaction { |
| 11 | tx := &Transaction{ |
| 12 | proxy: proxy, |
| 13 | txid: txid, |
| 14 | } |
| 15 | return tx |
| 16 | } |
| 17 | func (t *Transaction) Get(path string, depth int, deep bool) *Revision { |
| 18 | if t.txid == "" { |
| 19 | fmt.Errorf("closed transaction") |
| 20 | return nil |
| 21 | } |
| 22 | // TODO: need to review the return values at the different layers!!!!! |
| 23 | return t.proxy.Get(path, depth, deep, t.txid).(*Revision) |
| 24 | } |
| 25 | func (t *Transaction) Update(path string, data interface{}, strict bool) *Revision { |
| 26 | if t.txid == "" { |
| 27 | fmt.Errorf("closed transaction") |
| 28 | return nil |
| 29 | } |
| 30 | return t.proxy.Update(path, data, strict, t.txid).(*Revision) |
| 31 | } |
| 32 | func (t *Transaction) Add(path string, data interface{}) *Revision { |
| 33 | if t.txid == "" { |
| 34 | fmt.Errorf("closed transaction") |
| 35 | return nil |
| 36 | } |
| 37 | return t.proxy.Add(path, data, t.txid).(*Revision) |
| 38 | } |
| 39 | func (t *Transaction) Remove(path string) *Revision { |
| 40 | if t.txid == "" { |
| 41 | fmt.Errorf("closed transaction") |
| 42 | return nil |
| 43 | } |
| 44 | return t.proxy.Remove(path, t.txid).(*Revision) |
| 45 | } |
| 46 | func (t *Transaction) Cancel() { |
| 47 | t.proxy.cancelTransaction(t.txid) |
| 48 | t.txid = "" |
| 49 | } |
| 50 | func (t *Transaction) Commit() { |
| 51 | t.proxy.commitTransaction(t.txid) |
| 52 | t.txid = "" |
| 53 | } |