diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index d6cc4b1..19b3480 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -31,7 +31,7 @@ jobs: - name: Setup nim uses: jiro4989/setup-nim-action@v1.4.5 with: - nim-version: devel + nim-version: stable - name: Install Packages run: nimble install -y diff --git a/Dockerfile b/Dockerfile index 5599bdb..be820e2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/docs/book/customDatatypes.nim b/docs/book/customDatatypes.nim index 50ffd2f..442d83d 100644 --- a/docs/book/customDatatypes.nim +++ b/docs/book/customDatatypes.nim @@ -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 = @@ -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:") diff --git a/src/snorlogue/frontend/formCreateService.nim b/src/snorlogue/frontend/formCreateService.nim index 6c14b07..232cc72 100644 --- a/src/snorlogue/frontend/formCreateService.nim +++ b/src/snorlogue/frontend/formCreateService.nim @@ -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`_ 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`_ 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 diff --git a/tests/utils/testModels/creature.nim b/tests/utils/testModels/creature.nim index 76a5fba..d4e537b 100644 --- a/tests/utils/testModels/creature.nim +++ b/tests/utils/testModels/creature.nim @@ -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}'!"