Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 1 | package model |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | type Proxy struct { |
| 10 | Root *Root |
| 11 | Node *Node |
| 12 | Path string |
| 13 | Exclusive bool |
| 14 | Callbacks []interface{} |
| 15 | } |
| 16 | |
| 17 | func NewProxy(root *Root, node *Node, path string, exclusive bool) *Proxy { |
| 18 | p := &Proxy{ |
| 19 | Root: root, |
| 20 | Node: node, |
| 21 | Exclusive: exclusive, |
| 22 | Path: path, |
| 23 | Callbacks: []interface{}{}, |
| 24 | } |
| 25 | return p |
| 26 | } |
| 27 | |
| 28 | func (p *Proxy) Get(path string, depth int, deep bool, txid string) interface{} { |
| 29 | return p.Node.Get(path, "", depth, deep, txid) |
| 30 | } |
| 31 | |
| 32 | func (p *Proxy) Update(path string, data interface{}, strict bool, txid string) interface{} { |
| 33 | if !strings.HasPrefix(path, "/") { |
| 34 | fmt.Errorf("invalid path: %s", path) |
| 35 | return nil |
| 36 | } |
| 37 | var fullPath string |
| 38 | if path == "/" { |
| 39 | fullPath = p.Path |
| 40 | } else { |
| 41 | fullPath = p.Path + path |
| 42 | } |
| 43 | return p.Node.Update(fullPath, data, strict, txid, nil) |
| 44 | } |
| 45 | |
| 46 | func (p *Proxy) Add(path string, data interface{}, txid string) interface{} { |
| 47 | if !strings.HasPrefix(path, "/") { |
| 48 | fmt.Errorf("invalid path: %s", path) |
| 49 | return nil |
| 50 | } |
| 51 | var fullPath string |
| 52 | if path == "/" { |
| 53 | fullPath = p.Path |
| 54 | } else { |
| 55 | fullPath = p.Path + path |
| 56 | } |
| 57 | return p.Node.Add(fullPath, data, txid, nil) |
| 58 | } |
| 59 | |
| 60 | func (p *Proxy) Remove(path string, txid string) interface{} { |
| 61 | if !strings.HasPrefix(path, "/") { |
| 62 | fmt.Errorf("invalid path: %s", path) |
| 63 | return nil |
| 64 | } |
| 65 | var fullPath string |
| 66 | if path == "/" { |
| 67 | fullPath = p.Path |
| 68 | } else { |
| 69 | fullPath = p.Path + path |
| 70 | } |
| 71 | return p.Node.Remove(fullPath, txid, nil) |
| 72 | } |
| 73 | |
| 74 | func (p *Proxy) openTransaction() *Transaction { |
| 75 | txid := p.Root.makeTxBranch() |
| 76 | return NewTransaction(p, txid) |
| 77 | } |
| 78 | |
| 79 | func (p *Proxy) commitTransaction(txid string) { |
| 80 | p.Root.foldTxBranch(txid) |
| 81 | } |
| 82 | |
| 83 | func (p *Proxy) cancelTransaction(txid string) { |
| 84 | p.Root.deleteTxBranch(txid) |
| 85 | } |
| 86 | |
| 87 | func (p *Proxy) RegisterCallback(callbackType CallbackType, callback func(), args ...interface{}) { |
| 88 | } |
| 89 | |
| 90 | func (p *Proxy) UnregisterCallback(callbackType CallbackType, callback func(), args ...interface{}) { |
| 91 | } |
| 92 | |
| 93 | func (p *Proxy) InvokeCallback(callbackType CallbackType, context context.Context, proceedOnError bool) { |
| 94 | } |