|
| 1 | +{# One file per Form #} |
| 2 | + |
| 3 | +{% for type in types.based.Form|!protocol|annotated:"form" %} |
| 4 | +// sourcery:file:Forms/{{ type.localName }}/{{ type.localName }}+Form.generated.swift |
| 5 | +import Forms |
| 6 | + |
| 7 | +extension {{ type.localName }} { |
| 8 | + {{ type.accessLevel }} var fields: [FieldType] { |
| 9 | + return [ |
| 10 | +{% for variable in type.variables where variable.typeName|hasPrefix:"FormField<" and variable|!annotated:"ignoreField" %} |
| 11 | + {{ variable.name }}{% if not forloop.last %},{% endif %} |
| 12 | +{% endfor %} |
| 13 | + ] |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +extension {{ type.localName }} { |
| 18 | +{% for variable in type.variables where variable.typeName|hasPrefix:"FormField<" and variable|!annotated:"ignoreField" %} |
| 19 | + {{ type.accessLevel }} var {{ variable.name|replace:"Field",""}}: {{variable.typeName.name|replace:"FormField<",""|replace:">",""}}? { |
| 20 | + return {{ variable.name }}.value |
| 21 | + } |
| 22 | +{% endfor %} |
| 23 | +} |
| 24 | + |
| 25 | +{% if type|!annotated:"ignoreAssertValues" %} |
| 26 | +extension {{ type.localName }} { |
| 27 | + {{ type.accessLevel }} func assertValues(errorOnNil: Error = Abort(.internalServerError)) throws -> ( |
| 28 | +{% for variable in type.variables where variable.typeName|hasPrefix:"FormField<" and variable|!annotated:"ignoreAssertValues" and variable|!annotated:"ignoreField" %} |
| 29 | + {%if not forloop.last or not forloop.first %}{{ variable.name|replace:"Field",""}}: {% endif %}{{variable.typeName.name|replace:"FormField<",""|replace:">",""}}{% if not forloop.last %},{% endif %} |
| 30 | +{% endfor %} |
| 31 | + ) { |
| 32 | + guard |
| 33 | +{% for variable in type.variables where variable.typeName|hasPrefix:"FormField<" and variable|!annotated:"ignoreAssertValues" and variable|!annotated:"ignoreField" %} |
| 34 | + let {{ variable.name|replace:"Field",""}} = {{ variable.name|replace:"Field",""}}{% if not forloop.last %},{% endif %} |
| 35 | +{% endfor %} |
| 36 | + else { |
| 37 | + throw errorOnNil |
| 38 | + } |
| 39 | + |
| 40 | + return ( |
| 41 | +{% for variable in type.variables where variable.typeName|hasPrefix:"FormField<" and variable|!annotated:"ignoreAssertValues" and variable|!annotated:"ignoreField" %} |
| 42 | + {{ variable.name|replace:"Field",""}}{% if not forloop.last %},{% endif %} |
| 43 | +{% endfor %} |
| 44 | + ) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +{% endif %} |
| 49 | +// sourcery:end |
| 50 | +{% endfor %} |
0 commit comments