# HG changeset patch # User sam # Date 1712503179 -25200 # Node ID 1205e7757732193269800ae1b4553ead11cf9dad # Parent 45cf94a6535cbefe9054ff086951e8dc7cd4227a add: enforce adding default value for storage loads diff -r 45cf94a6535c -r 1205e7757732 semicongine/storage.nim --- a/semicongine/storage.nim Sun Apr 07 21:56:43 2024 +0700 +++ b/semicongine/storage.nim Sun Apr 07 22:19:39 2024 +0700 @@ -43,7 +43,7 @@ ON CONFLICT(key) DO UPDATE SET value=excluded.value """), key, $$value) -proc load*[T](storageType: StorageType, key: string, default = default(T)): T = +proc load*[T](storageType: StorageType, key: string, default: T): T = storageType.setup() const KEY_VALUE_TABLE_NAME = "shelf" let dbResult = db[storageType].getValue(sql(&"""SELECT value FROM {KEY_VALUE_TABLE_NAME} WHERE key = ? """), key) diff -r 45cf94a6535c -r 1205e7757732 tests/test_storage.nim --- a/tests/test_storage.nim Sun Apr 07 21:56:43 2024 +0700 +++ b/tests/test_storage.nim Sun Apr 07 22:19:39 2024 +0700 @@ -8,17 +8,17 @@ const KEY = "test" # get default - assert load[int](storage, KEY) == default(type(TEST_VALUE)) + assert storage.load(KEY, 0) == default(type(TEST_VALUE)) # save and load custom store(storage, KEY, TEST_VALUE) - assert load[int](storage, KEY) == TEST_VALUE + assert storage.load(KEY, 0) == TEST_VALUE proc stressTest(storage: StorageType) = for i in 1 .. 10000: let key = &"key-{i}" store(storage, key, i) - assert load[int](storage, key) == i + assert storage.load(key, 0) == i proc main() = SystemStorage.purge()