From 1cd0fe53173cbc49765d03d3d03296bf6ab2ac3b Mon Sep 17 00:00:00 2001 From: Gavriil Sitnikov Date: Thu, 22 Jun 2017 19:26:45 +0300 Subject: [PATCH] java type generation added --- Java/ApiDateFormattingService.swift.twig | 23 ++++++++++ Java/Class.java.twig | 8 ++++ Java/blocks/class/classtype.twig | 2 - Java/blocks/class/init-parameters-fields.twig | 7 +++ Java/blocks/class/type-parameters.twig | 2 +- Java/utils.twig | 45 +++++++++++++++---- 6 files changed, 76 insertions(+), 11 deletions(-) create mode 100644 Java/ApiDateFormattingService.swift.twig create mode 100644 Java/blocks/class/init-parameters-fields.twig diff --git a/Java/ApiDateFormattingService.swift.twig b/Java/ApiDateFormattingService.swift.twig new file mode 100644 index 0000000..6339878 --- /dev/null +++ b/Java/ApiDateFormattingService.swift.twig @@ -0,0 +1,23 @@ +import LeadKit +import ObjectMapper + +final class ApiDateFormattingService: DateFormattingService { + + static let shared = ApiDateFormattingService() + + private init() { + super.init(formattingArguments: [ + {% for dateFormat in dateFormats -%} + DateFormattingArguments(dateFormat: "{{ dateFormat }}"){% if not (loop.last) %},{%- endif %} + {% endfor %} + ]) + } +} + +extension ApiDateFormattingService { + + func mappingTransform(for dateFormat: String) -> TransformOf { + return mappingTransform(for: DateFormattingArguments(dateFormat: dateFormat)) + } + +} diff --git a/Java/Class.java.twig b/Java/Class.java.twig index 859be2d..84296dd 100644 --- a/Java/Class.java.twig +++ b/Java/Class.java.twig @@ -1,3 +1,4 @@ +{%- set hasParent = parent is not null -%} /** * This code is autogenerated by Touch Instinct tools */ @@ -17,4 +18,11 @@ public class {% include 'blocks/class/classtype.twig' with { type: type } %} ext super(); } +{% if (fields is not empty) %} + public {{ type.baseTypeName }}({%- include 'blocks/class/init-parameters-fields.twig' with { fields: allFieldsOrdered } -%}) { + super(); + } +{% endif %} + + } \ No newline at end of file diff --git a/Java/blocks/class/classtype.twig b/Java/blocks/class/classtype.twig index 285ae64..623f00f 100644 --- a/Java/blocks/class/classtype.twig +++ b/Java/blocks/class/classtype.twig @@ -1,3 +1 @@ -{%- import '../../utils.twig' as utils -%} - {{- type.baseTypeName -}}{%- include 'type-parameters.twig' with { typeParameters: type.typeParameters } -%} \ No newline at end of file diff --git a/Java/blocks/class/init-parameters-fields.twig b/Java/blocks/class/init-parameters-fields.twig new file mode 100644 index 0000000..bdab74a --- /dev/null +++ b/Java/blocks/class/init-parameters-fields.twig @@ -0,0 +1,7 @@ +{%- import '../../utils.twig' as utils -%} + +{%- if fields is not empty -%} +{%- for field in fields -%} + {{ utils.writeNullCheckAnnotation(field.type.type.baseTypeName) }} final {{ utils.formatValueType(field.type.type, field.nullable, field.optional) }} {{ field.name }} {%- if not (loop.last) %}, {% endif %} +{%- endfor -%} +{%- endif -%} \ No newline at end of file diff --git a/Java/blocks/class/type-parameters.twig b/Java/blocks/class/type-parameters.twig index 1aea606..2668317 100644 --- a/Java/blocks/class/type-parameters.twig +++ b/Java/blocks/class/type-parameters.twig @@ -2,7 +2,7 @@ {%- if typeParameters is not empty -%} < {%- for typeParameter in typeParameters %} -{{- utils.formatValueType(typeParameter) -}}{%- if not (loop.last) %}, {% endif %} +{{- utils.formatSimpleValueType(typeParameter) -}}{%- if not (loop.last) %}, {% endif %} {%- endfor -%} > {%- endif -%} \ No newline at end of file diff --git a/Java/utils.twig b/Java/utils.twig index 298436c..b843372 100644 --- a/Java/utils.twig +++ b/Java/utils.twig @@ -18,22 +18,51 @@ LoganSquareJsonModel {%- endif -%} {% endmacro %} -{% macro formatNullable(expr, nullable, optional) %} -{{- expr -}}{%- if nullable or optional -%}?{%- endif -%} +{% macro writeNullCheckAnnotation(baseTypeName, nullable, optional) %} +{%- if optional -%} +@NonNull +{%- elseif nullable -%} +@Nullable +{%- elseif not (baseTypeName == "Int" or baseTypeName == "Long" or baseTypeName == "Double" or baseTypeName == "Decimal" or baseTypeName == "Bool") -%} +@NonNull +{%- endif -%} {% endmacro %} -{% macro formatValueType(valueType) %} -{%- if valueType.baseTypeName == "Array" -%} -List<{{ valueType.itemsType.baseTypeName }}> +{% macro formatNonOptionalValueType(valueType, tryUsePrimitive) %} +{% import _self as self %} +{%- if valueType.baseTypeName == "Int" -%} +{%- if tryUsePrimitive -%}int{%- else -%}Integer{%- endif -%} +{%- elseif valueType.baseTypeName == "Long" -%} +{%- if tryUsePrimitive -%}long{%- else -%}Long{%- endif -%} +{%- elseif valueType.baseTypeName == "Double" -%} +{%- if tryUsePrimitive -%}double{%- else -%}Double{%- endif -%} +{%- elseif valueType.baseTypeName == "Decimal" -%} +BigDecimal +{%- elseif valueType.baseTypeName == "Bool" -%} +{%- if tryUsePrimitive -%}bool{%- else -%}Boolean{%- endif -%} {%- elseif valueType.baseTypeName == "Map" -%} -Map<{{ valueType.keysType.baseTypeName }}, {{ valueType.valuesType.baseTypeName }}> -{%- elseif valueType.baseTypeName == "DateTime" -%} -DateTime +Map<{{ self.formatNonOptionalValueType(valueType.keysType, false) }}, {{ self.formatNonOptionalValueType(valueType.valuesType, false) }}> +{%- elseif valueType.baseTypeName == "Array" -%} +List<{{ self.formatNonOptionalValueType(valueType.itemsType, false) }}> {%- else -%} {{ valueType.baseTypeName }} {%- endif -%} {% endmacro %} +{% macro formatValueType(valueType, nullable, optional) %} +{% import _self as self %} +{%- if optional -%} +JsonOptional<{{ self.formatNonOptionalValueType(valueType, false) }}> +{%- else -%} +{{ self.formatNonOptionalValueType(valueType, not nullable) }} +{%- endif -%} +{% endmacro %} + +{% macro formatSimpleValueType(valueType) %} +{%- import _self as self -%} +{{- self.formatValueType(valueType, true, false) -}} +{% endmacro %} + {% macro escapeIfNeeded(expr) %} {%- if expr == "default" -%} `{{ expr }}`