Skip to content

Commit

Permalink
Fix instanting object variants
Browse files Browse the repository at this point in the history
Somewhere during nim 1.6.X the behaviour for
object variants changed.
It is no longer allowed to change the kind value after
instantiating the variant.

This was a breaking change introduced in between patch versions. Not cool.
  • Loading branch information
PhilippMDoerner committed Oct 27, 2023
1 parent bf41a0c commit c3d050b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
24 changes: 14 additions & 10 deletions docs/book/customDatatypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,13 @@ nbCode:
options.add(IntOption(name: $enumValue, value: enumValue.int))

let formFieldValue: Option[int64] = value.map(val => val.int64)

result.name = fieldName
result.kind = FormFieldKind.INTSELECT
result.intSeqVal = formFieldValue
result.intOptions = options

FormField(
name: fieldName,
kind: FormFieldKind.INTSELECT,
intSeqVal: formFieldValue,
intOptions: options
)

# Maps `Level` to the `IntSelect` `FormField` and any value such a field might have is to be converted to an int on the form.
func toFormField*(value: Option[Level], fieldName: string): FormField =
Expand All @@ -111,11 +113,13 @@ nbCode:
let formFieldValue: Option[int64] = value.map(val => val.int64)

options.sort((opt1, opt2: IntOption) => cmp(opt1.name, opt2.name))

result.name = fieldName
result.kind = FormFieldKind.INTSELECT
result.intSeqVal = formFieldValue
result.intOptions = options

FormField(
name: fieldName,
kind: FormFieldKind.INTSELECT,
intSeqVal: formFieldValue,
intOptions: options
)

# Example Usage
putEnv("DB_HOST", ":memory:")
Expand Down
12 changes: 7 additions & 5 deletions tests/utils/testModels/creature.nim
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ func toFormField*(value: Option[CreatureFamily], fieldName: string): FormField =
options.add(IntOption(name: $enumValue, value: enumValue.int))

let formFieldValue: Option[int64] = value.map(val => val.int64)

result.name = fieldName
result.kind = FormFieldKind.INTSELECT
result.intSeqVal = formFieldValue
result.intOptions = options

FormField(
name: fieldName,
kind: FormFieldKind.INTSELECT,
intSeqVal: formFieldValue,
intOptions: options
)

proc afterCreateAction*(connection: DbConn, model: Creature): void =
echo fmt"Just created Creature '{model.name}'!"
Expand Down

0 comments on commit c3d050b

Please sign in to comment.