|
| 1 | +{% macro sub_schema(property) %}{% if property.sub_schema %}{% if property.type == 'array' %}List<{{property.sub_schema | ucFirstAndEscape}}>{% else %}{{property.sub_schema | ucFirstAndEscape }}{% endif %}{% else %}{{property.type | typeName}}{% endif %}{% endmacro %} |
| 2 | +part of {{ language.params.packageName }}.models; |
| 3 | + |
| 4 | +/// {{ definition.description }} |
| 5 | +class {{ definition.name | ucFirstAndEscape }} { |
| 6 | +{% for property in definition.properties %} |
| 7 | + /// {{ property.description }} |
| 8 | + final {% if not property.required %}{{_self.sub_schema(property)}}? {{ property.name | escapeKeyword }}{% else %}{{_self.sub_schema(property)}} {{ property.name | escapeKeyword }}{% endif %}; |
| 9 | +{% endfor %} |
| 10 | +{% if definition.additionalProperties %} |
| 11 | + final Map<String, dynamic> data; |
| 12 | +{% endif %} |
| 13 | + |
| 14 | + {{ definition.name | ucFirstAndEscape}}({ |
| 15 | +{% for property in definition.properties %}{% if property.required %} |
| 16 | + required {% endif %}this.{{ property.name | escapeKeyword }}, |
| 17 | +{% endfor %} |
| 18 | +{% if definition.additionalProperties %} |
| 19 | + required this.data, |
| 20 | +{% endif %} |
| 21 | + }); |
| 22 | + |
| 23 | + factory {{ definition.name | ucFirstAndEscape}}.fromMap(Map<String, dynamic> map) { |
| 24 | + return {{ definition.name | ucFirstAndEscape }}( |
| 25 | +{% for property in definition.properties %} |
| 26 | + {{ property.name | escapeKeyword }}: {% if property.sub_schema %}{% if property.type == 'array' %}List<{{property.sub_schema | ucFirstAndEscape}}>.from(map['{{property.name | escapeDollarSign }}'].map((p) => {{property.sub_schema | ucFirstAndEscape}}.fromMap(p))){% else %}{{property.sub_schema | ucFirstAndEscape}}.fromMap(map['{{property.name | escapeDollarSign }}']){% endif %}{% else %}map['{{property.name | escapeDollarSign }}']{% if property.type == "number" %}.toDouble(){% endif %}{% if property.type == "string" %}.toString(){% endif %}{% endif %}, |
| 27 | +{% endfor %} |
| 28 | +{% if definition.additionalProperties %} |
| 29 | + data: map, |
| 30 | +{% endif %} |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + Map<String, dynamic> toMap() { |
| 35 | + return { |
| 36 | +{% for property in definition.properties %} |
| 37 | + "{{ property.name | escapeDollarSign }}": {% if property.sub_schema %}{% if property.type == 'array' %}{{property.name | escapeKeyword}}.map((p) => p.toMap()){% else %}{{property.name | escapeKeyword}}.toMap(){% endif %}{% else %}{{property.name | escapeKeyword }}{% endif %}, |
| 38 | +{% endfor %} |
| 39 | +{% if definition.additionalProperties %} |
| 40 | + "data": data, |
| 41 | +{% endif %} |
| 42 | + }; |
| 43 | + } |
| 44 | +{% if definition.additionalProperties %} |
| 45 | + |
| 46 | + T convertTo<T>(T Function(Map) fromJson) => fromJson(data); |
| 47 | +{% endif %} |
| 48 | +{% for property in definition.properties %} |
| 49 | +{% if property.sub_schema %} |
| 50 | +{% for def in spec.definitions %} |
| 51 | +{% if def.name == property.sub_schema and def.additionalProperties and property.type == 'array' %} |
| 52 | + |
| 53 | + List<T> convertTo<T>(T Function(Map) fromJson) => |
| 54 | + {{property.name}}.map((d) => d.convertTo<T>(fromJson)).toList(); |
| 55 | +{% endif %} |
| 56 | +{% endfor %} |
| 57 | +{% endif %} |
| 58 | +{% endfor %} |
| 59 | +} |
0 commit comments