changeset 1099:1205e7757732

add: enforce adding default value for storage loads
author sam <sam@basx.dev>
date Sun, 07 Apr 2024 22:19:39 +0700
parents 45cf94a6535c
children 6b6987c25b83
files semicongine/storage.nim tests/test_storage.nim
diffstat 2 files changed, 4 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- 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)
--- 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()