SetState stores arbitrary future app state as JSON.
(ctx context.Context, key string, value any)
| 204 | |
| 205 | // SetState stores arbitrary future app state as JSON. |
| 206 | func (s Store) SetState(ctx context.Context, key string, value any) error { |
| 207 | if err := s.Init(ctx); err != nil { |
| 208 | return err |
| 209 | } |
| 210 | data, err := json.Marshal(value) |
| 211 | if err != nil { |
| 212 | return fmt.Errorf("appstate: marshal state: %w", err) |
| 213 | } |
| 214 | db, err := s.open() |
| 215 | if err != nil { |
| 216 | return err |
| 217 | } |
| 218 | defer db.Close() |
| 219 | _, err = db.ExecContext(ctx, `INSERT INTO app_state(key, value_json, updated_at) VALUES(?, ?, ?) ON CONFLICT(key) DO UPDATE SET value_json = excluded.value_json, updated_at = excluded.updated_at`, key, string(data), time.Now().UTC().Format(time.RFC3339Nano)) |
| 220 | if err != nil { |
| 221 | return fmt.Errorf("appstate: set state: %w", err) |
| 222 | } |
| 223 | return nil |
| 224 | } |
| 225 | |
| 226 | // GetState loads arbitrary future app state from JSON. |
| 227 | func (s Store) GetState(ctx context.Context, key string, value any) (bool, error) { |