fix optional decoding
This commit is contained in:
parent
eef236a25d
commit
bd1e64caa1
|
|
@ -74,7 +74,7 @@
|
|||
{% elseif field.type.type.baseTypeName == "Decimal" %}
|
||||
{{ self.decodeComplexField(field, "Decimal") }}
|
||||
{%- else -%}
|
||||
self.{{ field.name }} = {{ self.formatNullableOrOptional("try", field.nullable, field.optional) }} container.decode({{ self.formatValueType(field.type.type) }}.self, forKey: .{{ field.name }})
|
||||
self.{{ field.name }} = try container.{{- self.formatOptionalDecode(field) -}}({{ self.formatValueType(field.type.type) }}.self, forKey: .{{ field.name }})
|
||||
{%- endif -%}
|
||||
{% endmacro %}
|
||||
|
||||
|
|
@ -82,7 +82,7 @@
|
|||
{%- import _self as self -%}
|
||||
|
||||
{%- if field.optional or field.nullable -%}
|
||||
if let {{ field.name }} = try? container.decode({{ decodingType }}.self, forKey: .{{ field.name }}) {
|
||||
if let {{ field.name }} = try container.decodeIfPresent({{ decodingType }}.self, forKey: .{{ field.name }}) {
|
||||
{{ self.initExpr(field) }}
|
||||
} else {
|
||||
self.{{ field.name }} = nil
|
||||
|
|
@ -93,6 +93,14 @@
|
|||
{%- endif -%}
|
||||
{% endmacro %}
|
||||
|
||||
{% macro formatOptionalDecode(field) %}
|
||||
{%- if field.optional or field.nullable -%}
|
||||
decodeIfPresent
|
||||
{%- else -%}
|
||||
decode
|
||||
{%- endif -%}
|
||||
{% endmacro %}
|
||||
|
||||
{% macro initExpr(field) %}
|
||||
{%- if field.type.type.baseTypeName == "DateTime" -%}
|
||||
if let date = DateInRegion(string: {{ field.name }}, format: ApiDateFormat.{{ dateFormatToName(field.type.dateFormat) }}.swiftDateFormat, fromRegion: ApiDateFormattingService.shared.currentRegion) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue