GetWithContext returns a defensive copy of the stored bytes so callers can safely mutate the returned slice without changing the shared value.
(ctx context.Context, key string)
| 97 | // GetWithContext returns a defensive copy of the stored bytes so callers can |
| 98 | // safely mutate the returned slice without changing the shared value. |
| 99 | func (s *SharedState) GetWithContext(ctx context.Context, key string) ([]byte, bool, error) { //nolint:gocritic // Keep unnamed returns for clarity. |
| 100 | if err := s.ensureStorage(); err != nil { |
| 101 | return nil, false, err |
| 102 | } |
| 103 | |
| 104 | storageKey, ok := s.storageKey(key) |
| 105 | if !ok { |
| 106 | return nil, false, nil |
| 107 | } |
| 108 | |
| 109 | data, err := s.storage.GetWithContext(ctx, storageKey) |
| 110 | if err != nil { |
| 111 | return nil, false, err |
| 112 | } |
| 113 | if data == nil { |
| 114 | return nil, false, nil |
| 115 | } |
| 116 | |
| 117 | return append([]byte(nil), data...), true, nil |
| 118 | } |
| 119 | |
| 120 | func (s *SharedState) SetJSON(key string, v any, ttl time.Duration) error { |
| 121 | return s.SetJSONWithContext(context.Background(), key, v, ttl) |
no test coverage detected