-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.go
65 lines (55 loc) · 1.48 KB
/
memory.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
// memory.go
package taskwrappr
type MemoryMap struct {
Parent *MemoryMap
Actions map[string]*Action
Variables map[string]*Variable
}
func NewMemoryMap(parent *MemoryMap) *MemoryMap {
return &MemoryMap{
Parent: parent,
Actions: make(map[string]*Action),
Variables: make(map[string]*Variable),
}
}
func (m *MemoryMap) GetAction(name string) *Action {
for scope := m; scope != nil; scope = scope.Parent {
if action, ok := scope.Actions[name]; ok {
return action
}
}
return nil
}
func (m *MemoryMap) GetVariable(name string) *Variable {
for scope := m; scope != nil; scope = scope.Parent {
if variable, ok := scope.Variables[name]; ok {
return variable
}
}
return nil
}
func (m *MemoryMap) MakeVariable(name string, value interface{}) *Variable {
variable := NewVariable(value, DetermineVariableType(value))
m.Variables[name] = variable
return variable
}
func (m *MemoryMap) SetVariable(name string, value interface{}, variableType VariableType) *Variable {
for scope := m; scope != nil; scope = scope.Parent {
if variable := scope.GetVariable(name); variable != nil {
variable.Value = value
variable.Type = variableType
return variable
}
}
return m.MakeVariable(name, value)
}
func (m *MemoryMap) DeleteAction(name string) {
delete(m.Variables, name)
}
func (m *MemoryMap) DeleteVariable(name string) {
delete(m.Variables, name)
}
func (m *MemoryMap) Clear() {
m.Actions = make(map[string]*Action)
m.Variables = make(map[string]*Variable)
}