Skip to content

Commit

Permalink
chore: refactor longest line
Browse files Browse the repository at this point in the history
  • Loading branch information
loks0n committed Oct 3, 2024
1 parent 04d8f94 commit 3670119
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions templates/dotnet/Package/Models/Model.cs.twig
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,34 @@ namespace {{ spec.title | caseUcfirst }}.Models
}

public static {{ definition.name | caseUcfirst | overrideIdentifier}} From(Dictionary<string, object> map) => new {{ definition.name | caseUcfirst | overrideIdentifier}}(
{%~ for property in definition.properties %}
{{ property.name | caseCamel | escapeKeyword | removeDollarSign }}: {% if property.sub_schema %}{% if property.type == 'array' %}((JArray)map["{{ property.name }}"]).ToObject<List<Dictionary<string, object>>>().Select(it => {{property.sub_schema | caseUcfirst | overrideIdentifier}}.From(map: it)).ToList(){% else %}{{property.sub_schema | caseUcfirst | overrideIdentifier}}.From(map: ((JObject)map["{{ property.name }}"]).ToObject<Dictionary<string, object>>()!){% endif %}{% else %}{% if property.type == 'array' %}((JArray)map["{{ property.name }}"]).ToObject<{{ property | typeName }}>(){% else %}{% if property.type == "integer" or property.type == "number" %}{% if not property.required %}map["{{ property.name }}"] == null ? null : {% endif %}Convert.To{% if property.type == "integer" %}Int64{% else %}Double{% endif %}(map["{{ property.name }}"]){% else %}{% if property.type == "boolean" %}({{ property | typeName }}{% if not property.required %}?{% endif %})map["{{ property.name }}"]{% else %}map{% if not property.required %}.TryGetValue("{{ property.name }}", out var {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}) ? {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}?.ToString() : null{% else %}["{{ property.name }}"]{% if not property.required %}?{% endif %}.ToString(){% endif %}{% endif %}{% endif %}{% endif %}{% endif %}{% if not loop.last or (loop.last and definition.additionalProperties) %},{% endif %}

{%~ endfor %}
{%~ if definition.additionalProperties %}
data: map
{%~ endif %}
{%- for property in definition.properties -%}
{{ property.name | caseCamel | escapeKeyword | removeDollarSign }}: {%- if property.sub_schema -%}
{%- if property.type == 'array' -%}
((JArray)map["{{ property.name }}"]).ToObject<List<Dictionary<string, object>>>().Select(it => {{property.sub_schema | caseUcfirst | overrideIdentifier}}.From(map: it)).ToList()
{%- else -%}
{{property.sub_schema | caseUcfirst | overrideIdentifier}}.From(map: ((JObject)map["{{ property.name }}"]).ToObject<Dictionary<string, object>>()!)
{%- endif -%}
{%- else -%}
{%- if property.type == 'array' -%}
((JArray)map["{{ property.name }}"]).ToObject<{{ property | typeName }}>()
{%- else -%}
{%- if property.type == "integer" or property.type == "number" -%}
{%- if not property.required -%}map["{{ property.name }}"] == null ? null : {%- endif -%}
Convert.To{%- if property.type == "integer" -%}Int64{%- else -%}Double{%- endif -%}(map["{{ property.name }}"])
{%- else -%}
{%- if property.type == "boolean" -%}
({{ property | typeName }}{%- if not property.required -%}?{%- endif -%})map["{{ property.name }}"]
{%- else -%}
map{%- if not property.required -%}.TryGetValue("{{ property.name }}", out var {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}) ? {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}?.ToString() : null{%- else -%}["{{ property.name }}"]{%- if not property.required -%}?{%- endif -%}.ToString()
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- if not loop.last or (loop.last and definition.additionalProperties) -%},{%- endif -%}
{%- endfor -%}
{%- if definition.additionalProperties -%}
data: map
{%- endif -%}
);

public Dictionary<string, object?> ToMap() => new Dictionary<string, object?>()
Expand Down

0 comments on commit 3670119

Please sign in to comment.