khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 16 | package model |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "fmt" |
| 21 | "strings" |
| 22 | ) |
| 23 | |
| 24 | type Proxy struct { |
| 25 | Root *Root |
| 26 | Node *Node |
| 27 | Path string |
| 28 | Exclusive bool |
| 29 | Callbacks []interface{} |
| 30 | } |
| 31 | |
| 32 | func NewProxy(root *Root, node *Node, path string, exclusive bool) *Proxy { |
| 33 | p := &Proxy{ |
| 34 | Root: root, |
| 35 | Node: node, |
| 36 | Exclusive: exclusive, |
| 37 | Path: path, |
| 38 | Callbacks: []interface{}{}, |
| 39 | } |
| 40 | return p |
| 41 | } |
| 42 | |
| 43 | func (p *Proxy) Get(path string, depth int, deep bool, txid string) interface{} { |
| 44 | return p.Node.Get(path, "", depth, deep, txid) |
| 45 | } |
| 46 | |
| 47 | func (p *Proxy) Update(path string, data interface{}, strict bool, txid string) interface{} { |
| 48 | if !strings.HasPrefix(path, "/") { |
| 49 | fmt.Errorf("invalid path: %s", path) |
| 50 | return nil |
| 51 | } |
| 52 | var fullPath string |
| 53 | if path == "/" { |
| 54 | fullPath = p.Path |
| 55 | } else { |
| 56 | fullPath = p.Path + path |
| 57 | } |
| 58 | return p.Node.Update(fullPath, data, strict, txid, nil) |
| 59 | } |
| 60 | |
| 61 | func (p *Proxy) Add(path string, data interface{}, txid string) interface{} { |
| 62 | if !strings.HasPrefix(path, "/") { |
| 63 | fmt.Errorf("invalid path: %s", path) |
| 64 | return nil |
| 65 | } |
| 66 | var fullPath string |
| 67 | if path == "/" { |
| 68 | fullPath = p.Path |
| 69 | } else { |
| 70 | fullPath = p.Path + path |
| 71 | } |
| 72 | return p.Node.Add(fullPath, data, txid, nil) |
| 73 | } |
| 74 | |
| 75 | func (p *Proxy) Remove(path string, txid string) interface{} { |
| 76 | if !strings.HasPrefix(path, "/") { |
| 77 | fmt.Errorf("invalid path: %s", path) |
| 78 | return nil |
| 79 | } |
| 80 | var fullPath string |
| 81 | if path == "/" { |
| 82 | fullPath = p.Path |
| 83 | } else { |
| 84 | fullPath = p.Path + path |
| 85 | } |
| 86 | return p.Node.Remove(fullPath, txid, nil) |
| 87 | } |
| 88 | |
| 89 | func (p *Proxy) openTransaction() *Transaction { |
| 90 | txid := p.Root.makeTxBranch() |
| 91 | return NewTransaction(p, txid) |
| 92 | } |
| 93 | |
| 94 | func (p *Proxy) commitTransaction(txid string) { |
| 95 | p.Root.foldTxBranch(txid) |
| 96 | } |
| 97 | |
| 98 | func (p *Proxy) cancelTransaction(txid string) { |
| 99 | p.Root.deleteTxBranch(txid) |
| 100 | } |
| 101 | |
| 102 | func (p *Proxy) RegisterCallback(callbackType CallbackType, callback func(), args ...interface{}) { |
| 103 | } |
| 104 | |
| 105 | func (p *Proxy) UnregisterCallback(callbackType CallbackType, callback func(), args ...interface{}) { |
| 106 | } |
| 107 | |
| 108 | func (p *Proxy) InvokeCallback(callbackType CallbackType, context context.Context, proceedOnError bool) { |
| 109 | } |