Ver Fonte

Undo broken type gen, verify type at compile time

Zed há 7 anos atrás
pai
commit
520bafc617
3 ficheiros alterados com 21 adições e 18 exclusões
  1. 12 1
      src/prefs.nim
  2. 1 16
      src/prefs_impl.nim
  3. 8 1
      src/types.nim

+ 12 - 1
src/prefs.nim

@@ -1,9 +1,20 @@
-import strutils
+import sequtils, macros
 import types
 import prefs_impl
 
 export genUpdatePrefs
 
+static:
+  var pFields: seq[string]
+  for id in getTypeImpl(Prefs)[2]:
+    if $id[0] == "id": continue
+    pFields.add $id[0]
+
+  let pDefs = toSeq(allPrefs()).mapIt(it.name)
+  let missing = pDefs.filterIt(it notin pFields)
+  if missing.len > 0:
+    raiseAssert("{$1} missing from the Prefs type" % missing.join(", "))
+
 withCustomDb("prefs.db", "", "", ""):
   try:
     createTables()

+ 1 - 16
src/prefs_impl.nim

@@ -54,7 +54,7 @@ const prefList*: Table[string, seq[Pref]] = {
   ]
 }.toTable
 
-iterator allPrefs(): Pref =
+iterator allPrefs*(): Pref =
   for k, v in prefList:
     for pref in v:
       yield pref
@@ -93,18 +93,3 @@ macro genUpdatePrefs*(): untyped =
   result.add quote do:
     cache(prefs)
 
-macro genPrefsType*(): untyped =
-  result = nnkTypeSection.newTree(nnkTypeDef.newTree(
-    nnkPostfix.newTree(ident("*"), ident("Prefs")), newEmptyNode(),
-    nnkObjectTy.newTree(newEmptyNode(), newEmptyNode(), nnkRecList.newTree())))
-
-  result[0][2][2].add nnkIdentDefs.newTree(
-    nnkPostfix.newTree(ident("*"), ident("id")), ident("int"), newEmptyNode())
-
-  for pref in allPrefs():
-    result[0][2][2].add nnkIdentDefs.newTree(
-      nnkPostfix.newTree(ident("*"), ident(pref.name)),
-      (case pref.kind
-       of checkbox: ident("bool")
-       of input, select: ident("string")),
-      newEmptyNode())

+ 8 - 1
src/types.nim

@@ -50,7 +50,14 @@ db("cache.db", "", "", ""):
           formatIt: dbValue($it)
         .}: VideoType
 
-  genPrefsType()
+    Prefs* = object
+      videoPlayback*: bool
+      autoplayGifs*: bool
+      hideTweetStats*: bool
+      hideBanner*: bool
+      stickyProfile*: bool
+      replaceYouTube*: string
+      replaceTwitter*: string
 
 type
   QueryKind* = enum