Skip to content

Commit 9063331

Browse files
committed
Simplify presence check
1 parent 76409fe commit 9063331

File tree

11 files changed

+586
-450
lines changed

11 files changed

+586
-450
lines changed

protobuf/protobuf-core/src/commonMain/generated-code/kotlin-multiplatform/com/google/protobuf/kotlin/_rpc_internal/Api.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ public class ApiInternal: com.google.protobuf.kotlin.Api, kotlinx.rpc.protobuf.i
3939
if (other == null || this::class != other::class) return false
4040
other as ApiInternal
4141
other.checkRequiredFields()
42+
if (presenceMask != other.presenceMask) return false
4243
if (name != other.name) return false
4344
if (methods != other.methods) return false
4445
if (options != other.options) return false
4546
if (version != other.version) return false
46-
if (presenceMask[0] != other.presenceMask[0] || presenceMask[0] && sourceContext != other.sourceContext) return false
47+
if (presenceMask[0] && sourceContext != other.sourceContext) return false
4748
if (mixins != other.mixins) return false
4849
if (syntax != other.syntax) return false
4950
return true

protobuf/protobuf-core/src/commonMain/generated-code/kotlin-multiplatform/com/google/protobuf/kotlin/_rpc_internal/Struct.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ public class StructInternal: com.google.protobuf.kotlin.Struct, kotlinx.rpc.prot
6565
if (other == null || this::class != other::class) return false
6666
other as FieldsEntryInternal
6767
other.checkRequiredFields()
68+
if (presenceMask != other.presenceMask) return false
6869
if (key != other.key) return false
69-
if (presenceMask[0] != other.presenceMask[0] || presenceMask[0] && value != other.value) return false
70+
if (presenceMask[0] && value != other.value) return false
7071
return true
7172
}
7273

protobuf/protobuf-core/src/commonMain/generated-code/kotlin-multiplatform/com/google/protobuf/kotlin/_rpc_internal/Type.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ public class TypeInternal: com.google.protobuf.kotlin.Type, kotlinx.rpc.protobuf
3939
if (other == null || this::class != other::class) return false
4040
other as TypeInternal
4141
other.checkRequiredFields()
42+
if (presenceMask != other.presenceMask) return false
4243
if (name != other.name) return false
4344
if (fields != other.fields) return false
4445
if (oneofs != other.oneofs) return false
4546
if (options != other.options) return false
46-
if (presenceMask[0] != other.presenceMask[0] || presenceMask[0] && sourceContext != other.sourceContext) return false
47+
if (presenceMask[0] && sourceContext != other.sourceContext) return false
4748
if (syntax != other.syntax) return false
4849
if (edition != other.edition) return false
4950
return true
@@ -236,10 +237,11 @@ public class EnumInternal: com.google.protobuf.kotlin.Enum, kotlinx.rpc.protobuf
236237
if (other == null || this::class != other::class) return false
237238
other as EnumInternal
238239
other.checkRequiredFields()
240+
if (presenceMask != other.presenceMask) return false
239241
if (name != other.name) return false
240242
if (enumvalue != other.enumvalue) return false
241243
if (options != other.options) return false
242-
if (presenceMask[0] != other.presenceMask[0] || presenceMask[0] && sourceContext != other.sourceContext) return false
244+
if (presenceMask[0] && sourceContext != other.sourceContext) return false
243245
if (syntax != other.syntax) return false
244246
if (edition != other.edition) return false
245247
return true
@@ -395,8 +397,9 @@ public class OptionInternal: com.google.protobuf.kotlin.Option, kotlinx.rpc.prot
395397
if (other == null || this::class != other::class) return false
396398
other as OptionInternal
397399
other.checkRequiredFields()
400+
if (presenceMask != other.presenceMask) return false
398401
if (name != other.name) return false
399-
if (presenceMask[0] != other.presenceMask[0] || presenceMask[0] && value != other.value) return false
402+
if (presenceMask[0] && value != other.value) return false
400403
return true
401404
}
402405

protobuf/protobuf-core/src/commonMain/kotlin/kotlinx/rpc/protobuf/internal/BitSet.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,22 @@ public class BitSet(public val size: Int) {
6666
}
6767
return true
6868
}
69+
70+
override fun equals(other: Any?): Boolean {
71+
if (this === other) return true
72+
if (other == null || this::class != other::class) return false
73+
74+
other as BitSet
75+
76+
if (size != other.size) return false
77+
if (!data.contentEquals(other.data)) return false
78+
79+
return true
80+
}
81+
82+
override fun hashCode(): Int {
83+
var result = size
84+
result = 31 * result + data.contentHashCode()
85+
return result
86+
}
6987
}

protoc-gen/protobuf/src/main/kotlin/kotlinx/rpc/protoc/gen/ModelToProtobufKotlinCommonGenerator.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,14 @@ class ModelToProtobufKotlinCommonGenerator(
285285
addLine("if (other == null || this::class != other::class) return false")
286286
addLine("other as ${declaration.internalClassName()}")
287287
addLine("other.checkRequiredFields()")
288+
if (declaration.presenceMaskSize != 0) {
289+
addLine("if (presenceMask != other.presenceMask) return false")
290+
}
288291
if (fields.isNotEmpty()) {
289292
fields.forEach { field ->
290293
if (field.presenceIdx != null) {
291294
fieldEqualsCheck(
292-
presenceCheck = "presenceMask[${field.presenceIdx}] != other.presenceMask[${field.presenceIdx}] || presenceMask[${field.presenceIdx}] && ",
295+
presenceCheck = "presenceMask[${field.presenceIdx}] && ",
293296
field = field,
294297
)
295298
} else {

tests/protobuf-conformance/src/main/generated-code/kotlin-multiplatform/com/google/protobuf/conformance/_rpc_internal/Conformance.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,11 @@ class ConformanceRequestInternal: com.google.protobuf.conformance.ConformanceReq
185185
if (other == null || this::class != other::class) return false
186186
other as ConformanceRequestInternal
187187
other.checkRequiredFields()
188+
if (presenceMask != other.presenceMask) return false
188189
if (requestedOutputFormat != other.requestedOutputFormat) return false
189190
if (messageType != other.messageType) return false
190191
if (testCategory != other.testCategory) return false
191-
if (presenceMask[0] != other.presenceMask[0] || presenceMask[0] && jspbEncodingOptions != other.jspbEncodingOptions) return false
192+
if (presenceMask[0] && jspbEncodingOptions != other.jspbEncodingOptions) return false
192193
if (printUnknownFields != other.printUnknownFields) return false
193194
if (payload != other.payload) return false
194195
return true

0 commit comments

Comments
 (0)