diff options
Diffstat (limited to 'state/memory_state.go')
| -rw-r--r-- | state/memory_state.go | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/state/memory_state.go b/state/memory_state.go new file mode 100644 index 0000000..95c7318 --- /dev/null +++ b/state/memory_state.go | |||
| @@ -0,0 +1,44 @@ | |||
| 1 | package state | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "context" | ||
| 5 | ) | ||
| 6 | |||
| 7 | type memoryStateValue struct { | ||
| 8 | backend *MemoryStateBackend | ||
| 9 | stateName string | ||
| 10 | } | ||
| 11 | |||
| 12 | func (m *memoryStateValue) Value(ctx context.Context) (any, error) { | ||
| 13 | kg := m.backend.currentGroupKey | ||
| 14 | key := m.backend.currentKey | ||
| 15 | ns := m.backend.currentNamespace | ||
| 16 | if kgMap, exist := m.backend.states[m.stateName][kg][key]; exist { | ||
| 17 | if v, hasKey := kgMap[ns]; hasKey { | ||
| 18 | return v, nil | ||
| 19 | } | ||
| 20 | } | ||
| 21 | |||
| 22 | return nil, nil | ||
| 23 | } | ||
| 24 | |||
| 25 | func (m *memoryStateValue) Update(ctx context.Context, newVal any) (any, error) { | ||
| 26 | kg := m.backend.currentGroupKey | ||
| 27 | key := m.backend.currentKey | ||
| 28 | ns := m.backend.currentNamespace | ||
| 29 | if _, exists := m.backend.states[m.stateName][kg][key]; !exists { | ||
| 30 | m.backend.states[m.stateName][kg] = make(map[string]map[any]any) | ||
| 31 | } | ||
| 32 | |||
| 33 | m.backend.states[m.stateName][kg][key][ns] = newVal | ||
| 34 | return newVal, nil | ||
| 35 | } | ||
| 36 | |||
| 37 | func (m *memoryStateValue) Clear(ctx context.Context) error { | ||
| 38 | kg := m.backend.currentGroupKey | ||
| 39 | key := m.backend.currentKey | ||
| 40 | if kgMap, exists := m.backend.states[m.stateName][kg]; exists { | ||
| 41 | delete(kgMap, key) | ||
| 42 | } | ||
| 43 | return nil | ||
| 44 | } | ||
