|
1 | 1 | {% macro sub_schema(property) %}{% if property.sub_schema %}{% if property.type == 'array' %}List<{{property.sub_schema | caseUcfirst | overrideIdentifier}}>{% else %}{{property.sub_schema | caseUcfirst | overrideIdentifier}}{% endif %}{% else %}{{property | typeName}}{% endif %}{% if not property.required %}?{% endif %}{% endmacro %} |
2 | 2 | {% macro property_name(definition, property) %}{{ property.name | caseUcfirst | removeDollarSign | escapeKeyword }}{% endmacro %} |
| 3 | +{% macro array_source(src, required) %}{% if required %}((IEnumerable<object>){{ src | raw }}){% else %}({{ src | raw }} as IEnumerable<object> ?? Array.Empty<object>()){% endif %}{% endmacro %} |
| 4 | +{%~ macro parse_primitive_array(items_type, src, required) -%} |
| 5 | + {{ _self.array_source(src, required) }}.Select(x => {% if items_type == "string" %}x?.ToString(){% elseif items_type == "integer" %}{% if not required %}x == null ? (long?)null : {% endif %}Convert.ToInt64(x){% elseif items_type == "number" %}{% if not required %}x == null ? (double?)null : {% endif %}Convert.ToDouble(x){% elseif items_type == "boolean" %}{% if not required %}x == null ? (bool?)null : {% endif %}(bool)x{% else %}x{% endif %}){% if required and items_type == "string" %}.Where(x => x != null){% endif %}.ToList()! |
| 6 | +{%- endmacro -%} |
| 7 | +{%~ macro parse_subschema_array(sub_schema_name, src, required) -%} |
| 8 | + {{ _self.array_source(src, required) }}.Select(it => {{ sub_schema_name | caseUcfirst | overrideIdentifier }}.From(map: (Dictionary<string, object>)it)).ToList() |
| 9 | +{%- endmacro -%} |
3 | 10 | using System; |
4 | 11 | using System.Linq; |
5 | 12 | using System.Collections.Generic; |
@@ -38,25 +45,49 @@ namespace {{ spec.title | caseUcfirst }}.Models |
38 | 45 |
|
39 | 46 | public static {{ definition.name | caseUcfirst | overrideIdentifier }} From(Dictionary<string, object> map) => new {{ definition.name | caseUcfirst | overrideIdentifier }}( |
40 | 47 | {%~ for property in definition.properties %} |
| 48 | + {%~ set v = 'v' ~ loop.index0 %} |
| 49 | + {%~ set mapAccess = 'map["' ~ property.name ~ '"]' %} |
41 | 50 | {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}:{{' '}} |
42 | | - {%- if not property.required -%}map.ContainsKey("{{ property.name }}") ? {% endif %} |
| 51 | + {%- if not property.required -%}map.TryGetValue("{{ property.name }}", out var {{ v }}) ? {% endif %} |
43 | 52 | {%- if property.sub_schema %} |
44 | 53 | {%- if property.type == 'array' -%} |
45 | | - ((IEnumerable<object>)map["{{ property.name }}"]).Select(it => {{ property.sub_schema | caseUcfirst | overrideIdentifier }}.From(map: (Dictionary<string, object>)it)).ToList() |
| 54 | + {%- if property.required -%} |
| 55 | + {{ _self.parse_subschema_array(property.sub_schema, mapAccess, true) }} |
| 56 | + {%- else -%} |
| 57 | + {{ _self.parse_subschema_array(property.sub_schema, v, false) }} |
| 58 | + {%- endif %} |
46 | 59 | {%- else -%} |
47 | | - {{ property.sub_schema | caseUcfirst | overrideIdentifier }}.From(map: (Dictionary<string, object>)map["{{ property.name }}"]) |
| 60 | + {%- if property.required -%} |
| 61 | + {{ property.sub_schema | caseUcfirst | overrideIdentifier }}.From(map: (Dictionary<string, object>){{ mapAccess | raw }}) |
| 62 | + {%- else -%} |
| 63 | + ({{ v }} as Dictionary<string, object>) is { } obj |
| 64 | + ? {{ property.sub_schema | caseUcfirst | overrideIdentifier }}.From(map: obj) |
| 65 | + : null |
| 66 | + {%- endif %} |
48 | 67 | {%- endif %} |
49 | 68 | {%- else %} |
50 | 69 | {%- if property.type == 'array' -%} |
51 | | - ((IEnumerable<object>)map["{{ property.name }}"]).Select(x => {% if property.items.type == "string" %}x?.ToString(){% elseif property.items.type == "integer" %}{% if not property.required %}x == null ? (long?)null : {% endif %}Convert.ToInt64(x){% elseif property.items.type == "number" %}{% if not property.required %}x == null ? (double?)null : {% endif %}Convert.ToDouble(x){% elseif property.items.type == "boolean" %}{% if not property.required %}x == null ? (bool?)null : {% endif %}(bool)x{% else %}x{% endif %}).{% if property.items.type == "string" and property.required %}Where(x => x != null).{% endif %}ToList()! |
| 70 | + {%- if property.required -%} |
| 71 | + {{ _self.parse_primitive_array(property.items.type, mapAccess, true) }} |
| 72 | + {%- else -%} |
| 73 | + {{ _self.parse_primitive_array(property.items.type, v, false) }} |
| 74 | + {%- endif -%} |
52 | 75 | {%- else %} |
53 | 76 | {%- if property.type == "integer" or property.type == "number" %} |
54 | | - {%- if not property.required -%}map["{{ property.name }}"] == null ? null : {% endif %}Convert.To{% if property.type == "integer" %}Int64{% else %}Double{% endif %}(map["{{ property.name }}"]) |
| 77 | + {%- if not property.required -%}Convert.To{% if property.type == "integer" %}Int64{% else %}Double{% endif %}({{ v }}){% else %}Convert.To{% if property.type == "integer" %}Int64{% else %}Double{% endif %}({{ mapAccess | raw }}){%- endif %} |
55 | 78 | {%- else %} |
56 | 79 | {%- if property.type == "boolean" -%} |
57 | | - ({{ property | typeName }}{% if not property.required %}?{% endif %})map["{{ property.name }}"] |
| 80 | + {%- if not property.required -%} |
| 81 | + ({{ property | typeName }}?){{ v }} |
| 82 | + {%- else -%} |
| 83 | + ({{ property | typeName }}){{ mapAccess | raw }} |
| 84 | + {%- endif %} |
58 | 85 | {%- else -%} |
59 | | - map["{{ property.name }}"]{% if not property.required %}?{% endif %}.ToString() |
| 86 | + {%- if not property.required -%} |
| 87 | + {{ v }}?.ToString() |
| 88 | + {%- else -%} |
| 89 | + {{ mapAccess | raw }}.ToString() |
| 90 | + {%- endif %} |
60 | 91 | {%- endif %} |
61 | 92 | {%~ endif %} |
62 | 93 | {%~ endif %} |
|
0 commit comments