Skip to content

Commit

Permalink
fix: option's resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
y9vad9 committed Nov 13, 2024
1 parent 555ae6a commit 1114c2c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ private class SchemaAsRSResolver(
override fun resolveField(typeMemberUrl: RSTypeMemberUrl): RSField? {
return schema.getField(
ProtoMember.get(
ProtoType.get(typeMemberUrl.typeUrl.value),
typeMemberUrl.memberName
ProtoType.get(typeMemberUrl.typeUrl.value.replace("type.googleapis.com/", "")),
typeMemberUrl.memberName,
)
)?.asRMField()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.timemates.rrpc.generator.kotlin.adapter

internal object Constant {
val GENERATED_COMMENT = """
This file is generated by the `RRpc/rrpc-kotlin` library.
This file is generated by the `timemates/rrpc-kotlin` library.
WARNING: DO NOT MODIFY THIS FILE MANUALLY!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,18 @@ public object KotlinMetadataSchemaAdapter : SchemaAdapter {
GenerationOptions.METADATA_SCOPE_NAME,
)

@OptIn(NonPlatformSpecificAccess::class)
override fun process(
options: GenerationOptions,
resolver: RSResolver,
): RSResolver = with(KotlinPluginOptions(options)) {
if (metadataGeneration == MetadataGenerationType.SCOPED && metadataScopeName == null)
throw GenerationException("Metadata config requires generation to be scoped, but name isn't provided.")

resolver.resolveAvailableFiles()
.filterNot { file -> file.packageName.value.startsWith("wire") }
.toList()
.let {
CombinedFilesMetadataGenerator.generate(
name = metadataScopeName,
scoped = metadataGeneration == MetadataGenerationType.SCOPED,
resolver = RSResolver(it)
)
}.apply {
writeTo(output.toNioPath())
}
CombinedFilesMetadataGenerator.generate(
name = metadataScopeName,
scoped = metadataGeneration == MetadataGenerationType.SCOPED,
resolver = resolver,
).writeTo(output.toNioPath())

return resolver
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import com.squareup.kotlinpoet.buildCodeBlock
import com.squareup.kotlinpoet.withIndent
import org.timemates.rrpc.codegen.typemodel.LibClassNames
import org.timemates.rrpc.common.schema.RSResolver
import org.timemates.rrpc.common.schema.annotations.NonPlatformSpecificAccess
import org.timemates.rrpc.generator.kotlin.adapter.internal.ext.newline
import kotlin.random.Random

public object CombinedFilesMetadataGenerator {
@OptIn(NonPlatformSpecificAccess::class)
public fun generate(
name: String?,
scoped: Boolean,
Expand All @@ -29,7 +31,12 @@ public object CombinedFilesMetadataGenerator {
delegate = buildCodeBlock {
add("RMResolver(")
withIndent {
resolver.resolveAvailableFiles().forEach { file ->
resolver.resolveAvailableFiles()
.filterNot { file ->
file.packageName.value.startsWith("wire")
|| file.packageName.value.startsWith("google.protobuf")
}
.forEach { file ->
newline()
add(FileMetadataGenerator.generate(file, resolver))
add(",")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,31 @@ internal object OptionsMetadataGenerator {
newline()
add("listOf(")
options.list.forEach { option ->
val field = resolver.resolveField(option.fieldUrl) ?: return@forEach
val field = resolver.resolveField(option.fieldUrl)!! ?: return@forEach
withIndent {
newline()
add("%T(", LibClassNames.RS.Option)
withIndent {
newline()
add("name = %S,", option.name)
newline()
add("tag = %L,", field.tag)
newline()
add("fieldUrl = %1T(", LibClassNames.RS.TypeMemberUrl, option.fieldUrl.typeUrl)
newline()
add("fieldUrl = %T(", LibClassNames.RS.TypeMemberUrl)
withIndent {
newline()
add("typeUrl = %S,", option.fieldUrl.typeUrl)
newline()
add("memberName = %S,", option.fieldUrl.memberName)
}
newline()
add("),")
newline()
option.value?.let { value ->
add("value = %P,", generateValue(value))
add("value = %L,", generateValue(value))
}
}
newline()
add("),")
}
}
Expand All @@ -53,7 +59,7 @@ internal object OptionsMetadataGenerator {
private fun generateValue(value: RSOption.Value): CodeBlock {
return buildCodeBlock {
when (value) {
is RSOption.Value.Raw -> add("%T(%S)", LibClassNames.RS.OptionValueRaw)
is RSOption.Value.Raw -> add("%T(%S)", LibClassNames.RS.OptionValueRaw, value.string)
is RSOption.Value.RawMap -> {
add("%T(", LibClassNames.RS.Option)
newline()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.timemates.rrpc.generator.kotlin.options

import okio.Path
import okio.Path.Companion.toPath
import org.timemates.rrpc.codegen.configuration.GenerationOptions
import org.timemates.rrpc.codegen.exception.GenerationException

Expand All @@ -10,10 +9,12 @@ public value class KotlinPluginOptions(private val options: GenerationOptions) {
public val isClientGenerationEnabled: Boolean get() = options[GenerationOptions.KOTLIN_CLIENT_GENERATION] ?: true
public val isServerGenerationEnabled: Boolean get() = options[GenerationOptions.KOTLIN_SERVER_GENERATION] ?: true
public val isTypesGenerationEnabled: Boolean get() = options[GenerationOptions.KOTLIN_TYPE_GENERATION] ?: true
public val output: Path get() = options[GenerationOptions.KOTLIN_OUTPUT] ?: throw GenerationException("Kotlin output folder was not specified.")
public val output: Path
get() = options[GenerationOptions.KOTLIN_OUTPUT]
?: throw GenerationException("Kotlin output folder was not specified.")

public val metadataGeneration: MetadataGenerationType get() =
options[GenerationOptions.METADATA_GENERATION]
?: MetadataGenerationType.DISABLED
public val metadataGeneration: MetadataGenerationType
get() = options[GenerationOptions.METADATA_GENERATION]
?: MetadataGenerationType.DISABLED
public val metadataScopeName: String? get() = options[GenerationOptions.METADATA_SCOPE_NAME]
}

0 comments on commit 1114c2c

Please sign in to comment.