Dao generating

This commit is contained in:
Denis Karmyshakov 2018-10-12 15:07:05 +03:00
parent 2599ca2411
commit 4ff3c8c8d6
9 changed files with 106 additions and 42 deletions

View File

@ -3,6 +3,20 @@
*/
package {{ packageName }}.api;
{%- if (storageAttributes is not null) %}
import android.arch.persistence.room.Delete;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.OnConflictStrategy;
import android.arch.persistence.room.Query;
import android.arch.persistence.room.Transaction;
import android.arch.persistence.room.TypeConverter;
import android.arch.persistence.room.TypeConverters;
import io.reactivex.Flowable;
import io.reactivex.Single;
{%- endif %}
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.util.ObjectsCompat;
@ -29,14 +43,6 @@ import ru.touchin.templates.ApiModel;
import ru.touchin.roboswag.core.utils.ObjectUtils;
import ru.touchin.templates.logansquare.LoganSquareJsonModel;
{%- if (storageAttributes is not null) %}
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.PrimaryKey;
import android.arch.persistence.room.TypeConverters;
import android.arch.persistence.room.TypeConverter;
{%- endif %}
{%- if (description is not empty) %}
/**
@ -50,20 +56,18 @@ import android.arch.persistence.room.TypeConverter;
{%- set attributeWas = true -%}
tableName = "{{ storageAttributes.tableName }}"
{%- endif -%}
{%- if attributeWas -%}, {% endif %}
{%- if attributeWas and storageAttributes.primaryKeys is not empty -%}, {% endif %}
{%- if (storageAttributes.primaryKeys is not empty) -%}
{%- set attributeWas = true -%}
primaryKeys = { {%- for key in storageAttributes.primaryKeys -%} {%- if not (loop.last) %}, {% endif -%} "{{- key.name -}}" {%- endfor -%} }
primaryKeys = { {%- for key in storageAttributes.primaryKeys -%} {%- if not (loop.last) %}, {% endif -%} "{{- key -}}" {%- endfor -%} }
{%- endif -%}
)
@TypeConverters({{ type.baseTypeName }}.class)
{%- endif %}
@JsonObject(serializeNullObjects = true)
@JsonObject(serializeNullObjects = {{ serializeNulls }})
public class {% include 'blocks/class/classtype.twig' with { type: type } %} extends {% include 'blocks/class/supertype.twig' with { type: type, parent: parent } %} {
{%- if (storageAttributes is not null) -%}
{%- include 'blocks/class/converters.twig' -%}
{%- endif -%}
{%- include 'blocks/class/fields-converters.twig' -%}
{% include 'blocks/class/fields.twig' with { fields: fields } %}
@ -133,4 +137,6 @@ public class {% include 'blocks/class/classtype.twig' with { type: type } %} ext
{%- endif %}
{%- include 'blocks/class/dao.twig' %}
}

View File

@ -2,9 +2,7 @@
* This code is autogenerated by Touch Instinct tools
*/
package {{ packageName }}.api;
{%- if storageAttributes.store %}
{% if storable %}
import android.arch.persistence.room.TypeConverter;
{%- endif %}
import android.support.annotation.NonNull;
@ -20,17 +18,14 @@ import com.bluelinelabs.logansquare.annotation.JsonNumberValue;
import ru.touchin.templates.logansquare.LoganSquareEnum;
import ru.touchin.templates.logansquare.LoganSquareEnumConverter;
{%- if storable %}
import android.arch.persistence.room.TypeConverter;
{%- endif %}
/**
* {{ description }}
*/
@JsonEnum
public enum {{ name }} {
{%- include 'blocks/enum/cases.twig' with { values: values } %}
{%- include 'blocks/enum/cases.twig' with { values: values } %}
{%- include 'blocks/enum/converters.twig' %}
}

View File

@ -0,0 +1,38 @@
{%- import '../../utils.twig' as utils -%}
{%- if (storageAttributes is not null) %}
@android.arch.persistence.room.Dao
public interface Dao {
@NonNull
@Query("SELECT * FROM {{ default(storageAttributes.tableName, type.baseTypeName) }}")
List<{{ type.baseTypeName }}> get();
@NonNull
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(@NonNull final Iterable<{{ type.baseTypeName }}> list);
@NonNull
@Delete
void delete(@NonNull final Iterable<{{ type.baseTypeName }}> list);
@Query("DELETE FROM {{ default(storageAttributes.tableName, type.baseTypeName) }}")
void clear();
}
@android.arch.persistence.room.Dao
public interface ReactiveDao {
@NonNull
@Query("SELECT * FROM {{ default(storageAttributes.tableName, type.baseTypeName) }}")
Flowable<List<{{ type.baseTypeName }}>> observe();
@NonNull
@Query("SELECT * FROM {{ default(storageAttributes.tableName, type.baseTypeName) }}")
Single<List<{{ type.baseTypeName }}>> get();
}
{%- endif -%}

View File

@ -1,11 +1,17 @@
{%- import '../../utils.twig' as utils -%}
{%- if (storageAttributes is not null) -%}
{% set converters = [] %}
{%- for field in fields -%}
{%- if not (field.type.type.baseTypeName in converters) -%}
{%- if field.type.type.baseTypeName == "Array" %}
@TypeConverter
@Nullable
public static String serialize{{ capitalize(field.name) }}(@Nullable final {{ utils.formatValueType(field.type.type, true) }} value) {
public static String serialize{{ field.type.type.itemsType.baseTypeName }}List(@Nullable final {{ utils.formatValueType(field.type.type, true) }} value) {
if (value == null) {
return null;
}
@ -18,7 +24,7 @@
@TypeConverter
@Nullable
public static {{ utils.formatValueType(field.type.type, true) }} deserialize{{ capitalize(field.name) }}(@Nullable final String value) {
public static {{ utils.formatValueType(field.type.type, true) }} deserialize{{ field.type.type.itemsType.baseTypeName }}List(@Nullable final String value) {
if (value == null) {
return null;
}
@ -28,11 +34,13 @@
return null;
}
}
{%- set converters = merge(converters, field.type.type.itemsType.baseTypeName) -%}
{%- elseif field.type.type.baseTypeName == "Map" %}
@TypeConverter
@Nullable
public static String serialize{{ capitalize(field.name) }}(@Nullable final {{ utils.formatValueType(field.type.type, true) }} value) {
public static String serialize{{ field.type.type.valuesType.baseTypeName }}Map(@Nullable final {{ utils.formatValueType(field.type.type, true) }} value) {
if (value == null) {
return null;
}
@ -45,7 +53,7 @@
@TypeConverter
@Nullable
public static {{ utils.formatValueType(field.type.type, true) }} deserialize{{ capitalize(field.name) }}(@Nullable final String value) {
public static {{ utils.formatValueType(field.type.type, true) }} deserialize{{ field.type.type.valuesType.baseTypeName }}Map(@Nullable final String value) {
if (value == null) {
return null;
}
@ -55,6 +63,8 @@
return null;
}
}
{%- set converters = merge(converters, field.type.type.valuesType.baseTypeName) -%}
{%- elseif field.type.type.baseTypeName != "Bool"
and field.type.type.baseTypeName != "Int"
and field.type.type.baseTypeName != "Long"
@ -64,7 +74,7 @@
@TypeConverter
@Nullable
public static String serialize{{ capitalize(field.name) }}(@Nullable final {{ utils.formatValueType(field.type.type, true) }} value) {
public static String serialize{{ field.type.type.baseTypeName }}(@Nullable final {{ utils.formatValueType(field.type.type, true) }} value) {
if (value == null) {
return null;
}
@ -77,7 +87,7 @@
@TypeConverter
@Nullable
public static {{ utils.formatValueType(field.type.type, true) }} deserialize{{ capitalize(field.name) }}(@Nullable final String value) {
public static {{ utils.formatValueType(field.type.type, true) }} deserialize{{ field.type.type.baseTypeName }}(@Nullable final String value) {
if (value == null) {
return null;
}
@ -87,5 +97,12 @@
return null;
}
}
{%- set converters = merge(converters, field.type.type.baseTypeName) -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- endif -%}

View File

@ -9,7 +9,7 @@
{{ utils.writeNullCheckAnnotation(field.type.type.baseTypeName, field.nullable) }}
public {{ utils.formatValueType(field.type.type, field.nullable) }} {% if (field.type.type.baseTypeName == "Bool") and (field.name matches "^is[A-Z,0-9].*") -%}{{ field.name }}{%- else -%}get{{ capitalize(field.name) }}{%- endif -%}() {
{%- if field.nullable %}
if ({{ fieldName }} == null) {
if (this.{{ field.name }} == null) {
return null;
}
{%- endif %}

View File

@ -31,12 +31,14 @@
and field.type.type.baseTypeName != "Decimal"
and field.type.type.baseTypeName != "DateTime"
and (field.type.values is empty) %}
{%- if field.nullable %}
if (this.{{ field.name }} != null) {
this.{{ field.name }}.validate();
if (this.{{ field.name }} instanceof ApiModel) {
{%- if field.nullable %}
if (this.{{ field.name }} != null) {
((ApiModel) this.{{ field.name }}).validate();
}
{%- else %}
((ApiModel) this.{{ field.name }}).validate();
{%- endif %}
}
{%- else %}
this.{{ field.name }}.validate();
{%- endif -%}
{%- endif -%}
{%- endfor -%}

View File

@ -8,9 +8,14 @@
*/
{%- endif %}
{{ utils.writeNullCheckAnnotation(field.type.type.baseTypeName, field.nullable) }}
{%- if (storageAttributes is not null) and field.type.storable %}
{%- if (storageAttributes is not null) %}
{%- if (field.type.storable) %}
@TypeConverters({{ field.type.type.baseTypeName }}.class)
{%- endif %}
{%- if (field.autoGenerate) %}
@PrimaryKey(autoGenerate = true)
{%- endif %}
{%- endif %}
@JsonField(name = "{{ field.jsonName }}")
private {{ utils.formatValueType(field.type.type, field.nullable) }} {{ field.name }};
{%- endfor -%}

View File

@ -1,10 +1,11 @@
{%- import '../../utils.twig' as utils -%}
{%- for value in values %}
{% if (value.description is not empty) %}
{%- if (value.description is not empty) %}
/**
* {{ value.description }}
*/
{% endif -%}
* {{ value.description }}
*/
{%- endif %}
{%- if valuesTypes == "STRING" %}
@JsonStringValue("{{ value.value }}")
{%- elseif valuesTypes == "INT" %}

View File

@ -19,4 +19,4 @@
return {{ name }}.valueOf(value);
}
{%- endif %}
{%- endif -%}