Skip to content

Commit 697d6cd

Browse files
committed
✨ add support for enums.
1 parent 885cb81 commit 697d6cd

File tree

4 files changed

+96
-14
lines changed

4 files changed

+96
-14
lines changed

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,38 @@ abstract interface class StorageOperationsApi {
692692
/// * [ArgumentError] if the key invalid.
693693
Future<void> setDuration(String key, Duration value);
694694

695+
/// Saves an enum value of type [E] with the given [key].
696+
///
697+
/// This method stores an enum value by its name, associating it with the specified key.
698+
/// If a value already exists for this key, it will be replaced.
699+
///
700+
/// Parameters:
701+
/// - [key]: The unique identifier for the enum value.
702+
/// - [value]: The enum value to store.
703+
///
704+
/// Returns a [Future] that completes when the enum value has been successfully stored.
705+
///
706+
/// Throws:
707+
/// - [ArgumentError] if the key is invalid.
708+
Future<void> setEnum<E extends Enum>(String key, E value);
709+
710+
/// Retrieves an enum value of type [E] by its [key].
711+
///
712+
/// This method fetches an enum value previously stored using [setEnum].
713+
/// If no value exists for the given key, or the stored value does not match
714+
/// any of the provided enum values, this method returns `null`.
715+
///
716+
/// Parameters:
717+
/// - [key]: The unique identifier of the enum value to retrieve.
718+
/// - [values]: A list of all possible enum values of type [E].
719+
///
720+
/// Returns a [Future] that completes with the stored enum value if found,
721+
/// or `null` if the key doesn't exist or the value does not match any enum values.
722+
///
723+
/// Throws:
724+
/// * [ArgumentError] if the key is invalid.
725+
Future<E?> getEnum<E extends Enum>(String key, List<E> values);
726+
695727
/// Retrieves a value of type [E] by its [key].
696728
///
697729
/// This is a generic method that attempts to retrieve a value and cast it to
@@ -711,7 +743,7 @@ abstract interface class StorageOperationsApi {
711743
///
712744
/// Throws:
713745
/// * [ArgumentError] if the key is invalid.
714-
Future<E?> get<E extends Object>(String key);
746+
Future<E?> get<E extends Object>(String key, {List<Enum>? enumValues});
715747

716748
/// Saves a value of type [E] with the given [key].
717749
///

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

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,19 @@ abstract class StorageBackend with GenericStorageOperationsMixin implements Stor
135135

136136
@override
137137
Future<void> setDuration(String key, Duration value) => setInt(key, value.inMilliseconds);
138+
139+
@override
140+
Future<void> setEnum<E extends Enum>(String key, E value) => setString(key, value.name);
141+
142+
@override
143+
Future<E?> getEnum<E extends Enum>(String key, List<E> values) async {
144+
final enumName = await getString(key);
145+
if (enumName == null) return null;
146+
for (final value in values) {
147+
if (value.name == enumName) return value;
148+
}
149+
return null;
150+
}
138151
}
139152

140153
/// A mixin that provides generic implementations for storage operations.
@@ -170,19 +183,19 @@ mixin GenericStorageOperationsMixin implements StorageOperationsApi {
170183
}
171184

172185
@override
173-
Future<E?> get<E extends Object>(String key) async {
174-
return switch (E) {
175-
const (String) => await getString(key) as E?,
176-
const (int) => await getInt(key) as E?,
177-
const (double) => await getDouble(key) as E?,
178-
const (bool) => await getBool(key) as E?,
179-
const (DateTime) => await getDateTime(key) as E?,
180-
const (Duration) => await getDuration(key) as E?,
181-
const (List<String>) => await getStringList(key) as E?,
182-
const (Map<String, dynamic>) => await getJson(key) as E?,
183-
const (List<Map<String, dynamic>>) => await getJsonList(key) as E?,
184-
_ => throw UnsupportedError('Type $E is not supported'),
185-
};
186+
Future<E?> get<E extends Object>(String key, {List<Enum>? enumValues}) async {
187+
if (E == String) return await getString(key) as E?;
188+
if (E == int) return await getInt(key) as E?;
189+
if (E == double) return await getDouble(key) as E?;
190+
if (E == bool) return await getBool(key) as E?;
191+
if (E == DateTime) return await getDateTime(key) as E?;
192+
if (E == Duration) return await getDuration(key) as E?;
193+
if (E == List<String>) return await getStringList(key) as E?;
194+
if (E == Map<String, dynamic>) return await getJson(key) as E?;
195+
if (E == List<Map<String, dynamic>>) return await getJsonList(key) as E?;
196+
if (enumValues != null) return await getEnum(key, enumValues) as E?;
197+
198+
throw UnsupportedError('Type $E is not supported');
186199
}
187200

188201
@override
@@ -197,6 +210,7 @@ mixin GenericStorageOperationsMixin implements StorageOperationsApi {
197210
List<String> value => setStringList(key, value),
198211
Map<String, dynamic> value => setJson(key, value),
199212
List<Map<String, dynamic>> value => setJsonList(key, value),
213+
Enum value => setString(key, value.name),
200214
_ => throw UnsupportedError('Type ${value.runtimeType} is not supported'),
201215
};
202216
}

packages/hyper_storage/lib/src/hyper_storage_container.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,24 @@ final class HyperStorageContainer extends StorageContainer with ItemHolderMixin,
214214
notifyListeners(key);
215215
}
216216

217+
@override
218+
Future<E?> getEnum<E extends Enum>(String key, List<E> values) async {
219+
validateKey(key);
220+
final String? enumName = await backend.getString(encodeKey(key));
221+
if (enumName == null) return null;
222+
for (final enumValue in values) {
223+
if (enumValue.name == enumName) return enumValue;
224+
}
225+
return null;
226+
}
227+
228+
@override
229+
Future<void> setEnum<E extends Enum>(String key, E value) async {
230+
validateKey(key);
231+
await backend.setString(encodeKey(key), value.name);
232+
notifyListeners(key);
233+
}
234+
217235
@override
218236
Future<void> removeAll(Iterable<String> keys) async {
219237
validateKeys(keys);

packages/hyper_storage/lib/src/storage_base.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,24 @@ abstract class _HyperStorageImpl extends BaseStorage
192192
notifyListeners(key);
193193
}
194194

195+
@override
196+
Future<E?> getEnum<E extends Enum>(String key, List<E> values) async {
197+
validateKey(key);
198+
final String? enumName = await backend.getString(key);
199+
if (enumName == null) return null;
200+
for (final enumValue in values) {
201+
if (enumValue.name == enumName) return enumValue;
202+
}
203+
return null;
204+
}
205+
206+
@override
207+
Future<void> setEnum<E extends Enum>(String key, E value) async {
208+
validateKey(key);
209+
await backend.setString(key, value.name);
210+
notifyListeners(key);
211+
}
212+
195213
@override
196214
Future<void> setInt(String key, int value) async {
197215
validateKey(key);

0 commit comments

Comments
 (0)