Skip to content

Commit

Permalink
Merge branch 'main' of github.com:PhilippMDoerner/Snorlogue
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippMDoerner committed Oct 27, 2023
2 parents b87c629 + cf10230 commit 08e4cd7
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: Setup nim
uses: jiro4989/[email protected]
with:
nim-version: devel
nim-version: stable

- name: Install Packages
run: nimble install -y
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM bitnami/minideb

RUN apt-get update && apt-get install -y curl xz-utils gcc openssl ca-certificates git
RUN apt-get update && apt-get install -y curl xz-utils gcc openssl ca-certificates git libpcre3

WORKDIR /root/
RUN curl https://nim-lang.org/choosenim/init.sh -sSf | bash -s -- -y
Expand Down
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
55 changes: 26 additions & 29 deletions src/snorlogue/frontend/formCreateService.nim
Original file line number Diff line number Diff line change
Expand Up @@ -59,37 +59,34 @@ func toFormField*(value: Option[FilePath], fieldName: string): FormField =
func toIntSelectField(value: Option[int64], fieldName: string, options: var seq[
IntOption]): FormField =
options.sort((opt1, opt2: IntOption) => cmp(opt1.name, opt2.name))

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

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

# Disabled as a compiler bug causes this proc to be chosen also for string and int types instead of their appropriate overloads
# func toFormField*[T: enum or range](value: Option[T], fieldName: string): FormField =
# ## Converts an enum or range field on Model into a select
# ## `FormField<fieldTypes.html#FormField>`_ with int values.
# ##
# ## Note: This can not be split into 2 separate procs, as the compiler will
# ## immediately complain about ambiguity issues for the `TaintedString` type.
# var options: seq[IntOption] = @[]
# var formFieldValue: Option[int64] = none(int64)
# when T is enum:
# for enumValue in T:
# options.add(IntOption(name: $enumValue, value: enumValue.int))

# # This must be within when statement as otherwise it throws:
# # `type mismatch: got 'TaintedString' for 'val' but expected 'int64'`
# formFieldValue = value.map(val => val.int64)

# elif T is range:
# const rangeName = T.name
# for rangeVal in T.low..T.high:
# options.add(IntOption(name: fmt"{rangeName} {rangeVal}", value: rangeVal.int))

# formFieldValue = value.map(val => val.int64)

# toIntSelectField(formFieldValue, fieldName, options)
func toFormField*[T: enum or range](value: Option[T], fieldName: string): FormField =
## Converts an enum or range field on Model into a select
## `FormField<fieldTypes.html#FormField>`_ with int values.
##
## Note: This can not be split into 2 separate procs, as the compiler will
## immediately complain about ambiguity issues for the `TaintedString` type.
var options: seq[IntOption] = @[]
var formFieldValue: Option[int64] = none(int64)
when T is enum:
for enumValue in T:
options.add(IntOption(name: $enumValue, value: enumValue.int))

# This must be within when statement as otherwise it throws:
# `type mismatch: got 'TaintedString' for 'val' but expected 'int64'`
formFieldValue = value.map(val => val.int64)

elif T is range:
const rangeName = T.name
for rangeVal in T.low..T.high:
options.add(IntOption(name: fmt"{rangeName} {rangeVal}", value: rangeVal.int))

formFieldValue = value.map(val => val.int64)

toIntSelectField(formFieldValue, fieldName, options)

func toFormField*[T](value: T, fieldName: string): FormField =
## Helper proc to enable converting non-optional fields into
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 08e4cd7

Please sign in to comment.