Merge pull request #36 from TouchInstinct/feature/swift_date_migration_part_2

SwiftDate 5.0 migration part 2
This commit is contained in:
Ivan Smolin 2018-07-31 19:39:11 +03:00 committed by GitHub
commit ed75f2571f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 5 deletions

View File

@ -6,12 +6,16 @@
import LeadKit
import SwiftDate
enum ApiDateFormat: String, LeadKit.DateFormat {
enum ApiDateFormat: String, DateFormat {
{% for format in dateFormats -%}
case {{ format.name }} = "{{ format.format }}"
{% endfor %}
var swiftDateFormat: DateToStringStyles {
var dateToStringFormat: DateToStringStyles {
return .custom(rawValue)
}
var stringToDateFormat: StringToDateStyles {
return .custom(rawValue)
}

View File

@ -32,8 +32,7 @@
{%- import _self as self -%}
{%- if field.type.type.baseTypeName == "DateTime" -%}
{{ self.formatNullableOrOptional(field.name, field.nullable, field.optional) -}}
.toString(ApiDateFormat.{{ dateFormatToName(field.type.dateFormat) }}.swiftDateFormat)
ApiDateFormattingService.string(from: {{ self.formatNullableOrOptional(field.name, field.nullable, field.optional) -}}, format: .{{ dateFormatToName(field.type.dateFormat) }})
{%- elseif field.type.type.baseTypeName == "Decimal" -%}
{{ self.formatNullableOrOptional(field.name, field.nullable, field.optional) -}}
.decimalValue
@ -103,7 +102,7 @@
{% macro initExpr(field) %}
{%- if field.type.type.baseTypeName == "DateTime" -%}
if let date = ApiDateFormattingService.date(from: {{ field.name }}, format: "{{ field.type.dateFormat }}") {
if let date = ApiDateFormattingService.date(from: {{ field.name }}, format: .{{ dateFormatToName(field.type.dateFormat) }}, parsedIn: nil) {
self.{{ field.name }} = date
} else {
throw LeadKitError.failedToDecode(reason: "init?(string:format:fromRegion:) returned nil")