Skip to content

Commit

Permalink
Remove out modifier from generic parameters of MessageOneof methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleg-Melnik committed Oct 11, 2024
1 parent 8b7785d commit 04bfc26
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@

package io.spine.chords.codegen.plugins

import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier.OVERRIDE
import com.squareup.kotlinpoet.KModifier.PRIVATE
import com.squareup.kotlinpoet.KModifier.PUBLIC
import com.squareup.kotlinpoet.ParameterizedTypeName
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.WildcardTypeName
import com.squareup.kotlinpoet.asClassName
import io.spine.chords.runtime.MessageOneof
import io.spine.protodata.Field
Expand Down Expand Up @@ -87,10 +88,10 @@ internal class MessageOneofObjectGenerator(
* The generated code for some `oneof` filed looks like the following:
* ```
* public object IpAddressValueOneof : MessageOneof<IpAddress> {
* private val fieldMap: Map<Int, MessageField<IpAddress, *>> =
* private val fieldMap: Map<Int, MessageField<IpAddress, Any>> =
* mapOf(
* 1 to IpAddressDef.ipv4,
* 2 to IpAddressDef.ipv6
* 1 to IpAddressDef.ipv4 as MessageField<IpAddress, Any>,
* 2 to IpAddressDef.ipv6 as MessageField<IpAddress, Any>
* )
*
* public override val name: String = "value"
Expand All @@ -109,7 +110,7 @@ internal class MessageOneofObjectGenerator(
): TypeSpec {
val messageFieldType = messageFieldClassName.parameterizedBy(
messageFullClassName,
WildcardTypeName.producerOf(messageFieldValueTypeAlias)
messageFieldValueTypeAlias
)
val fieldMapType = Map::class.asClassName().parameterizedBy(
Int::class.asClassName(),
Expand All @@ -129,10 +130,17 @@ internal class MessageOneofObjectGenerator(
.addProperty(
PropertySpec
.builder("fieldMap", fieldMapType, PRIVATE)
.addAnnotation(
buildSuppressAnnotation(
"UNCHECKED_CAST",
"RemoveRedundantQualifierName"
)
)
.initializer(
fieldMapInitializer(
oneofFields,
messageTypeName.messageDefClassName()
messageTypeName.messageDefClassName(),
messageFieldType
)
)
.build()
Expand Down Expand Up @@ -161,27 +169,41 @@ internal class MessageOneofObjectGenerator(
}
}

/**
* Builds `@Suppress` annotation with given [warnings].
*/
@Suppress("SameParameterValue")
private fun buildSuppressAnnotation(vararg warnings: String) =
AnnotationSpec.builder(Suppress::class.asClassName())
.also { builder ->
warnings.forEach { warning ->
builder.addMember("%S", warning)
}
}
.build()

/**
* Generates initialization code for the `fieldMap` property
* of the [MessageOneof] implementation.
*
* The generated code looks like the following:
* ```
* mapOf(
* 1 to IpAddressDef.ipv4,
* 2 to IpAddressDef.ipv6
* 1 to IpAddressDef.ipv4 as MessageField<IpAddressDef, Any>,
* 2 to IpAddressDef.ipv6 as MessageField<IpAddressDef, Any>
* )
* ```
*/
private fun fieldMapInitializer(
fields: Iterable<Field>,
generatedClassName: String
messageDefClassName: String,
messageField: ParameterizedTypeName
): String {
return fields.joinToString(
",${lineSeparator()}",
"mapOf(${lineSeparator()}",
")"
) {
"${it.number} to $generatedClassName.${it.name.javaCase()}"
"${it.number} to $messageDefClassName.${it.name.javaCase()} as $messageField"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public interface MessageOneof<T : Message> {
/**
* Returns collection of [MessageField]s declared by this `oneof`.
*/
public val fields: Collection<MessageField<T, out MessageFieldValue>>
public val fields: Collection<MessageField<T, MessageFieldValue>>

/**
* Returns [MessageField] that is currently set in this oneof.
*/
public fun selectedField(message: T): MessageField<T, out MessageFieldValue>?
public fun selectedField(message: T): MessageField<T, MessageFieldValue>?
}

0 comments on commit 04bfc26

Please sign in to comment.