From 9f404eddf8bccef01ae9484dddad3c2da79bbacc Mon Sep 17 00:00:00 2001 From: grechka62 Date: Thu, 10 Nov 2022 15:34:33 +0300 Subject: [PATCH] Add equality methods generating in kotlin --- .../generators/kotlin/generator.py | 17 +++-- .../generators/kotlin/kotlin_entities.py | 75 +++++++++++++++---- .../references/kotlin/EntityWithArray.kt | 21 ++++++ .../kotlin/EntityWithArrayOfEnums.kt | 21 ++++++ .../kotlin/EntityWithArrayOfExpressions.kt | 14 ++++ .../kotlin/EntityWithArrayOfNestedItems.kt | 38 ++++++++++ .../kotlin/EntityWithArrayWithTransform.kt | 14 ++++ .../kotlin/EntityWithComplexProperty.kt | 28 +++++++ ...tityWithComplexPropertyWithDefaultValue.kt | 28 +++++++ .../kotlin/EntityWithEntityProperty.kt | 14 ++++ .../EntityWithOptionalComplexProperty.kt | 28 +++++++ .../kotlin/EntityWithOptionalProperty.kt | 14 ++++ .../EntityWithOptionalStringEnumProperty.kt | 14 ++++ .../EntityWithPropertyWithDefaultValue.kt | 40 ++++++++++ .../kotlin/EntityWithRequiredProperty.kt | 14 ++++ .../kotlin/EntityWithSimpleProperties.kt | 38 ++++++++++ .../kotlin/EntityWithStrictArray.kt | 21 ++++++ .../kotlin/EntityWithStringArrayProperty.kt | 14 ++++ .../kotlin/EntityWithStringEnumProperty.kt | 14 ++++ ...yWithStringEnumPropertyWithDefaultValue.kt | 14 ++++ 20 files changed, 460 insertions(+), 21 deletions(-) diff --git a/api_generator/api_generator/generators/kotlin/generator.py b/api_generator/api_generator/generators/kotlin/generator.py index f389589ca..cd8018b45 100644 --- a/api_generator/api_generator/generators/kotlin/generator.py +++ b/api_generator/api_generator/generators/kotlin/generator.py @@ -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: diff --git a/api_generator/api_generator/generators/kotlin/kotlin_entities.py b/api_generator/api_generator/generators/kotlin/kotlin_entities.py index db0d5e233..7594bdef4 100644 --- a/api_generator/api_generator/generators/kotlin/kotlin_entities.py +++ b/api_generator/api_generator/generators/kotlin/kotlin_entities.py @@ -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 diff --git a/api_generator/tests/references/kotlin/EntityWithArray.kt b/api_generator/tests/references/kotlin/EntityWithArray.kt index 156d567e8..1c3cb65f8 100644 --- a/api_generator/tests/references/kotlin/EntityWithArray.kt +++ b/api_generator/tests/references/kotlin/EntityWithArray.kt @@ -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, ) = EntityWithArray( diff --git a/api_generator/tests/references/kotlin/EntityWithArrayOfEnums.kt b/api_generator/tests/references/kotlin/EntityWithArrayOfEnums.kt index 15a8115ff..3f575a6fb 100644 --- a/api_generator/tests/references/kotlin/EntityWithArrayOfEnums.kt +++ b/api_generator/tests/references/kotlin/EntityWithArrayOfEnums.kt @@ -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( diff --git a/api_generator/tests/references/kotlin/EntityWithArrayOfExpressions.kt b/api_generator/tests/references/kotlin/EntityWithArrayOfExpressions.kt index 193671015..a41f2372e 100644 --- a/api_generator/tests/references/kotlin/EntityWithArrayOfExpressions.kt +++ b/api_generator/tests/references/kotlin/EntityWithArrayOfExpressions.kt @@ -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" diff --git a/api_generator/tests/references/kotlin/EntityWithArrayOfNestedItems.kt b/api_generator/tests/references/kotlin/EntityWithArrayOfNestedItems.kt index c160f1525..9c0010d1d 100644 --- a/api_generator/tests/references/kotlin/EntityWithArrayOfNestedItems.kt +++ b/api_generator/tests/references/kotlin/EntityWithArrayOfNestedItems.kt @@ -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( @@ -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") diff --git a/api_generator/tests/references/kotlin/EntityWithArrayWithTransform.kt b/api_generator/tests/references/kotlin/EntityWithArrayWithTransform.kt index b2c55690e..147d2cbf4 100644 --- a/api_generator/tests/references/kotlin/EntityWithArrayWithTransform.kt +++ b/api_generator/tests/references/kotlin/EntityWithArrayWithTransform.kt @@ -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" diff --git a/api_generator/tests/references/kotlin/EntityWithComplexProperty.kt b/api_generator/tests/references/kotlin/EntityWithComplexProperty.kt index eaf6a7944..f67f81628 100644 --- a/api_generator/tests/references/kotlin/EntityWithComplexProperty.kt +++ b/api_generator/tests/references/kotlin/EntityWithComplexProperty.kt @@ -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") diff --git a/api_generator/tests/references/kotlin/EntityWithComplexPropertyWithDefaultValue.kt b/api_generator/tests/references/kotlin/EntityWithComplexPropertyWithDefaultValue.kt index e80113a32..238a1e3a4 100644 --- a/api_generator/tests/references/kotlin/EntityWithComplexPropertyWithDefaultValue.kt +++ b/api_generator/tests/references/kotlin/EntityWithComplexPropertyWithDefaultValue.kt @@ -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") diff --git a/api_generator/tests/references/kotlin/EntityWithEntityProperty.kt b/api_generator/tests/references/kotlin/EntityWithEntityProperty.kt index f0922be88..fb7ea2b64 100644 --- a/api_generator/tests/references/kotlin/EntityWithEntityProperty.kt +++ b/api_generator/tests/references/kotlin/EntityWithEntityProperty.kt @@ -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" diff --git a/api_generator/tests/references/kotlin/EntityWithOptionalComplexProperty.kt b/api_generator/tests/references/kotlin/EntityWithOptionalComplexProperty.kt index 715a6da3a..ae697c3d9 100644 --- a/api_generator/tests/references/kotlin/EntityWithOptionalComplexProperty.kt +++ b/api_generator/tests/references/kotlin/EntityWithOptionalComplexProperty.kt @@ -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") diff --git a/api_generator/tests/references/kotlin/EntityWithOptionalProperty.kt b/api_generator/tests/references/kotlin/EntityWithOptionalProperty.kt index 53f20db73..c22272b4b 100644 --- a/api_generator/tests/references/kotlin/EntityWithOptionalProperty.kt +++ b/api_generator/tests/references/kotlin/EntityWithOptionalProperty.kt @@ -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" diff --git a/api_generator/tests/references/kotlin/EntityWithOptionalStringEnumProperty.kt b/api_generator/tests/references/kotlin/EntityWithOptionalStringEnumProperty.kt index 4f259d2a5..6319a736e 100644 --- a/api_generator/tests/references/kotlin/EntityWithOptionalStringEnumProperty.kt +++ b/api_generator/tests/references/kotlin/EntityWithOptionalStringEnumProperty.kt @@ -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" diff --git a/api_generator/tests/references/kotlin/EntityWithPropertyWithDefaultValue.kt b/api_generator/tests/references/kotlin/EntityWithPropertyWithDefaultValue.kt index 50f17b384..cb0a7d38a 100644 --- a/api_generator/tests/references/kotlin/EntityWithPropertyWithDefaultValue.kt +++ b/api_generator/tests/references/kotlin/EntityWithPropertyWithDefaultValue.kt @@ -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")) diff --git a/api_generator/tests/references/kotlin/EntityWithRequiredProperty.kt b/api_generator/tests/references/kotlin/EntityWithRequiredProperty.kt index 4b3eeeecc..f32e4ff55 100644 --- a/api_generator/tests/references/kotlin/EntityWithRequiredProperty.kt +++ b/api_generator/tests/references/kotlin/EntityWithRequiredProperty.kt @@ -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" diff --git a/api_generator/tests/references/kotlin/EntityWithSimpleProperties.kt b/api_generator/tests/references/kotlin/EntityWithSimpleProperties.kt index 93ede8793..652a12d20 100644 --- a/api_generator/tests/references/kotlin/EntityWithSimpleProperties.kt +++ b/api_generator/tests/references/kotlin/EntityWithSimpleProperties.kt @@ -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" diff --git a/api_generator/tests/references/kotlin/EntityWithStrictArray.kt b/api_generator/tests/references/kotlin/EntityWithStrictArray.kt index 477a3d70f..108eca110 100644 --- a/api_generator/tests/references/kotlin/EntityWithStrictArray.kt +++ b/api_generator/tests/references/kotlin/EntityWithStrictArray.kt @@ -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, ) = EntityWithStrictArray( diff --git a/api_generator/tests/references/kotlin/EntityWithStringArrayProperty.kt b/api_generator/tests/references/kotlin/EntityWithStringArrayProperty.kt index 5d2ac7e5f..e438136da 100644 --- a/api_generator/tests/references/kotlin/EntityWithStringArrayProperty.kt +++ b/api_generator/tests/references/kotlin/EntityWithStringArrayProperty.kt @@ -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" diff --git a/api_generator/tests/references/kotlin/EntityWithStringEnumProperty.kt b/api_generator/tests/references/kotlin/EntityWithStringEnumProperty.kt index 0b011546d..7b948f184 100644 --- a/api_generator/tests/references/kotlin/EntityWithStringEnumProperty.kt +++ b/api_generator/tests/references/kotlin/EntityWithStringEnumProperty.kt @@ -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" diff --git a/api_generator/tests/references/kotlin/EntityWithStringEnumPropertyWithDefaultValue.kt b/api_generator/tests/references/kotlin/EntityWithStringEnumPropertyWithDefaultValue.kt index b7623dbc1..91d92b6cf 100644 --- a/api_generator/tests/references/kotlin/EntityWithStringEnumPropertyWithDefaultValue.kt +++ b/api_generator/tests/references/kotlin/EntityWithStringEnumPropertyWithDefaultValue.kt @@ -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"