Deleting State Keys does not seem possible today
- Dominant language
- Go
- Stars
- 8.8k
- Forks
- 1k
- Avg merge
- 3d 18h
- Merged PRs (30d)
- 88
Description
This does not seem possible today from what I can tell. In particular, being able to remove user/app state, because I can always start a new session to nuke the session state.
The following change enables one to send empty strings to delete them
```diff
diff --git a/session/database/service.go b/session/database/service.go
index 56e808a..c9e5173 100644
--- a/session/database/service.go
+++ b/session/database/service.go
@@ -67,6 +67,29 @@ func AutoMigrate(service session.Service) error {
return nil
}
+func filterMap(m map[string]any) map[string]any {
+ result := make(map[string]any)
+ for k, v := range m {
+ if v == nil {
+ continue
+ }
+ switch val := v.(type) {
+ case string:
+ if val != "" {
+ result[k] = v
+ }
+ case *string:
+ if val != nil && *val != "" {
+ result[k] = v
+ }
+ default:
+ // Keep all other types
+ result[k] = v
+ }
+ }
+ return result
+}
+
// Create generates a session and inserts it to the db, implements session.Service
func (s *databaseService) Create(ctx context.Context, req *session.CreateRequest) (*session.CreateResponse, error) {
if req.AppName == "" || req.UserID == "" {
@@ -109,19 +132,22 @@ func (s *databaseService) Create(ctx context.Context, req *session.CreateRequest
// apply state delta
if len(appDelta) > 0 {
maps.Copy(storageApp.State, appDelta)
+ storageApp.State = filterMap(storageApp.State)
if err := tx.Save(&storageApp).Error; err != nil {
return fmt.Errorf("failed to save app state: %w", err)
}
}
if len(userDelta) > 0 {
maps.Copy(storageUser.State, userDelta)
+ storageUser.State = filterMap(storageUser.State)
if err := tx.Save(&storageUser).Error; err != nil {
return fmt.Errorf("failed to save user state: %w", err)
}
}
createdSession.State = sessionState
+ createdSession.State = filterMap(createdSession.State)
```
Contributor guide
Research direction
Start in session/database/service.go, especially databaseService.Create and the app, user, and session state updates shown in the issue. Verify the intended behavior for empty strings, nil values, and other state values, and confirm that completed work removes the requested state keys without changing unrelated values.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- backend, databases
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 42/100