-
-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathenvValues.go
161 lines (135 loc) · 3.49 KB
/
envValues.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package env
import (
"fmt"
"reflect"
"strings"
)
// define
// Define defines/sets interface value to symbol in current scope.
func (e *Env) Define(symbol string, value interface{}) error {
if value == nil {
return e.DefineValue(symbol, NilValue)
}
return e.DefineValue(symbol, reflect.ValueOf(value))
}
// DefineValue defines/sets reflect value to symbol in current scope.
func (e *Env) DefineValue(symbol string, value reflect.Value) error {
if strings.Contains(symbol, ".") {
return ErrSymbolContainsDot
}
e.rwMutex.Lock()
e.values[symbol] = value
e.rwMutex.Unlock()
return nil
}
// DefineGlobal defines/sets interface value to symbol in global scope.
func (e *Env) DefineGlobal(symbol string, value interface{}) error {
for e.parent != nil {
e = e.parent
}
return e.Define(symbol, value)
}
// DefineGlobalValue defines/sets reflect value to symbol in global scope.
func (e *Env) DefineGlobalValue(symbol string, value reflect.Value) error {
for e.parent != nil {
e = e.parent
}
return e.DefineValue(symbol, value)
}
// set
// Set interface value to the scope where symbol is frist found.
func (e *Env) Set(symbol string, value interface{}) error {
if value == nil {
return e.SetValue(symbol, NilValue)
}
return e.SetValue(symbol, reflect.ValueOf(value))
}
// SetValue reflect value to the scope where symbol is frist found.
func (e *Env) SetValue(symbol string, value reflect.Value) error {
e.rwMutex.RLock()
_, ok := e.values[symbol]
e.rwMutex.RUnlock()
if ok {
e.rwMutex.Lock()
e.values[symbol] = value
e.rwMutex.Unlock()
return nil
}
if e.parent == nil {
return fmt.Errorf("undefined symbol '%s'", symbol)
}
return e.parent.SetValue(symbol, value)
}
// get
// Get returns interface value from the scope where symbol is frist found.
func (e *Env) Get(symbol string) (interface{}, error) {
rv, err := e.GetValue(symbol)
return rv.Interface(), err
}
// GetValue returns reflect value from the scope where symbol is frist found.
func (e *Env) GetValue(symbol string) (reflect.Value, error) {
e.rwMutex.RLock()
value, ok := e.values[symbol]
e.rwMutex.RUnlock()
if ok {
return value, nil
}
if e.externalLookup != nil {
var err error
value, err = e.externalLookup.Get(symbol)
if err == nil {
return value, nil
}
}
if e.parent == nil {
return NilValue, fmt.Errorf("undefined symbol '%s'", symbol)
}
return e.parent.GetValue(symbol)
}
// delete
// Delete deletes symbol in current scope.
func (e *Env) Delete(symbol string) {
e.rwMutex.Lock()
delete(e.values, symbol)
e.rwMutex.Unlock()
}
// DeleteGlobal deletes the first matching symbol found in current or parent scope.
func (e *Env) DeleteGlobal(symbol string) {
if e.parent == nil {
e.Delete(symbol)
return
}
e.rwMutex.RLock()
_, ok := e.values[symbol]
e.rwMutex.RUnlock()
if ok {
e.Delete(symbol)
return
}
e.parent.DeleteGlobal(symbol)
}
// Addr
// Addr returns reflect.Addr of value for first matching symbol found in current or parent scope.
func (e *Env) Addr(symbol string) (reflect.Value, error) {
e.rwMutex.RLock()
defer e.rwMutex.RUnlock()
if v, ok := e.values[symbol]; ok {
if v.CanAddr() {
return v.Addr(), nil
}
return NilValue, fmt.Errorf("unaddressable")
}
if e.externalLookup != nil {
v, err := e.externalLookup.Get(symbol)
if err == nil {
if v.CanAddr() {
return v.Addr(), nil
}
return NilValue, fmt.Errorf("unaddressable")
}
}
if e.parent == nil {
return NilValue, fmt.Errorf("undefined symbol '%s'", symbol)
}
return e.parent.Addr(symbol)
}