Set walks the scope and sets a value in a parent scope if it exists, else current.
(name string, val interface{})
| 102 | |
| 103 | // Set walks the scope and sets a value in a parent scope if it exists, else current. |
| 104 | func (scope *Scope) Set(name string, val interface{}) { |
| 105 | if val != nil { |
| 106 | value := reflect.ValueOf(val) |
| 107 | if !value.CanAddr() { |
| 108 | nv := reflect.New(value.Type()) |
| 109 | nv.Elem().Set(value) |
| 110 | val = nv.Interface() |
| 111 | } else { |
| 112 | val = value.Addr().Interface() |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | exists := false |
| 117 | currentScope := scope |
| 118 | for !exists && currentScope != nil { |
| 119 | currentScope.Lock() |
| 120 | _, exists = currentScope.Vals[name] |
| 121 | if exists { |
| 122 | currentScope.Vals[name] = val |
| 123 | } |
| 124 | currentScope.Unlock() |
| 125 | currentScope = currentScope.Parent |
| 126 | } |
| 127 | if !exists { |
| 128 | scope.Lock() |
| 129 | scope.Vals[name] = val |
| 130 | scope.Unlock() |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // Keys returns all keys in scope |
| 135 | func (scope *Scope) Keys() (keys []string) { |
no outgoing calls