Fixed NumberFormattingService, bugfix

This commit is contained in:
Ivan Babkin 2019-05-16 13:18:56 +03:00
parent 649089d79e
commit e023fed90a
4 changed files with 37 additions and 21 deletions

View File

@ -1,29 +1,43 @@
{%- import 'macroses/common.utils.twig' as utils -%}
import LeadKit
import Foundation
final class ApiNumberFormattingService {
enum ApiNumberFormat: NumberFormat {
static let shared = ApiNumberFormattingService()
case decimal
var numberFormatter: NumberFormatter {
let numberFormatter = NumberFormatter()
switch self {
case .decimal:
let numberFormatter = NumberFormatter()
numberFormatter.decimalSeparator = "."
numberFormatter.minimumIntegerDigits = 1
numberFormatter.maximumFractionDigits = 16
}
return numberFormatter
}
}
final class ApiNumberFormattingService: NumberFormattingService, Singleton {
typealias NumberFormatType = ApiNumberFormat
func decimalNumber(from string: String) -> NSDecimalNumber? {
guard let number = numberFormatter.number(from: string) else {
let formatters = computedFormatters
static let shared = ApiNumebrFormattingService()
private init() {}
func decimalNumber(from string: String, format: ApiNumberFormat) -> NSDecimalNumber? {
guard let number = number(from: string, format: format) else {
return nil
}
return NSDecimalNumber(decimal: number.decimalValue)
}
func string(from number: NSDecimalNumber) -> String {
return number.stringValue
}
private init() {}
private lazy var numberFormatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.decimalSeparator = "."
return formatter
}()
}
{{ "\n" }}

View File

@ -4,7 +4,6 @@
return {{ type.baseTypeName }}({% include '../class/fields-optional-initialization.twig' with { fields: allFieldsOrdered } %})
}
{%- set containsOptionalFields = false -%}
{%- if allFieldsOrdered is not empty -%}
{%- for field in allFieldsOrdered -%}
{%- if field.nullable or field.optional -%}

View File

@ -46,7 +46,7 @@
{%- elseif field.type.type.baseTypeName == "Url" -%}
{{ self.formatStrongOrNullableOptional(field, strong) -}}.absoluteString
{%- elseif field.type.type.baseTypeName == "StringDecimal" -%}
ApiNumberFormattingService.shared.string(from: {{ field.name -}})
{{- self.formatEncodingStringDecimal(field) -}}
{%- else -%}
{{ field.name }}
{%- endif -%}
@ -66,6 +66,10 @@
ApiDateFormattingService.string(from: {{ field.name -}}, format: .{{- dateFormatToName(field.type.dateFormat) -}})
{%- endmacro -%}
{%- macro formatEncodingStringDecimal(field) -%}
ApiNumberFormattingService.shared.string(from: {{ field.name -}}, format: .decimal)
{%- endmacro -%}
{% macro escapeIfNeeded(expr) %}
{%- if expr == "default" or expr == "operator" -%}
`{{ expr }}`
@ -84,8 +88,7 @@
{%- set intInit = "Int(%s.timeIntervalSince1970)"|format(field.name) -%}
{{ complexFieldUtils.encodeComplexFieldIfPresent(field, intInit) -}}
{%- elseif field.type.type.baseTypeName == "StringDecimal" -%}
{%- set stringDecimalInit = "ApiNumberFormattingService.shared.string(from: %s)"|format(field.name) -%}
{{ complexFieldUtils.encodeComplexFieldIfPresent(field, stringDecimalInit) -}}
{{ complexFieldUtils.encodeComplexFieldIfPresent(field, self.formatEncodingStringDecimal(field)) -}}
{%- else -%}
try container.encodeIfPresent({{- self.formatEncodingValue(field, false) -}}, forKey: .{{- field.name -}})
{%- endif -%}

View File

@ -24,7 +24,7 @@
{%- set urlInit = "URL(string: %s)"|format(field.name) -%}
{{ self.decodeThrowableField(field, urlInit, 'Unable to decode URL from string') -}}
{%- elseif field.type.type.baseTypeName == "StringDecimal" -%}
{%- set stringDecimalInit = "ApiNumberFormattingService.shared.decimalNumber(from: %s)"|format(field.name) -%}
{%- set stringDecimalInit = "ApiNumberFormattingService.shared.decimalNumber(from: %s, format: .decimal)"|format(field.name) -%}
{{- self.decodeThrowableField(field, stringDecimalInit, 'Unable to decode decimal from string') -}}
{%- elseif field.type.type.baseTypeName == "DateTimeTimestamp" -%}
self.{{ field.name }} = DateInRegion(seconds: TimeInterval({{ field.name }}))