-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathstore.go
77 lines (62 loc) · 1.56 KB
/
store.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package hcat
import (
"sync"
)
// Store is what Template uses to determine the values that are
// available for template parsing.
type Store struct {
sync.RWMutex
// data is the map of individual dependencies and the most recent data for
// that dependency.
data map[string]interface{}
}
// NewStore creates a new Store with empty values for each
// of the key structs.
func NewStore() *Store {
return &Store{
data: make(map[string]interface{}),
}
}
// Save accepts a dependency and the data to store associated with that
// dep. This function converts the given data to a proper type and stores
// it interally.
func (s *Store) Save(id string, data interface{}) {
s.Lock()
defer s.Unlock()
if _, ok := s.data[id]; ok {
s.data[id] = data
return
}
s.data[id] = data
}
// Recall gets the current value for the given dependency in the Store.
func (s *Store) Recall(id string) (interface{}, bool) {
s.RLock()
defer s.RUnlock()
data, ok := s.data[id]
return data, ok
}
// Forget accepts a dependency and removes all associated data with this
// dependency.
func (s *Store) Delete(id string) {
s.Lock()
defer s.Unlock()
delete(s.data, id)
}
// Reset clears all stored data.
func (s *Store) Reset() {
s.Lock()
defer s.Unlock()
for k := range s.data {
delete(s.data, k)
}
}
// forceSet is used to force set the value of a dependency for a given hash
// code. Used in testing.
func (s *Store) forceSet(hashCode string, data interface{}) {
s.Lock()
defer s.Unlock()
s.data[hashCode] = data
}