# HG changeset patch # User sam # Date 1712503179 -25200 # Node ID 1705e005cdeeafd6f176dc3cf24d4afae08e0fa8 # Parent 3e0116b5d2ed5be33b589b0b09fc687960492a49 add: enforce adding default value for storage loads diff -r 3e0116b5d2ed -r 1705e005cdee 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 3e0116b5d2ed -r 1705e005cdee 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()