Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Commit

Permalink
Generate assertValues function
Browse files Browse the repository at this point in the history
- allow skipping fields using "ignoreField"
- allow skipping field from assertValues using "ignoreAssertValues"
  • Loading branch information
siemensikkema committed Mar 9, 2018
1 parent 0cd6eef commit d5a7848
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions Forms/Forms.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,46 @@
import Forms

extension {{ type.localName }} {
internal var fields: [FieldType] {
{{ type.accessLevel }} var fields: [FieldType] {
return [
{% for variable in type.variables where variable.typeName|hasPrefix:"FormField<" %}
{{ variable.name }},
{% for variable in type.variables where variable.typeName|hasPrefix:"FormField<" and variable|!annotated:"ignoreField" %}
{{ variable.name }}{% if not forloop.last %},{% endif %}
{% endfor %}
]
}
}

internal protocol {{ type.localName }}FieldsType {
{% for variable in type.variables where variable.typeName|hasPrefix:"FormField<" %}
var {{ variable.name|replace:"Field",""}}: {{variable.typeName.name|replace:"FormField<",""|replace:">",""}}? { get }
extension {{ type.localName }} {
{% for variable in type.variables where variable.typeName|hasPrefix:"FormField<" and variable|!annotated:"ignoreField" %}
{{ type.accessLevel }} var {{ variable.name|replace:"Field",""}}: {{variable.typeName.name|replace:"FormField<",""|replace:">",""}}? {
return {{ variable.name }}.value
}
{% endfor %}
}

extension {{ type.localName }}: {{ type.localName }}FieldsType {
{% for variable in type.variables where variable.typeName|hasPrefix:"FormField<" %}
var {{ variable.name|replace:"Field",""}}: {{variable.typeName.name|replace:"FormField<",""|replace:">",""}}? {
return {{ variable.name }}.value
}
{% if type|!annotated:"ignoreAssertValues" %}
extension {{ type.localName }} {
{{ type.accessLevel }} func assertValues(errorOnNil: Error = Abort(.internalServerError)) throws -> (
{% for variable in type.variables where variable.typeName|hasPrefix:"FormField<" and variable|!annotated:"ignoreAssertValues" and variable|!annotated:"ignoreField" %}
{%if not forloop.last or not forloop.first %}{{ variable.name|replace:"Field",""}}: {% endif %}{{variable.typeName.name|replace:"FormField<",""|replace:">",""}}{% if not forloop.last %},{% endif %}
{% endfor %}
) {
guard
{% for variable in type.variables where variable.typeName|hasPrefix:"FormField<" and variable|!annotated:"ignoreAssertValues" and variable|!annotated:"ignoreField" %}
let {{ variable.name|replace:"Field",""}} = {{ variable.name|replace:"Field",""}}{% if not forloop.last %},{% endif %}
{% endfor %}
else {
throw errorOnNil
}

return (
{% for variable in type.variables where variable.typeName|hasPrefix:"FormField<" and variable|!annotated:"ignoreAssertValues" and variable|!annotated:"ignoreField" %}
{{ variable.name|replace:"Field",""}}{% if not forloop.last %},{% endif %}
{% endfor %}
)
}
}

{% endif %}
// sourcery:end
{% endfor %}

0 comments on commit d5a7848

Please sign in to comment.