From af755a8a8d32cadcac6265812ae6d7dcc004450b Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Thu, 2 Jan 2020 18:51:38 -0600 Subject: [PATCH] Round out support for `Option` type model fields. The Option type has two forms depending on the type of the wrapped value (see https://nim-lang.org/docs/options.html#Option). We only supported one of these previously. This commit adds support for the other type as well. Additionally, this fixes a compile error that was introduced into the use of `isSome` in the generated code after Nim 1.0. --- src/fiber_orm/util.nim | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/fiber_orm/util.nim b/src/fiber_orm/util.nim index 8411af8..0ec2222 100644 --- a/src/fiber_orm/util.nim +++ b/src/fiber_orm/util.nim @@ -164,7 +164,11 @@ proc createParseStmt*(t, value: NimNode): NimNode = result = quote do: parsePGDatetime(`value`) elif t.getTypeInst == Option.getType: - let innerType = t.getTypeImpl[2][0][0][1] + var innerType = t.getTypeImpl[2][0] # start at the first RecList + # If the value is a non-pointer type, there is another inner RecList + if innerType.kind == nnkRecList: innerType = innerType[0] + innerType = innerType[1] # now we can take the field type from the first symbol + let parseStmt = createParseStmt(innerType, value) result = quote do: if `value`.len == 0: none[`innerType`]() @@ -293,7 +297,7 @@ macro populateMutateClauses*(t: typed, newRecord: bool, mc: var MutateClauses): result.add quote do: `mc`.columns.add(identNameToDb(`fieldName`)) - if `t`.`fieldIdent`.isSome: + if isSome(`t`.`fieldIdent`): `mc`.placeholders.add("?") `mc`.values.add(dbFormat(`t`.`fieldIdent`.get)) else: