diff options
| author | SangTran-127 <tranquangsang12.7@gmail.com> | 2026-08-09 14:52:46 +0700 |
|---|---|---|
| committer | SangTran-127 <tranquangsang12.7@gmail.com> | 2026-08-09 14:52:46 +0700 |
| commit | a892cb5b1d121177881fa56a79abbafef5880a80 (patch) | |
| tree | a410523a4c91de1a35b80a279bc6c8c1179fdffb /state/memory_state.go | |
| parent | 4d62f01af159fddd56585b3dce19b8a0f47369c1 (diff) | |
| download | goflink-a892cb5b1d121177881fa56a79abbafef5880a80.tar.gz goflink-a892cb5b1d121177881fa56a79abbafef5880a80.zip | |
feat: implement GetValueState and memoryStateValue for state management
Diffstat (limited to 'state/memory_state.go')
| -rw-r--r-- | state/memory_state.go | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/state/memory_state.go b/state/memory_state.go new file mode 100644 index 0000000..b7d26e2 --- /dev/null +++ b/state/memory_state.go @@ -0,0 +1,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 +} |