Add equality methods generating in kotlin

This commit is contained in:
grechka62
2022-11-10 15:34:33 +03:00
parent 2436ee97d1
commit 9f404eddf8
20 changed files with 460 additions and 21 deletions
@@ -61,14 +61,17 @@ class KotlinGenerator(Generator):
result += EMPTY
result += entity.serialization_declaration.indented(indent_width=4)
if not is_template and self._generate_equality and not entity.instance_properties:
result += EMPTY
result += self.__manual_equals_hash_code_declaration.indented(indent_width=4)
if not is_template:
patch = entity.copy_with_new_array_declaration
if patch:
result += patch
if self._generate_equality and not entity.instance_properties:
result += EMPTY
result += self.__manual_equals_hash_code_declaration.indented(indent_width=4)
if entity.instance_properties:
result += entity.deep_equals_declaration
if entity.contains_array_of_objects:
result += entity.equals_except_array_declaration
result += entity.copy_with_new_array_declaration
static_declarations = entity.static_declarations
if static_declarations.lines:
@@ -234,10 +234,53 @@ class KotlinEntity(Entity):
return result
@property
def copy_with_new_array_declaration(self) -> Optional[Text]:
if not self.__contains_array_of_objects:
return None
def contains_array_of_objects(self) -> bool:
for p in self.instance_properties_kotlin:
if p.is_required_array_of_objects:
return True
return False
@property
def deep_equals_declaration(self) -> Text:
result = EMPTY
result += ' override fun equals(other: Any?): Boolean {'
result += self.__same_object_check
result += ' other ?: return false'
result += f' if (other !is {utils.capitalize_camel_case(self.name)}) {{'
result += ' return false'
result += ' }'
for p in self.instance_properties_kotlin:
result += p.equality_declaration
result += ' return true'
result += ' }'
return result
@property
def equals_except_array_declaration(self) -> Text:
result = EMPTY
class_name = utils.capitalize_camel_case(self.name)
result += f' fun equalsExceptArray(other: {class_name}): Boolean {{'
result += self.__same_object_check
for prop in list(filter(lambda p: not p.is_required_array_of_objects,
self.instance_properties_kotlin)):
result += prop.equality_declaration
result += ' return true'
result += ' }'
return result
@property
def __same_object_check(self) -> Text:
result = Text(' if (this === other) {')
result += ' return true'
result += ' }'
return result
@property
def copy_with_new_array_declaration(self) -> Text:
result = EMPTY
result += ' fun copyWithNewArray('
@@ -247,8 +290,7 @@ class KotlinEntity(Entity):
def append_arg(name: str):
constructor_params.append(f' {name},')
for p in sorted(filter(lambda prop: not isinstance(prop.property_type, StaticString),
self.properties), key=lambda prop: prop.name):
for p in self.instance_properties:
property_name = utils.lower_camel_case(p.name)
if p.optional:
append_arg(property_name)
@@ -273,15 +315,6 @@ class KotlinEntity(Entity):
result += ' )'
return result
@property
def __contains_array_of_objects(self) -> bool:
for p in self.properties:
if not p.optional and \
isinstance(p.property_type, Array) and \
isinstance(p.property_type.property_type, Object):
return True
return False
class KotlinProperty(Property):
@property
@@ -549,6 +582,20 @@ class KotlinProperty(Property):
reader_type = f'{EXPRESSION_TYPE_NAME}<{reader_type}>'
return f'val {self.reader_declaration_name}: Reader<{reader_type}{optional}> = {{ {lambda_val} }}'
@property
def is_required_array_of_objects(self) -> bool:
return not self.optional and \
isinstance(self.property_type, Array) and \
isinstance(self.property_type.property_type, Object)
@property
def equality_declaration(self) -> Text:
property_name = utils.lower_camel_case(self.name)
result = Text(f' if ({property_name} != other.{property_name}) {{')
result += ' return false'
result += ' }'
return result
class KotlinPropertyType(PropertyType):
@property
@@ -27,6 +27,27 @@ class EntityWithArray(
return json
}
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
other ?: return false
if (other !is EntityWithArray) {
return false
}
if (array != other.array) {
return false
}
return true
}
fun equalsExceptArray(other: EntityWithArray): Boolean {
if (this === other) {
return true
}
return true
}
fun copyWithNewArray(
array: List<Entity>,
) = EntityWithArray(
@@ -27,6 +27,27 @@ class EntityWithArrayOfEnums(
return json
}
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
other ?: return false
if (other !is EntityWithArrayOfEnums) {
return false
}
if (items != other.items) {
return false
}
return true
}
fun equalsExceptArray(other: EntityWithArrayOfEnums): Boolean {
if (this === other) {
return true
}
return true
}
fun copyWithNewArray(
items: List<EntityWithArrayOfEnums.Item>,
) = EntityWithArrayOfEnums(
@@ -27,6 +27,20 @@ class EntityWithArrayOfExpressions(
return json
}
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
other ?: return false
if (other !is EntityWithArrayOfExpressions) {
return false
}
if (items != other.items) {
return false
}
return true
}
companion object {
const val TYPE = "entity_with_array_of_expressions"
@@ -27,6 +27,27 @@ class EntityWithArrayOfNestedItems(
return json
}
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
other ?: return false
if (other !is EntityWithArrayOfNestedItems) {
return false
}
if (items != other.items) {
return false
}
return true
}
fun equalsExceptArray(other: EntityWithArrayOfNestedItems): Boolean {
if (this === other) {
return true
}
return true
}
fun copyWithNewArray(
items: List<EntityWithArrayOfNestedItems.Item>,
) = EntityWithArrayOfNestedItems(
@@ -64,6 +85,23 @@ class EntityWithArrayOfNestedItems(
return json
}
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
other ?: return false
if (other !is Item) {
return false
}
if (entity != other.entity) {
return false
}
if (property != other.property) {
return false
}
return true
}
companion object {
@JvmStatic
@JvmName("fromJson")
@@ -27,6 +27,20 @@ class EntityWithArrayWithTransform(
return json
}
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
other ?: return false
if (other !is EntityWithArrayWithTransform) {
return false
}
if (array != other.array) {
return false
}
return true
}
companion object {
const val TYPE = "entity_with_array_with_transform"
@@ -27,6 +27,20 @@ class EntityWithComplexProperty(
return json
}
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
other ?: return false
if (other !is EntityWithComplexProperty) {
return false
}
if (property != other.property) {
return false
}
return true
}
companion object {
const val TYPE = "entity_with_complex_property"
@@ -54,6 +68,20 @@ class EntityWithComplexProperty(
return json
}
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
other ?: return false
if (other !is Property) {
return false
}
if (value != other.value) {
return false
}
return true
}
companion object {
@JvmStatic
@JvmName("fromJson")
@@ -27,6 +27,20 @@ class EntityWithComplexPropertyWithDefaultValue(
return json
}
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
other ?: return false
if (other !is EntityWithComplexPropertyWithDefaultValue) {
return false
}
if (property != other.property) {
return false
}
return true
}
companion object {
const val TYPE = "entity_with_complex_property_with_default_value"
@@ -56,6 +70,20 @@ class EntityWithComplexPropertyWithDefaultValue(
return json
}
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
other ?: return false
if (other !is Property) {
return false
}
if (value != other.value) {
return false
}
return true
}
companion object {
@JvmStatic
@JvmName("fromJson")
@@ -27,6 +27,20 @@ class EntityWithEntityProperty(
return json
}
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
other ?: return false
if (other !is EntityWithEntityProperty) {
return false
}
if (entity != other.entity) {
return false
}
return true
}
companion object {
const val TYPE = "entity_with_entity_property"
@@ -27,6 +27,20 @@ class EntityWithOptionalComplexProperty(
return json
}
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
other ?: return false
if (other !is EntityWithOptionalComplexProperty) {
return false
}
if (property != other.property) {
return false
}
return true
}
companion object {
const val TYPE = "entity_with_optional_complex_property"
@@ -54,6 +68,20 @@ class EntityWithOptionalComplexProperty(
return json
}
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
other ?: return false
if (other !is Property) {
return false
}
if (value != other.value) {
return false
}
return true
}
companion object {
@JvmStatic
@JvmName("fromJson")
@@ -27,6 +27,20 @@ class EntityWithOptionalProperty(
return json
}
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
other ?: return false
if (other !is EntityWithOptionalProperty) {
return false
}
if (property != other.property) {
return false
}
return true
}
companion object {
const val TYPE = "entity_with_optional_property"
@@ -27,6 +27,20 @@ class EntityWithOptionalStringEnumProperty(
return json
}
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
other ?: return false
if (other !is EntityWithOptionalStringEnumProperty) {
return false
}
if (property != other.property) {
return false
}
return true
}
companion object {
const val TYPE = "entity_with_optional_string_enum_property"
@@ -31,6 +31,26 @@ class EntityWithPropertyWithDefaultValue(
return json
}
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
other ?: return false
if (other !is EntityWithPropertyWithDefaultValue) {
return false
}
if (int != other.int) {
return false
}
if (nested != other.nested) {
return false
}
if (url != other.url) {
return false
}
return true
}
companion object {
const val TYPE = "entity_with_property_with_default_value"
@@ -72,6 +92,26 @@ class EntityWithPropertyWithDefaultValue(
return json
}
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
other ?: return false
if (other !is Nested) {
return false
}
if (int != other.int) {
return false
}
if (nonOptional != other.nonOptional) {
return false
}
if (url != other.url) {
return false
}
return true
}
companion object {
private val INT_DEFAULT_VALUE = Expression.constant(0)
private val URL_DEFAULT_VALUE = Expression.constant(Uri.parse("https://yandex.ru"))
@@ -27,6 +27,20 @@ class EntityWithRequiredProperty(
return json
}
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
other ?: return false
if (other !is EntityWithRequiredProperty) {
return false
}
if (property != other.property) {
return false
}
return true
}
companion object {
const val TYPE = "entity_with_required_property"
@@ -43,6 +43,44 @@ class EntityWithSimpleProperties(
return json
}
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
other ?: return false
if (other !is EntityWithSimpleProperties) {
return false
}
if (boolean != other.boolean) {
return false
}
if (booleanInt != other.booleanInt) {
return false
}
if (color != other.color) {
return false
}
if (double != other.double) {
return false
}
if (id != other.id) {
return false
}
if (integer != other.integer) {
return false
}
if (positiveInteger != other.positiveInteger) {
return false
}
if (string != other.string) {
return false
}
if (url != other.url) {
return false
}
return true
}
companion object {
const val TYPE = "entity_with_simple_properties"
@@ -27,6 +27,27 @@ class EntityWithStrictArray(
return json
}
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
other ?: return false
if (other !is EntityWithStrictArray) {
return false
}
if (array != other.array) {
return false
}
return true
}
fun equalsExceptArray(other: EntityWithStrictArray): Boolean {
if (this === other) {
return true
}
return true
}
fun copyWithNewArray(
array: List<Entity>,
) = EntityWithStrictArray(
@@ -27,6 +27,20 @@ class EntityWithStringArrayProperty(
return json
}
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
other ?: return false
if (other !is EntityWithStringArrayProperty) {
return false
}
if (array != other.array) {
return false
}
return true
}
companion object {
const val TYPE = "entity_with_string_array_property"
@@ -27,6 +27,20 @@ class EntityWithStringEnumProperty(
return json
}
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
other ?: return false
if (other !is EntityWithStringEnumProperty) {
return false
}
if (property != other.property) {
return false
}
return true
}
companion object {
const val TYPE = "entity_with_string_enum_property"
@@ -27,6 +27,20 @@ class EntityWithStringEnumPropertyWithDefaultValue(
return json
}
override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}
other ?: return false
if (other !is EntityWithStringEnumPropertyWithDefaultValue) {
return false
}
if (value != other.value) {
return false
}
return true
}
companion object {
const val TYPE = "entity_with_string_enum_property_with_default_value"