-
Notifications
You must be signed in to change notification settings - Fork 2
/
vars.go
115 lines (101 loc) · 3.36 KB
/
vars.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
// Copyright 2021 Josh Deprez
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package yarn
import "sync"
// VariableStorage stores values of any kind.
type VariableStorage interface {
GetValue(name string) (value any, ok bool)
SetValue(name string, value any)
}
// MapVariableStorage implements VariableStorage, in memory, using a map.
// In addition to the core VariableStorage functionality, there are methods for
// accessing the contents as an ordinary map[string]any.
type MapVariableStorage struct {
mu sync.RWMutex
m map[string]any
}
// NewMapVariableStorage creates a new empty MapVariableStorage.
func NewMapVariableStorage() *MapVariableStorage {
return &MapVariableStorage{
m: make(map[string]any),
}
}
// NewMapVariableStorageFromMap creates a new MapVariableStorage with initial
// contents copied from src. It does not keep a reference to src.
func NewMapVariableStorageFromMap(src map[string]any) *MapVariableStorage {
return &MapVariableStorage{
m: copyMap(src),
}
}
// Clear empties the storage of all values.
func (m *MapVariableStorage) Clear() {
m.mu.Lock()
defer m.mu.Unlock()
for name := range m.m {
delete(m.m, name)
}
}
// GetValue fetches a value from the storage, returning (nil, false) if not present.
func (m *MapVariableStorage) GetValue(name string) (value any, found bool) {
m.mu.RLock()
defer m.mu.RUnlock()
value, found = m.m[name]
return value, found
}
// SetValue sets a value in the storage.
func (m *MapVariableStorage) SetValue(name string, value any) {
m.mu.Lock()
defer m.mu.Unlock()
m.m[name] = value
}
// Delete deletes values from the storage.
func (m *MapVariableStorage) Delete(names ...string) {
m.mu.Lock()
defer m.mu.Unlock()
for _, name := range names {
delete(m.m, name)
}
}
// Contents returns a copy of the contents of the storage, as a regular map.
// The returned map is a copy, it is not a reference to the map contained within
// the storage (to avoid accidental data races).
func (m *MapVariableStorage) Contents() map[string]any {
m.mu.RLock()
defer m.mu.RUnlock()
return copyMap(m.m)
}
// Clone returns a new MapVariableStorage that is a clone of the receiver.
// The new storage is a deep copy, and does not contain a reference to the
// original map inside the receiver (to avoid accidental data races).
func (m *MapVariableStorage) Clone() *MapVariableStorage {
m.mu.RLock()
defer m.mu.RUnlock()
return NewMapVariableStorageFromMap(m.m)
}
// ReplaceContents replaces the contents of the storage with values from a
// regular map. ReplaceContents copies src, it does not keep a reference to src
// (to avoid accidental data races).
func (m *MapVariableStorage) ReplaceContents(src map[string]any) {
m2 := copyMap(src)
m.mu.Lock()
defer m.mu.Unlock()
m.m = m2
}
func copyMap[K comparable, V any](src map[K]V) map[K]V {
m := make(map[K]V, len(src))
for name, val := range src {
m[name] = val
}
return m
}