-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
envTypes.go
83 lines (71 loc) · 1.76 KB
/
envTypes.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
package env
import (
"fmt"
"reflect"
"strings"
)
// DefineType defines type in current scope.
func (e *Env) DefineType(symbol string, aType interface{}) error {
var reflectType reflect.Type
if aType == nil {
reflectType = NilType
} else {
var ok bool
reflectType, ok = aType.(reflect.Type)
if !ok {
reflectType = reflect.TypeOf(aType)
}
}
return e.DefineReflectType(symbol, reflectType)
}
// DefineReflectType defines type in current scope.
func (e *Env) DefineReflectType(symbol string, reflectType reflect.Type) error {
if strings.Contains(symbol, ".") {
return ErrSymbolContainsDot
}
e.rwMutex.Lock()
if e.types == nil {
e.types = make(map[string]reflect.Type)
}
e.types[symbol] = reflectType
e.rwMutex.Unlock()
return nil
}
// DefineGlobalType defines type in global scope.
func (e *Env) DefineGlobalType(symbol string, aType interface{}) error {
for e.parent != nil {
e = e.parent
}
return e.DefineType(symbol, aType)
}
// DefineGlobalReflectType defines type in global scope.
func (e *Env) DefineGlobalReflectType(symbol string, reflectType reflect.Type) error {
for e.parent != nil {
e = e.parent
}
return e.DefineReflectType(symbol, reflectType)
}
// Type returns reflect type from the scope where symbol is frist found.
func (e *Env) Type(symbol string) (reflect.Type, error) {
e.rwMutex.RLock()
reflectType, ok := e.types[symbol]
e.rwMutex.RUnlock()
if ok {
return reflectType, nil
}
if e.externalLookup != nil {
var err error
reflectType, err = e.externalLookup.Type(symbol)
if err == nil {
return reflectType, nil
}
}
if e.parent == nil {
reflectType, ok = basicTypes[symbol]
if ok {
return reflectType, nil
}
return NilType, fmt.Errorf("undefined type '%s'", symbol)
}
return e.parent.Type(symbol)
}