Skip to content

Commit 33b3e49

Browse files
committed
🎨 improve key validation logic and enhance stream functionality in storage containers
1 parent 9dc413d commit 33b3e49

File tree

9 files changed

+894
-54
lines changed

9 files changed

+894
-54
lines changed

packages/hyper_storage/lib/src/api/serializable_container.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ abstract class SerializableStorageContainer<E> extends StorageContainer implemen
732732
addKeyListener(key, retrieveAndAdd);
733733

734734
yield* controller.stream;
735-
await controller.close();
735+
await controller.close(); // coverage:ignore-line
736736
}
737737

738738
/// Provides a [Stream] of values for all items in the container.
@@ -763,6 +763,6 @@ abstract class SerializableStorageContainer<E> extends StorageContainer implemen
763763
addListener(retrieveAndAdd);
764764

765765
yield* controller.stream;
766-
await controller.close();
766+
await controller.close(); // coverage:ignore-line
767767
}
768768
}

packages/hyper_storage/lib/src/hyper_storage.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,6 @@ class HyperStorage extends _HyperStorageImpl {
423423
addKeyListener(key, retrieveAndAdd);
424424

425425
yield* controller.stream;
426-
await controller.close();
426+
await controller.close(); // coverage:ignore-line
427427
}
428428
}

packages/hyper_storage/lib/src/hyper_storage_container.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,6 @@ final class HyperStorageContainer extends StorageContainer with ItemHolderMixin,
283283
addKeyListener(key, retrieveAndAdd);
284284

285285
yield* controller.stream;
286-
await controller.close();
286+
await controller.close(); // coverage:ignore-line
287287
}
288288
}

packages/hyper_storage/lib/src/item_holder.dart

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,26 @@ mixin ItemHolderMixin on BaseStorage {
248248
@visibleForTesting
249249
String encodeKey(String key);
250250

251-
/// Validates a key before use. This method should be implemented by the class
252-
/// using this mixin if key validation is required (e.g., for containers).
253-
/// The default implementation does nothing.
251+
/// Validates that a single key is acceptable for storage operations.
252+
///
253+
/// This method checks that the key meets basic requirements for storage:
254+
/// - Must not be empty
255+
/// - Must not consist only of whitespace characters
256+
///
257+
/// Parameters:
258+
/// * [key] - The key to validate.
259+
///
260+
/// Throws:
261+
/// * [ArgumentError] if the key is empty.
262+
/// * [ArgumentError] if the key contains only whitespace.
263+
///
264+
/// Example:
265+
/// ```dart
266+
/// validateKey('validKey'); // OK
267+
/// validateKey(''); // Throws ArgumentError: Key cannot be empty
268+
/// validateKey(' '); // Throws ArgumentError: Key cannot be only whitespace
269+
/// validateKey(' key '); // OK - has non-whitespace content
270+
/// ```
254271
@internal
255272
@protected
256273
@visibleForTesting

packages/hyper_storage/lib/src/storage_base.dart

Lines changed: 25 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,6 @@ abstract class _HyperStorageImpl extends BaseStorage
2121
/// backend should already be initialized before creating this instance.
2222
_HyperStorageImpl({required super.backend});
2323

24-
/// Validates that a single key is acceptable for storage operations.
25-
///
26-
/// This method checks that the key meets basic requirements for storage:
27-
/// - Must not be empty
28-
/// - Must not consist only of whitespace characters
29-
///
30-
/// Parameters:
31-
/// * [key] - The key to validate.
32-
///
33-
/// Throws:
34-
/// * [ArgumentError] if the key is empty.
35-
/// * [ArgumentError] if the key contains only whitespace.
36-
///
37-
/// Example:
38-
/// ```dart
39-
/// _validateKey('validKey'); // OK
40-
/// _validateKey(''); // Throws ArgumentError: Key cannot be empty
41-
/// _validateKey(' '); // Throws ArgumentError: Key cannot be only whitespace
42-
/// _validateKey(' key '); // OK - has non-whitespace content
43-
/// ```
44-
void _validateKey(String key) {
45-
if (key.isEmpty) throw ArgumentError('Key cannot be empty');
46-
if (key.trim().isEmpty) throw ArgumentError('Key cannot be only whitespace');
47-
}
48-
4924
/// Validates that all keys in a collection are acceptable for storage operations.
5025
///
5126
/// This method validates multiple keys using [_validateKey]. If the iterable
@@ -69,7 +44,7 @@ abstract class _HyperStorageImpl extends BaseStorage
6944
void _validateKeys(Iterable<String>? keys) {
7045
if (keys == null || keys.isEmpty) return;
7146
for (final key in keys) {
72-
_validateKey(key);
47+
validateKey(key);
7348
}
7449
}
7550

@@ -81,11 +56,14 @@ abstract class _HyperStorageImpl extends BaseStorage
8156
@override
8257
@internal
8358
@protected
84-
void validateKey(String key) => _validateKey(key);
59+
void validateKey(String key) {
60+
if (key.isEmpty) throw ArgumentError('Key cannot be empty');
61+
if (key.trim().isEmpty) throw ArgumentError('Key cannot be only whitespace');
62+
}
8563

8664
@override
8765
Future<bool> containsKey(String key) {
88-
_validateKey(key);
66+
validateKey(key);
8967
return backend.containsKey(key);
9068
}
9169

@@ -98,43 +76,43 @@ abstract class _HyperStorageImpl extends BaseStorage
9876

9977
@override
10078
Future<bool?> getBool(String key) {
101-
_validateKey(key);
79+
validateKey(key);
10280
return backend.getBool(key);
10381
}
10482

10583
@override
10684
Future<DateTime?> getDateTime(String key, {bool isUtc = false}) {
107-
_validateKey(key);
85+
validateKey(key);
10886
return backend.getDateTime(key, isUtc: isUtc);
10987
}
11088

11189
@override
11290
Future<double?> getDouble(String key) {
113-
_validateKey(key);
91+
validateKey(key);
11492
return backend.getDouble(key);
11593
}
11694

11795
@override
11896
Future<Duration?> getDuration(String key) {
119-
_validateKey(key);
97+
validateKey(key);
12098
return backend.getDuration(key);
12199
}
122100

123101
@override
124102
Future<int?> getInt(String key) {
125-
_validateKey(key);
103+
validateKey(key);
126104
return backend.getInt(key);
127105
}
128106

129107
@override
130108
Future<Map<String, dynamic>?> getJson(String key) {
131-
_validateKey(key);
109+
validateKey(key);
132110
return backend.getJson(key);
133111
}
134112

135113
@override
136114
Future<List<Map<String, dynamic>>?> getJsonList(String key) {
137-
_validateKey(key);
115+
validateKey(key);
138116
return backend.getJsonList(key);
139117
}
140118

@@ -143,13 +121,13 @@ abstract class _HyperStorageImpl extends BaseStorage
143121

144122
@override
145123
Future<String?> getString(String key) {
146-
_validateKey(key);
124+
validateKey(key);
147125
return backend.getString(key);
148126
}
149127

150128
@override
151129
Future<List<String>?> getStringList(String key) {
152-
_validateKey(key);
130+
validateKey(key);
153131
return backend.getStringList(key);
154132
}
155133

@@ -161,7 +139,7 @@ abstract class _HyperStorageImpl extends BaseStorage
161139

162140
@override
163141
Future<void> remove(String key) async {
164-
_validateKey(key);
142+
validateKey(key);
165143
await backend.remove(key);
166144
notifyListeners(key);
167145
}
@@ -188,63 +166,63 @@ abstract class _HyperStorageImpl extends BaseStorage
188166

189167
@override
190168
Future<void> setBool(String key, bool value) async {
191-
_validateKey(key);
169+
validateKey(key);
192170
await backend.setBool(key, value);
193171
notifyListeners(key);
194172
}
195173

196174
@override
197175
Future<void> setDateTime(String key, DateTime value) async {
198-
_validateKey(key);
176+
validateKey(key);
199177
await backend.setDateTime(key, value);
200178
notifyListeners(key);
201179
}
202180

203181
@override
204182
Future<void> setDouble(String key, double value) async {
205-
_validateKey(key);
183+
validateKey(key);
206184
await backend.setDouble(key, value);
207185
notifyListeners(key);
208186
}
209187

210188
@override
211189
Future<void> setDuration(String key, Duration value) async {
212-
_validateKey(key);
190+
validateKey(key);
213191
await backend.setDuration(key, value);
214192
notifyListeners(key);
215193
}
216194

217195
@override
218196
Future<void> setInt(String key, int value) async {
219-
_validateKey(key);
197+
validateKey(key);
220198
await backend.setInt(key, value);
221199
notifyListeners(key);
222200
}
223201

224202
@override
225203
Future<void> setJson(String key, Map<String, dynamic> value) async {
226-
_validateKey(key);
204+
validateKey(key);
227205
await backend.setJson(key, value);
228206
notifyListeners(key);
229207
}
230208

231209
@override
232210
Future<void> setJsonList(String key, List<Map<String, dynamic>> value) async {
233-
_validateKey(key);
211+
validateKey(key);
234212
await backend.setJsonList(key, value);
235213
notifyListeners(key);
236214
}
237215

238216
@override
239217
Future<void> setString(String key, String value) async {
240-
_validateKey(key);
218+
validateKey(key);
241219
await backend.setString(key, value);
242220
notifyListeners(key);
243221
}
244222

245223
@override
246224
Future<void> setStringList(String key, List<String> value) async {
247-
_validateKey(key);
225+
validateKey(key);
248226
await backend.setStringList(key, value);
249227
notifyListeners(key);
250228
}

0 commit comments

Comments
 (0)