Skip to content

Commit

Permalink
moigagoo#159 Moved procs into utils module
Browse files Browse the repository at this point in the history
Renamed "isContainer" to "isRefObject"
Added documentation to utility procs
  • Loading branch information
PhilippMDoerner committed Aug 23, 2022
1 parent 4ff0fc9 commit b39f970
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
18 changes: 6 additions & 12 deletions src/norm/private/postgres/rowutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ndb/postgres

import dbtypes
import ../dot
import ../utils
import ../../model
import ../../pragmas

Expand All @@ -14,16 +15,9 @@ when (NimMajor, NimMinor) <= (1, 6):
else:
import std/macros


func isContainer*[T](val: typedesc[T]): bool {.compileTime.} = T is ref object
func isContainer*[T](val: T): bool {.compileTime.} = T is ref object
func isContainer*[T](val: typedesc[Option[T]]): bool {.compileTime.} = T is ref object
func isContainer*[T](val: Option[T]): bool {.compileTime.} = T is ref object

func toOptional*[T: ref object](val: T): Option[T] = some val
func toOptional*[T: ref object](val: Option[T]): Option[T] = val
func isEmptyColumn(row: Row, index: int): bool = row[index].kind == dvkNull

func isEmptyColumn*(row: Row, index: int): bool =
## Checks whether the column at a given index is empty
row[index].kind == dvkNull

## This does the actual heavy lifting for parsing
proc fromRowPos[T: ref object](obj: var T, row: Row, pos: var Natural, skip: static bool = false) =
Expand All @@ -33,13 +27,13 @@ proc fromRowPos[T: ref object](obj: var T, row: Row, pos: var Natural, skip: sta
]##

for fld, dummyVal in T()[].fieldPairs:
when isContainer(typeof(dummyVal)): ## If we're dealing with a ``Model`` field
when isRefObject(typeof(dummyVal)): ## If we're dealing with a ``Model`` field
if dot(obj, fld).toOptional().isSome: ## and it's either a ``some Model`` or ``Model``
var subMod = dot(obj, fld).toOptional().get() ## then we try to populate it with the next ``row`` values.

if row.isEmptyColumn(pos): ## If we have a ``NULL`` at this point, we return an empty ``Model``:
when typeof(dummyVal) is Option: ## ``val`` is guaranteed to be either ``Model`` or an ``Option[Model]`` at this point,
when isContainer(dummyVal):
when isRefObject(dummyVal):
dot(obj, fld) = none typeof(subMod) ## and the fact that we got a ``NULL`` tells us it's an ``Option[Model]``,

inc pos
Expand Down
17 changes: 6 additions & 11 deletions src/norm/private/sqlite/rowutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ndb/sqlite

import dbtypes
import ../dot
import ../utils
import ../../model
import ../../pragmas

Expand All @@ -14,15 +15,9 @@ when (NimMajor, NimMinor) <= (1, 6):
else:
import std/macros

func isContainer*[T](val: typedesc[T]): bool {.compileTime.} = T is ref object
func isContainer*[T](val: T): bool {.compileTime.} = T is ref object
func isContainer*[T](val: typedesc[Option[T]]): bool {.compileTime.} = T is ref object
func isContainer*[T](val: Option[T]): bool {.compileTime.} = T is ref object

func toOptional*[T: ref object](val: T): Option[T] = some val
func toOptional*[T: ref object](val: Option[T]): Option[T] = val
func isEmptyColumn(row: Row, index: int): bool = row[index].kind == dvkNull

func isEmptyColumn*(row: Row, index: int): bool =
## Checks whether the column at a given index is empty
row[index].kind == dvkNull

## This does the actual heavy lifting for parsing
proc fromRowPos[T: ref object](obj: var T, row: Row, pos: var Natural, skip: static bool = false) =
Expand All @@ -32,13 +27,13 @@ proc fromRowPos[T: ref object](obj: var T, row: Row, pos: var Natural, skip: sta
]##

for fld, dummyVal in T()[].fieldPairs:
when isContainer(typeof(dummyVal)): ## If we're dealing with a ``Model`` field
when isRefObject(typeof(dummyVal)): ## If we're dealing with a ``Model`` field
if dot(obj, fld).toOptional().isSome: ## and it's either a ``some Model`` or ``Model``
var subMod = dot(obj, fld).toOptional().get() ## then we try to populate it with the next ``row`` values.

if row.isEmptyColumn(pos): ## If we have a ``NULL`` at this point, we return an empty ``Model``:
when typeof(dummyVal) is Option: ## ``val`` is guaranteed to be either ``Model`` or an ``Option[Model]`` at this point,
when isContainer(dummyVal):
when isRefObject(dummyVal):
dot(obj, fld) = none typeof(subMod) ## and the fact that we got a ``NULL`` tells us it's an ``Option[Model]``,

inc pos
Expand Down
25 changes: 25 additions & 0 deletions src/norm/private/utils.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import std/options

func isRefObject*[T](val: typedesc[T]): bool {.compileTime.} =
## Checks if a given type is of type ref object
T is ref object

func isRefObject*[T](val: T): bool {.compileTime.} =
## Checks if a given variable is of type ref object
T is ref object

func isRefObject*[T](val: typedesc[Option[T]]): bool {.compileTime.} =
## Checks if the inner type of an given optional type is a ref object
T is ref object

func isRefObject*[T](val: Option[T]): bool {.compileTime.} =
## Checks if the inner type of the optional variable is a ref object
T is ref object

func toOptional*[T: ref object](val: T): Option[T] =
## Converts non optional type to optional type
some val

func toOptional*[T: ref object](val: Option[T]): Option[T] =
## Convert optional type to optional type, doing effectively nothing
val

0 comments on commit b39f970

Please sign in to comment.