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.
This commit is contained in:
Jonathan Bernard 2020-01-02 18:51:38 -06:00
parent 1f57e0dccc
commit af755a8a8d

View File

@ -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: