Compare commits

..

1 Commits

Author SHA1 Message Date
mrtokii ae31e089c4 Change type output in Swift template 2019-10-29 23:19:56 +03:00
28 changed files with 78 additions and 275 deletions

View File

@ -95,10 +95,10 @@ public class {% include 'blocks/class/classtype.twig' with { type: type } %} ext
}
{%- if (allFieldsOrdered is not empty) %}
//TODO: if TItems instance of arrayList then new ArrayList
{%- if (storageAttributes is not null) %}
@Ignore
{%- endif %}
//TODO: if TItems instance of arrayList then new ArrayList
{%- if (storageAttributes is not null) %}
@Ignore
{%- endif %}
public {{ type.baseTypeName }}(
{%- include 'blocks/class/init-parameters-fields.twig' with { fields: allFieldsOrdered } %}
) {

View File

@ -2,6 +2,6 @@
{%- if fields is not empty -%}
{%- for field in fields %}
{{ utils.writeNullCheckAnnotation(field.type.type.baseTypeName, field.nullable, field.optional) }} final {{ utils.formatValueType(field.type.type, field.nullable, field.optional) }} {{ field.name }} {%- if not (loop.last) %}, {% endif %}
{{ utils.writeNullCheckAnnotation(field.type.type.baseTypeName, field.nullable, field.optional) }} final {{ utils.formatValueType(field.type.type, field.nullable, field.optional) }} {{ field.name }} {%- if not (loop.last) %}, {% endif %}
{%- endfor -%}
{%- endif -%}

View File

@ -1,44 +0,0 @@
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonEncodingException
import com.squareup.moshi.JsonReader
import com.squareup.moshi.JsonWriter
import java.util.Arrays
abstract class AbstractCalendarJsonAdapter<T, FORMATTER>(
private val formats: Array<String>
) : JsonAdapter<T>() {
companion object {
private const val CANT_FIND_ANY_FORMATTERS = "Can't find any DateTimeFormatter"
}
private val formatters = formats.map(::createFormatter)
abstract fun createFormatter(pattern: String): FORMATTER
override fun fromJson(reader: JsonReader): T? {
val dateTimeString = reader.nextString()
formatters.forEachIndexed { index, dateTimeFormatter ->
try {
return fromJsonInner(dateTimeString, dateTimeFormatter)
} catch (e: IllegalArgumentException) {
if (index == formatters.lastIndex) {
throw JsonEncodingException("$dateTimeString doesn't fit any of the formats ${Arrays.toString(formats)}")
}
}
}
throw IllegalArgumentException(CANT_FIND_ANY_FORMATTERS)
}
override fun toJson(writer: JsonWriter, value: T?) {
if (formatters.isEmpty()) throw IllegalArgumentException(CANT_FIND_ANY_FORMATTERS)
value?.let {
writer.value(toJsonInner(value, formatters.first()))
} ?: throw JsonEncodingException("Value can't be null")
}
abstract fun fromJsonInner(value: String, dateTimeFormatter: FORMATTER): T
abstract fun toJsonInner(value: T?, dateTimeFormatter: FORMATTER): String?
}

View File

@ -1,14 +0,0 @@
import java.time.format.DateTimeFormatter
import java.time.temporal.TemporalAccessor
abstract class AbstractJavaTimeJsonAdapter<T : TemporalAccessor>(
formats: Array<String>
) : AbstractCalendarJsonAdapter<T, DateTimeFormatter>(formats) {
override fun createFormatter(pattern: String): DateTimeFormatter = DateTimeFormatter.ofPattern(pattern)
override fun toJsonInner(value: T?, dateTimeFormatter: DateTimeFormatter): String? {
value ?: return null
return dateTimeFormatter.format(value)
}
}

View File

@ -1,8 +0,0 @@
import org.joda.time.format.DateTimeFormat
import org.joda.time.format.DateTimeFormatter
abstract class AbstractJodaTimeJsonAdapter<T>(
formats: Array<String>
) : AbstractCalendarJsonAdapter<T, DateTimeFormatter>(formats) {
override fun createFormatter(pattern: String): DateTimeFormatter = DateTimeFormat.forPattern(pattern)
}

View File

@ -1,14 +0,0 @@
import android.graphics.Color
import com.squareup.moshi.FromJson
import com.squareup.moshi.ToJson
class ColorAdapter {
@ToJson
fun toJson(@HexColor color: Int): String = "#${Integer.toHexString(color)}"
@FromJson
@HexColor
fun fromJson(color: String): Int = Color.parseColor(color)
}

View File

@ -1,22 +0,0 @@
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi
import java.lang.reflect.Type
class DateFactory : JsonAdapter.Factory {
override fun create(type: Type, annotations: Set<out Annotation>, moshi: Moshi): JsonAdapter<*>? {
val typeName: String = when(type) {
is Class<*> -> type.canonicalName ?: type.toString()
else -> type.toString()
}
return getCalendarAdapter(typeName, getPatterns(annotations))
}
private fun getPatterns(annotations: Set<out Annotation>) = annotations
.mapNotNull { it as? Format }
.firstOrNull()
?.patterns
?: throw IllegalArgumentException("You should use Format annotation for DateTime and LocalDate fields")
}

View File

@ -1,16 +0,0 @@
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormatter
class DateTimeJodaTimeJsonAdapter(
formats: Array<String>
) : AbstractJodaTimeJsonAdapter<DateTime>(formats) {
override fun fromJsonInner(value: String, dateTimeFormatter: DateTimeFormatter): DateTime {
return DateTime.parse(value, dateTimeFormatter)
}
override fun toJsonInner(value: DateTime?, dateTimeFormatter: DateTimeFormatter): String? {
return value?.toString(dateTimeFormatter)
}
}

View File

@ -1,5 +0,0 @@
import com.squareup.moshi.JsonQualifier
@JsonQualifier
@Target(AnnotationTarget.FIELD)
annotation class Format(val patterns: Array<String>)

View File

@ -1,5 +0,0 @@
import com.squareup.moshi.JsonQualifier
@JsonQualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class HexColor

View File

@ -1,12 +0,0 @@
import java.time.LocalDate
import java.time.format.DateTimeFormatter
class LocalDateJavaTimeJsonAdapter(
formats: Array<String>
) : AbstractJavaTimeJsonAdapter<LocalDate>(formats) {
override fun fromJsonInner(value: String, dateTimeFormatter: DateTimeFormatter): LocalDate {
return LocalDate.parse(value, dateTimeFormatter)
}
}

View File

@ -1,16 +0,0 @@
import org.joda.time.LocalDate
import org.joda.time.format.DateTimeFormatter
class LocalDateJodaTimeJsonAdapter(
formats: Array<String>
) : AbstractJodaTimeJsonAdapter<LocalDate>(formats) {
override fun fromJsonInner(value: String, dateTimeFormatter: DateTimeFormatter): LocalDate {
return LocalDate.parse(value, dateTimeFormatter)
}
override fun toJsonInner(value: LocalDate?, dateTimeFormatter: DateTimeFormatter): String? {
return value?.toString(dateTimeFormatter)
}
}

View File

@ -1,13 +0,0 @@
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
class ZonedDateTimeJavaTimeJsonAdapter(
formats: Array<String>
) : AbstractJavaTimeJsonAdapter<ZonedDateTime>(formats) {
override fun fromJsonInner(value: String, dateTimeFormatter: DateTimeFormatter): ZonedDateTime {
return ZonedDateTime.parse(value, dateTimeFormatter)
}
}

View File

@ -9,7 +9,6 @@ import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.annotation.JsonFormat
import java.math.BigDecimal
import java.time.LocalDate
import java.time.ZonedDateTime
{% if (description is not empty) -%}

View File

@ -4,13 +4,10 @@
{%- if field.type.type.baseTypeName == "DateTime" %}
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "{{ utils.getDateFormat(field) }}", timezone = "utc")
{%- endif %}
{%- if field.type.type.baseTypeName == "Date" %}
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "{{ utils.getDateFormat(field) }}")
{%- endif %}
{%- if field.optional %}
@JsonInclude(JsonInclude.Include.NON_NULL)
{%- elseif field.nullable %}
{%- if field.nullable %}
@JsonInclude(JsonInclude.Include.ALWAYS)
{%- elseif field.optional %}
@JsonInclude(JsonInclude.Include.NON_NULL)
{%- endif %}
{% if not (field in superclassesFields) %}val {% endif %}{{ field.name }}: {{ utils.formatValueType(field.type.type, field.nullable, field.optional) }}{{ utils.writeNullCheckMark(field.nullable, field.optional) }} {%- if not (loop.last) %},{% endif %}{{ utils.addDescription(field) }}
{%- endfor -%}

View File

@ -14,8 +14,6 @@
Boolean
{%- elseif valueType.baseTypeName == "DateTime" -%}
ZonedDateTime
{%- elseif valueType.baseTypeName == "Date" -%}
LocalDate
{%- elseif valueType.baseTypeName == "Decimal" -%}
BigDecimal
{%- elseif valueType.baseTypeName == "Map" -%}

View File

@ -6,28 +6,28 @@
import LeadKit
import SwiftDate
public enum ApiDateFormat: String, DateFormat {
enum ApiDateFormat: String, DateFormat {
{% for format in dateFormats -%}
case {{ format.name }} = "{{ format.format }}"
{% endfor %}
public var dateToStringFormat: DateToStringStyles {
var dateToStringFormat: DateToStringStyles {
return .custom(rawValue)
}
public var stringToDateFormat: StringToDateStyles {
var stringToDateFormat: StringToDateStyles {
return .custom(rawValue)
}
}
public final class ApiDateFormattingService: DateFormattingService, Singleton {
final class ApiDateFormattingService: DateFormattingService, Singleton {
public typealias DateFormatType = ApiDateFormat
typealias DateFormatType = ApiDateFormat
public static let shared = ApiDateFormattingService()
var currentRegion: Region = .local
public var currentRegion: Region = .local
static let shared = ApiDateFormattingService()
private init() {}

View File

@ -3,11 +3,11 @@
import LeadKit
import Foundation
public enum ApiNumberFormat: NumberFormat {
enum ApiNumberFormat: NumberFormat {
case decimal
public var numberFormatter: NumberFormatter {
var numberFormatter: NumberFormatter {
let numberFormatter = NumberFormatter()
numberFormatter.decimalSeparator = "."
numberFormatter.minimumIntegerDigits = 1
@ -17,17 +17,17 @@ public enum ApiNumberFormat: NumberFormat {
}
}
public final class ApiNumberFormattingService: NumberFormattingService, Singleton {
final class ApiNumberFormattingService: NumberFormattingService, Singleton {
public typealias NumberFormatType = ApiNumberFormat
typealias NumberFormatType = ApiNumberFormat
public static let shared = ApiNumberFormattingService()
let formatters = computedFormatters
public let formatters = computedFormatters
static let shared = ApiNumberFormattingService()
private init() {}
public func decimalNumber(from string: String, format: ApiNumberFormat) -> NSDecimalNumber? {
func decimalNumber(from string: String, format: ApiNumberFormat) -> NSDecimalNumber? {
guard let number = number(from: string, format: format) else {
return nil
}
@ -37,7 +37,7 @@ public final class ApiNumberFormattingService: NumberFormattingService, Singleto
extension ApiNumberFormattingService {
public static func decimalNumber(from string: String, format: ApiNumberFormat) -> NSDecimalNumber? {
static func decimalNumber(from string: String, format: ApiNumberFormat) -> NSDecimalNumber? {
return shared.decimalNumber(from: string, format: format)
}
}

View File

@ -14,12 +14,12 @@ import SwiftDate
import LeadKit
/// {{ description }}
public {% if (not hasChilds) -%}final {% endif %}class {{ classType }}: {{ parentClassType }} {
{% if (not hasChilds) -%}final {% endif %}class {{ classType }}: {{ parentClassType }} {
{% include 'blocks/class/coding-keys.twig' with { fields: fields } %}
{% include 'blocks/class/fields.twig' with { fields: fields } %}
// MARK: - Initializers
public {% if (hasParent and (fields is empty)) %} override {% endif %}init({%- include 'blocks/class/init-parameters-fields.twig' with { fields: allFieldsOrdered } -%}) {
{% if (hasParent and (fields is empty)) %} override {% endif %}init({%- include 'blocks/class/init-parameters-fields.twig' with { fields: allFieldsOrdered } -%}) {
{%- include 'blocks/class/fields-initialization.twig' with { fields: fields } -%}
{% if hasParent %}
super.init({%- include 'blocks/class/fields-super-initialization.twig' with { fields: superclassesFields } -%})
@ -27,7 +27,7 @@ public {% if (not hasChilds) -%}final {% endif %}class {{ classType }}: {{ paren
}
{% if hasParent or fields is not empty %}
public required init(from decoder: Decoder) throws {
required init(from decoder: Decoder) throws {
{%- if fields is not empty %}
{% include 'blocks/class/fields-initialization-from-decoder.twig' with { fields: fields} %}
{%- endif -%}
@ -37,7 +37,7 @@ public {% if (not hasChilds) -%}final {% endif %}class {{ classType }}: {{ paren
}
{% endif %}
public {% if hasParent -%}override {% endif %}func encode(to encoder: Encoder) throws {
{% if hasParent -%}override {% endif %}func encode(to encoder: Encoder) throws {
{%- if fields is not empty %}
{% include 'blocks/class/fields-encode-to-encoder.twig' with { fields: fields} %}
{%- endif -%}
@ -47,7 +47,7 @@ public {% if (not hasChilds) -%}final {% endif %}class {{ classType }}: {{ paren
}
{% if classAndFieldsHaveNotGenericsOrNonEqutableCollections -%}
public func isEqual(to other: {{ classType }}?) -> Bool {
func isEqual(to other: {{ classType }}?) -> Bool {
{% if (fields is empty) and (not hasParent) %}
return false
{% else %}
@ -64,7 +64,7 @@ public {% if (not hasChilds) -%}final {% endif %}class {{ classType }}: {{ paren
{%- if (not hasChilds) and classAndFieldsHaveNotGenericsOrNonEqutableCollections -%}
extension {{ type.baseTypeName }}: Equatable {
public static func ==(lhs: {{ classType }}, rhs: {{ classType }}) -> Bool {
static func ==(lhs: {{ classType }}, rhs: {{ classType }}) -> Bool {
return lhs.isEqual(to: rhs)
}
}
@ -74,7 +74,7 @@ extension {{ type.baseTypeName }}: Equatable {
{%- if classAndFieldsHaveNotGenericsOrNonEqutableCollections -%}
extension {{ type.baseTypeName }} {
public static let new{{ type.baseTypeName }} = {{ type.baseTypeName }}({%- include 'blocks/class/fields-initialization-default-values.twig' with { fields: allFieldsOrdered } -%})
static let new{{ type.baseTypeName }} = {{ type.baseTypeName }}({%- include 'blocks/class/fields-initialization-default-values.twig' with { fields: allFieldsOrdered } -%})
{% include 'blocks/class/copy-declaration.twig' with { hasChilds: hasChilds, type: type, fields: allFieldsOrdered } %}
}

View File

@ -6,7 +6,7 @@
{% for value in values -%}
/// - {{ utils.decapitalize(value.name) }}: {{ value.description }}
{% endfor -%}
public enum {{ name }}: {{ enumutils.enumType(valuesTypes) }}, Codable, RawRepresentable, CaseIterable {
enum {{ name }}: {{ enumutils.enumType(valuesTypes) }}, Codable, RawRepresentable, CaseIterable {
{% include 'blocks/enum/cases.twig' with { values: values } %}
}
{{ "\n" }}

View File

@ -5,7 +5,7 @@ import Alamofire
{% set serviceName = concat(networkServiceName, "NetworkService") -%}
{% set protocolName = concat(networkServiceName, "NetworkProtocol") -%}
public protocol {{ protocolName }} {
protocol {{ protocolName }} {
func apiRequest<T: Decodable>(with parametersSingle: Single<ApiRequestParameters>, additionalValidStatusCodes: Set<Int>, decoder: JSONDecoder) -> Single<T>
func deferredApiRequestParameters(relativeUrl: String,
@ -19,25 +19,25 @@ public protocol {{ protocolName }} {
{% endfor %}
}
open class {{ serviceName }}: NetworkService, {{ protocolName }} {
class {{ serviceName }}: NetworkService, {{ protocolName }} {
public static let apiBaseUrl = "{{ apiUrl }}"
static let apiBaseUrl = "{{ apiUrl }}"
public convenience init() {
convenience init() {
self.init(configuration: NetworkServiceConfiguration(baseUrl: {{ serviceName }}.apiBaseUrl))
}
open func apiRequest<T: Decodable>(with parametersSingle: Single<ApiRequestParameters>, additionalValidStatusCodes: Set<Int> = [], decoder: JSONDecoder = JSONDecoder()) -> Single<T> {
func apiRequest<T: Decodable>(with parametersSingle: Single<ApiRequestParameters>, additionalValidStatusCodes: Set<Int> = [], decoder: JSONDecoder = JSONDecoder()) -> Single<T> {
return parametersSingle.flatMap {
self.rxRequest(with: $0, additionalValidStatusCodes: additionalValidStatusCodes, decoder: decoder).map { $0.model }.asSingle()
}
}
open func deferredApiRequestParameters(relativeUrl: String,
method: HTTPMethod = .get,
parameters: Parameters? = nil,
requestEncoding: ParameterEncoding? = nil,
requestHeaders: HTTPHeaders? = nil) -> Single<ApiRequestParameters> {
func deferredApiRequestParameters(relativeUrl: String,
method: HTTPMethod = .get,
parameters: Parameters? = nil,
requestEncoding: ParameterEncoding? = nil,
requestHeaders: HTTPHeaders? = nil) -> Single<ApiRequestParameters> {
return .deferredJust {
self.configuration.apiRequestParameters(relativeUrl: relativeUrl,
method: method,

View File

@ -1,6 +1,6 @@
{%- import '../../macroses/common.utils.twig' as utils -%}
public func copy{%- if hasChilds -%}{{ type.baseTypeName }}{%- endif -%}With({%- include '../class/nullable-parameters-fields.twig' with { fields: allFieldsOrdered } -%}) -> {{ type.baseTypeName }} {
func copy{%- if hasChilds -%}{{ type.baseTypeName }}{%- endif -%}With({%- include '../class/nullable-parameters-fields.twig' with { fields: allFieldsOrdered } -%}) -> {{ type.baseTypeName }} {
return {{ type.baseTypeName }}({% include '../class/fields-optional-initialization.twig' with { fields: allFieldsOrdered } %})
}
@ -14,7 +14,7 @@
{% if containsOptionalFields %}
public func copy{%- if hasChilds -%}{{ type.baseTypeName }}{%- endif -%}Without({%- include '../class/bool-parameters-fields.twig' with { fields: allFieldsOrdered } -%}) -> {{ type.baseTypeName }} {
func copy{%- if hasChilds -%}{{ type.baseTypeName }}{%- endif -%}Without({%- include '../class/bool-parameters-fields.twig' with { fields: allFieldsOrdered } -%}) -> {{ type.baseTypeName }} {
return {{ type.baseTypeName }}({%- include '../class/fields-without-initialization.twig' with { fields: allFieldsOrdered } -%})
}
{% endif %}

View File

@ -4,6 +4,6 @@
// MARK: - Fields
{% for field in fields %}
/// {{ field.description }}
public let {{ field.name }}: {{ utils.formatNullableOrOptional(utils.formatValueType(field.type.type), field.nullable, field.optional) }}
let {{ field.name }}: {{ utils.formatNullableOrOptional(utils.formatValueType(field.type.type), field.nullable, field.optional) }}
{% endfor -%}
{% endif %}

View File

@ -9,4 +9,4 @@
{%- set funcName = utils.decapitalize(method.name) -%}
{{ isStatic ? "static " : "" }}func {{ funcName }}({%- if hasBody -%}{{ bodyParamName }}: {{ bodyTypeName }},{{ " " }}{%- endif -%}requestEncoding: ParameterEncoding?, requestHeaders: HTTPHeaders?, additionalValidStatusCodes: Set<Int>) -> Single<{{ utils.formatValueType(method.apiResponseType) }}>
{{ isStatic ? "static " : "" }}func {{ funcName }}({%- if hasBody -%}{{ bodyParamName }}: {{ bodyTypeName }},{{ " " }}{%- endif -%}requestEncoding: ParameterEncoding?, requestHeaders: HTTPHeaders?, additionalValidStatusCodes: Set<Int>) -> Single<{{ method.responseType.type.typeName }}>

View File

@ -11,10 +11,10 @@
{%- set funcName = utils.decapitalize(method.name) -%}
/// {{ method.description }}
public {{ isStatic ? "static " : "" }}func {{ funcName }}({%- if hasBody -%}{{ bodyParamName }}: {{ bodyTypeName }},{{ "\n " }}{%- endif -%}
{{ isStatic ? "static " : "" }}func {{ funcName }}({%- if hasBody -%}{{ bodyParamName }}: {{ bodyTypeName }},{{ "\n " }}{%- endif -%}
requestEncoding: ParameterEncoding? = nil,
requestHeaders: HTTPHeaders? = nil,
additionalValidStatusCodes: Set<Int> = []) -> Single<{{ utils.formatValueType(method.apiResponseType) }}> {
additionalValidStatusCodes: Set<Int> = []) -> Single<{{ method.responseType.type.typeName }}> {
{% if isStatic -%}
return shared.{{ funcName }}({%- if hasBody -%}{{ bodyParamName }}: {{ bodyParamName }},{{ "\n " }}{%- endif -%}

View File

@ -12,42 +12,40 @@
{% macro formatValueType(valueType) %}
{%- import _self as self -%}
{%- set baseTypeName = valueType.baseTypeName -%}
{%- if baseTypeName == "Array" -%}
{%- if valueType.baseTypeName == "Array" -%}
[{{ self.formatValueType(valueType.itemsType) }}]
{%- elseif baseTypeName == "Map" -%}
{%- elseif valueType.baseTypeName == "Map" -%}
[{{ self.formatValueType(valueType.keysType) }}: {{ self.formatValueType(valueType.valuesType) }}]
{%- elseif baseTypeName == "Date" or baseTypeName == "DateTime" or baseTypeName == "DateTimeTimestamp" -%}
{%- elseif valueType.baseTypeName == "DateTime" or valueType.baseTypeName == "DateTimeTimestamp" -%}
DateInRegion
{%- elseif baseTypeName == "Long" -%}
{%- elseif valueType.baseTypeName == "Long" -%}
Int64
{%- elseif baseTypeName == "Decimal" or baseTypeName == "StringDecimal" -%}
{%- elseif valueType.baseTypeName == "Decimal" or valueType.baseTypeName == "StringDecimal" -%}
NSDecimalNumber
{%- elseif baseTypeName == "Url" -%}
{%- elseif valueType.baseTypeName == "Url" -%}
URL
{%- elseif baseTypeName == "Color" -%}
{%- elseif valueType.baseTypeName == "Color" -%}
UIColor
{%- else -%}
{{ baseTypeName }}
{{ valueType.typeName }}
{%- endif -%}
{% endmacro %}
{% macro formatEncodingValue(field, isStrongLinkCaptured) %}
{%- import _self as self -%}
{%- set baseTypeName = field.type.type.baseTypeName -%}
{%- if baseTypeName == "Date" or baseTypeName == "DateTime" -%}
{%- if field.type.type.baseTypeName == "DateTime" -%}
{{- self.formatEncodingDate(field) -}}
{%- elseif baseTypeName == "StringDecimal" -%}
{%- elseif field.type.type.baseTypeName == "StringDecimal" -%}
{{- self.formatEncodingStringDecimal(field) -}}
{%- elseif baseTypeName == "Decimal" -%}
{%- elseif field.type.type.baseTypeName == "Decimal" -%}
{{ self.formatFieldName(field, isStrongLinkCaptured) -}}.decimalValue
{%- elseif baseTypeName == "DateTimeTimestamp" -%}
{%- elseif field.type.type.baseTypeName == "DateTimeTimestamp" -%}
Int({{ self.formatFieldName(field, isStrongLinkCaptured) -}}.timeIntervalSince1970)
{%- elseif baseTypeName == "Color" -%}
{%- elseif field.type.type.baseTypeName == "Color" -%}
{{ self.formatFieldName(field, isStrongLinkCaptured) -}}.hexString
{%- elseif baseTypeName == "Url" -%}
{%- elseif field.type.type.baseTypeName == "Url" -%}
{{ self.formatFieldName(field, isStrongLinkCaptured) -}}.absoluteString
{%- else -%}
{{ field.name }}
@ -65,11 +63,7 @@
{% endmacro %}
{%- macro formatEncodingDate(field) -%}
{%- if field.type.dateFormats is not empty -%}
ApiDateFormattingService.string(from: {{ field.name -}}, format: .{{- dateFormatToName(field.type.dateFormats[0]) -}})
{%- else -%}
ApiDateFormattingService.string(from: {{ field.name -}}, format: .{{- dateFormatToName(field.type.dateFormat) -}})
{%- endif -%}
ApiDateFormattingService.string(from: {{ field.name -}}, format: .{{- dateFormatToName(field.type.dateFormat) -}})
{%- endmacro -%}
{%- macro formatEncodingStringDecimal(field) -%}
@ -79,7 +73,7 @@
{% macro encodeIfPresent(field) %}
{%- import _self as self -%}
{%- set baseTypeName = field.type.type.baseTypeName -%}
{% if baseTypeName == "Date" or baseTypeName == "DateTime" or baseTypeName == "DateTimeTimestamp" or baseTypeName == "StringDecimal" %}
{% if baseTypeName == "DateTime" or baseTypeName == "DateTimeTimestamp" or baseTypeName == "StringDecimal" %}
if let {{ field.name }} = {{ field.name }} {
try container.encode({{- self.formatEncodingValue(field, true) -}}, forKey: .{{- field.name -}})
}
@ -107,12 +101,12 @@
{% macro decodeFromContainer(field) %}
{%- import _self as self -%}
{%- import 'complexField.utils.twig' as complexFieldUtils -%}
{%- set baseTypeName = field.type.type.baseTypeName -%}
{%- if baseTypeName == "Color" or baseTypeName == "Date" or baseTypeName == "DateTime" or baseTypeName == "StringDecimal" or baseTypeName == "Url" -%}
{%- if field.type.type.baseTypeName == "Color" or field.type.type.baseTypeName == "DateTime" or field.type.type.baseTypeName == "StringDecimal" or field.type.type.baseTypeName == "Url" -%}
{{ complexFieldUtils.decodeComplexField(field, "String") -}}
{%- elseif baseTypeName == "DateTimeTimestamp" -%}
{%- elseif field.type.type.baseTypeName == "DateTimeTimestamp" -%}
{{ complexFieldUtils.decodeComplexField(field, "Int") -}}
{%- elseif baseTypeName == "Decimal" -%}
{%- elseif field.type.type.baseTypeName == "Decimal" -%}
{{ complexFieldUtils.decodeComplexField(field, "Decimal") -}}
{%- else -%}
self.{{ field.name }} = try container.{{- self.formatOptionalDecode(field) -}}({{ self.formatValueType(field.type.type) }}.self, forKey: .{{ field.name }})
@ -154,7 +148,7 @@
[:]
{%- elseif baseTypeName == "String" -%}
""
{%- elseif baseTypeName == "Date" or baseTypeName == "DateTime" or baseTypeName == "DateTimeTimestamp" -%}
{%- elseif baseTypeName == "DateTime" or baseTypeName == "DateTimeTimestamp" -%}
Date().inDefaultRegion()
{%- elseif baseTypeName == "Double" -%}
0.0

View File

@ -14,21 +14,21 @@
{% macro initExpr(field) %}
{%- import _self as self -%}
{%- set baseTypeName = field.type.type.baseTypeName -%}
{%- if baseTypeName == "Date" or baseTypeName == "DateTime" -%}
{{ self.commonDateInit(field) -}}
{%- elseif baseTypeName == "Color" -%}
{%- if field.type.type.baseTypeName == "DateTime" -%}
{%- set dateTimeInit = "ApiDateFormattingService.date(from: %s, format: .%s, parsedIn: nil)"|format(field.name, dateFormatToName(field.type.dateFormat)) -%}
{{ self.decodeThrowableField(field, dateTimeInit, 'init?(string:format:fromRegion:) returned nil') -}}
{%- elseif field.type.type.baseTypeName == "Color" -%}
{%- set colorInit = "UIColor(hexString: %s)"|format(field.name) -%}
{{ self.decodeThrowableField(field, colorInit, 'Unable to decode color from hex string') -}}
{%- elseif baseTypeName == "Url" -%}
{%- elseif field.type.type.baseTypeName == "Url" -%}
{%- set urlInit = "URL(string: %s)"|format(field.name) -%}
{{ self.decodeThrowableField(field, urlInit, 'Unable to decode URL from string') -}}
{%- elseif baseTypeName == "StringDecimal" -%}
{%- elseif field.type.type.baseTypeName == "StringDecimal" -%}
{%- set stringDecimalInit = "ApiNumberFormattingService.decimalNumber(from: %s, format: .decimal)"|format(field.name) -%}
{{- self.decodeThrowableField(field, stringDecimalInit, 'Unable to decode decimal from string') -}}
{%- elseif baseTypeName == "DateTimeTimestamp" -%}
{%- elseif field.type.type.baseTypeName == "DateTimeTimestamp" -%}
self.{{ field.name }} = DateInRegion(seconds: TimeInterval({{ field.name }}))
{%- elseif baseTypeName == "Decimal" -%}
{%- elseif field.type.type.baseTypeName == "Decimal" -%}
self.{{ field.name }} = NSDecimalNumber(decimal: {{ field.name }})
{%- endif -%}
{% endmacro %}
@ -39,20 +39,4 @@
} else {
throw LeadKitError.failedToDecode(reason: "{{ errorMessage }}")
}
{%- endmacro -%}
{%- macro commonDateInit(field) -%}
{%- import _self as self -%}
{%- if field.type.dateFormats is not empty -%}
{%- set formattedDateFormats = [] -%}
{%- for dateFormat in field.type.dateFormats -%}
{%- set formattedDateFormats = formattedDateFormats|merge([".%s"|format(dateFormatToName(dateFormat))]) -%}
{%- endfor -%}
{%- set dateFormatsString = formattedDateFormats|join(', ') -%}
{%- set dateInit = "ApiDateFormattingService.date(from: %s, formats: [%s], parsedIn: nil)"|format(field.name, dateFormatsString) -%}
{{- self.decodeThrowableField(field, dateInit, "Unable to decode date from string") -}}
{%- else -%}
{%- set dateInit = "ApiDateFormattingService.date(from: %s, format: .%s, parsedIn: nil)"|format(field.name, dateFormatToName(field.type.dateFormat)) -%}
{{- self.decodeThrowableField(field, dateInit, "Unable to decode date from string") -}}
{%- endif -%}
{%- endmacro -%}

View File

@ -42,8 +42,8 @@
<div>{{ field.jsonName }}</div>
<div>
{{ self.formatValueType(field.type.type, field.nullable, objectsLinks, useAnchors) }}
{%- if field.type.type.typeName in ["DateTime", "Date"] -%}
&nbsp;({{ field.type.allDateFormats|join(', ') }})
{%- if field.type.type.typeName == "DateTime" -%}
&nbsp;({{ field.type.dateFormat }})
{%- endif -%}
</div>
<div>{{ field.description | escape }}</div>