aboutsummaryrefslogtreecommitdiff
path: root/state/memory_state.go
blob: b7d26e20ef16f0503fd7856c9745568d1f2d05db (plain)
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
package state

import "context"

type memoryStateValue struct {
	backend   *MemoryStateBackend
	stateName string
}

func (m *memoryStateValue) Value(ctx context.Context) (any, error) {
	kg := m.backend.currentGroupKey
	key := m.backend.currentKey

	if kgMap, exist := m.backend.states[m.stateName][kg]; exist {
		if v, hasKey := kgMap[key]; hasKey {
			return v, nil
		}
	}

	return nil, nil
}

func (m *memoryStateValue) Update(ctx context.Context, newVal any) (any, error) {
	kg := m.backend.currentGroupKey
	key := m.backend.currentKey
	if _, exists := m.backend.states[m.stateName][kg]; !exists {
		m.backend.states[m.stateName][kg] = make(map[string]any)
	}

	m.backend.states[m.stateName][kg][key] = newVal
	return newVal, nil
}

func (m *memoryStateValue) Clear(ctx context.Context) error {
	kg := m.backend.currentGroupKey
	key := m.backend.currentKey
	if kgMap, exists := m.backend.states[m.stateName][kg]; exists {
		delete(kgMap, key)
	}
	return nil
}