Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement a converter to dynamic message #212

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
2dd4541
implement a converter to dynamic message
andrewparmet Dec 30, 2023
dd96e8e
api dump
andrewparmet Dec 30, 2023
72fbb9f
fix
andrewparmet Dec 30, 2023
da3ff2e
internal
andrewparmet Dec 30, 2023
58b1d4c
reorganize shared but unpublished code
andrewparmet Jan 2, 2024
e28dd17
lint
andrewparmet Jan 2, 2024
3659cb2
move over what is needed to infer protobuf type names
andrewparmet Jan 2, 2024
cd757ca
map value well known wrapper types do not work correctly
andrewparmet Jan 2, 2024
21d9534
maps work
andrewparmet Jan 2, 2024
283d20d
compilation
andrewparmet Jan 2, 2024
eca1983
more tests
andrewparmet Jan 2, 2024
8e96790
api dump
andrewparmet Jan 2, 2024
f980afa
fix field presence lookup
andrewparmet Jan 3, 2024
1036d0e
api dump
andrewparmet Jan 3, 2024
b2b0e1a
more api cleanup
andrewparmet Jan 3, 2024
786ee1d
fix map wrapper types
andrewparmet Jan 3, 2024
9d426aa
some cleanup
andrewparmet Jan 3, 2024
5f6a280
mostly clean up map wrapper types
andrewparmet Jan 3, 2024
3689bc8
isolate more and get tests passing
andrewparmet Jan 3, 2024
c783115
fix
andrewparmet Jan 3, 2024
437909a
fix tags in codegen for large field numbers
andrewparmet Jan 4, 2024
d1a4417
api dump
andrewparmet Jan 4, 2024
deddc2d
Merge branch 'fix-tags-for-large-numbers' into implement-converter-to…
andrewparmet Jan 4, 2024
d357e38
change strategy for sharing potentially unpublished code
andrewparmet Jan 4, 2024
e2e0379
lint
andrewparmet Jan 4, 2024
c679e4f
Merge branch 'change-strategy-for-common-unpublished-code' into imple…
andrewparmet Jan 4, 2024
33206b3
fix map wrapper types for well known wrappers
andrewparmet Jan 5, 2024
dbcd52c
Merge branch 'fix-map-wrapper-types' into implement-converter-to-dyna…
andrewparmet Jan 5, 2024
ed9c87b
remove guava
andrewparmet Jan 6, 2024
294fb53
rm guava
andrewparmet Jan 6, 2024
3af2204
rm line
andrewparmet Jan 6, 2024
d150db2
reflect need not be compatible with android
andrewparmet Jan 6, 2024
0e82a08
turns out this line was needed
andrewparmet Jan 6, 2024
301a9e2
Update build.gradle.kts
andrewparmet Jan 9, 2024
d50dd74
Merge branch 'main' into implement-converter-to-dynamic-message
andrewparmet Feb 7, 2024
af1af55
Update FieldTypeExt.kt
andrewparmet Feb 7, 2024
a171c1e
Merge branch 'main' into implement-converter-to-dynamic-message
andrewparmet Mar 17, 2024
ad92a9f
no data objects
andrewparmet Mar 17, 2024
ae1c609
don't allow conversion of null
andrewparmet Apr 22, 2024
dc621f5
Merge branch 'main' into implement-converter-to-dynamic-message
andrewparmet May 8, 2024
27ff3cc
classpath scan only in tests
andrewparmet May 8, 2024
5eaae73
apidump
andrewparmet May 8, 2024
c7e643e
update readme
andrewparmet May 8, 2024
bc8cea8
space
andrewparmet May 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
implement a converter to dynamic message
  • Loading branch information
andrewparmet committed Dec 30, 2023
commit 2dd454150e9c34915e2365360dd0890c3c32bbd6
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/LocalProtoktBuild.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import protokt.v1.gradle.CODEGEN_NAME
import protokt.v1.gradle.configureProtokt
import java.io.File

fun Project.localProtokt() {
configureProtokt(this, null) {
fun Project.localProtokt(disableJava: Boolean = true) {
configureProtokt(this, null, disableJava) {
"$rootDir/protokt-codegen/build/install/$CODEGEN_NAME/bin/$CODEGEN_NAME"
}

Expand Down
7 changes: 5 additions & 2 deletions buildSrc/src/main/kotlin/protokt/v1/gradle/ProtobufBuild.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import java.net.URLEncoder
internal fun configureProtobufPlugin(
project: Project,
ext: ProtoktExtension,
disableJava: Boolean,
binaryPath: String
) {
project.apply<ProtobufPlugin>()
Expand All @@ -52,8 +53,10 @@ internal fun configureProtobufPlugin(

generateProtoTasks {
for (task in all()) {
task.builtins {
findByName("java")?.run(::remove)
if (disableJava) {
task.builtins {
findByName("java")?.run(::remove)
}
}

task.plugins {
Expand Down
9 changes: 7 additions & 2 deletions buildSrc/src/main/kotlin/protokt/v1/gradle/ProtoktBuild.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,15 @@ const val EXTENSIONS = "protoktExtensions"

const val TEST_EXTENSIONS = "testProtoktExtensions"

fun configureProtokt(project: Project, protoktVersion: Any?, resolveBinary: () -> String) {
fun configureProtokt(
project: Project,
protoktVersion: Any?,
disableJava: Boolean = true,
resolveBinary: () -> String
) {
injectKotlinPluginsIntoProtobufGradle()
val ext = project.extensions.create<ProtoktExtension>("protokt")
configureProtobufPlugin(project, ext, resolveBinary())
configureProtobufPlugin(project, ext, disableJava, resolveBinary())

project.createExtensionConfigurationsAndConfigureProtobuf()

Expand Down
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

[versions]
autoService = "1.0.1"
classgraph = "4.8.153"
grpc-java = "1.58.0"
grpc-kotlin = "1.4.0"
guava = "33.0.0-android"
kotlinLogging = "5.1.0"
kotlinPoet = "1.14.2"
kotlinx-coroutines = "1.6.0"
Expand Down Expand Up @@ -60,9 +62,11 @@ wire = { id = "com.squareup.wire", version.ref = "wire" }
[libraries]
autoService = { module = "com.google.auto.service:auto-service", version.ref = "autoService" }
autoServiceAnnotations = { module = "com.google.auto.service:auto-service-annotations", version.ref = "autoService" }
classgraph = { module = "io.github.classgraph:classgraph", version.ref = "classgraph" }
grpc-kotlin-gen = { module = "io.grpc:protoc-gen-grpc-kotlin", version.ref = "grpc-kotlin" }
grpc-netty = { module = "io.grpc:grpc-netty", version.ref = "grpc-java" }
grpc-stub = { module = "io.grpc:grpc-stub", version.ref = "grpc-java" }
guava = { module = "com.google.guava:guava", version.ref = "guava" }
kotlinLogging = { module = "io.github.oshai:kotlin-logging", version.ref = "kotlinLogging" }
kotlinPoet = { module = "com.squareup:kotlinpoet", version.ref = "kotlinPoet" }
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ object CodeGenerator {

fun generate(contents: ProtoFileContents) =
contents.types.flatMap {
generate(
it,
Context(emptyList(), contents.info)
)
.map { type -> GeneratedType(it, type) }
generate(it, Context(emptyList(), contents.info)).map(::GeneratedType)
}

fun generate(type: TopLevelType, ctx: Context): Iterable<TypeSpec> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private class DeserializerGenerator(
beginControlFlow("when (deserializer.readTag())")
val constructor =
buildCodeBlock {
add("0·->·return·%T(\n", msg.className)
add("0u·->·return·%T(\n", msg.className)
withIndent {
constructorLines(properties).forEach(::add)
}
Expand All @@ -92,7 +92,7 @@ private class DeserializerGenerator(
addStatement("%L", constructor)
deserializerInfo.forEach {
addStatement(
"%L -> %N = %L",
"%Lu -> %N = %L",
it.tag,
it.fieldName,
it.value
Expand Down Expand Up @@ -156,7 +156,7 @@ private class DeserializerGenerator(
CodeBlock.of("%T.from(unknownFields)", UnknownFieldSet::class)

private class DeserializerInfo(
val tag: Int,
val tag: UInt,
val fieldName: String,
val value: CodeBlock
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ private class MapEntryGenerator(
beginControlFlow("when (deserializer.readTag())")
addStatement("%L", constructOnZero(value))
addStatement(
"${key.tag.value} -> key = %L",
"${key.tag.value}u -> key = %L",
deserialize(key, ctx)
)
addStatement(
"${value.tag.value} -> value = %L",
"${value.tag.value}u -> value = %L",
deserialize(value, ctx)
)
endControlFlow()
Expand Down Expand Up @@ -170,7 +170,7 @@ private class MapEntryGenerator(

private fun constructOnZero(f: StandardField) =
buildCodeBlock {
add("0 -> return %T(key, value", msg.className)
add("0u -> return %T(key, value", msg.className)
if (f.type == FieldType.Message) {
add(" ?: %T{}", value.className)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,10 @@ class ProtoFileContents(
)

class GeneratedType(
val rawType: TopLevelType,
val typeSpec: TypeSpec
)

sealed class Tag(val value: Int) : Comparable<Tag> {
sealed class Tag(val value: UInt) : Comparable<Tag> {
class Packed(
number: Int
) : Tag(computeTag(number, 2))
Expand All @@ -188,4 +187,4 @@ sealed class Tag(val value: Int) : Comparable<Tag> {
}

private fun computeTag(fieldNumber: Int, wireType: Int) =
(fieldNumber shl 3) or wireType
(fieldNumber shl 3).toUInt() or wireType.toUInt()
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,13 @@ inline fun <reified T : KtMessage> Any.unpack(deserializer: KtDeserializer<T>):
return deserializer.deserialize(value)
}

@Suppress("DEPRECATION")
inline fun <reified T : KtMessage> Any.isA() =
typeUrl.substringAfterLast('/') ==
(
T::class.java.getAnnotation(KtGeneratedMessage::class.java)?.fullTypeName
?: T::class.java.getAnnotation(com.toasttab.protokt.rt.KtGeneratedMessage::class.java)?.fullTypeName
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was removed a few PRs ago so it's dead code

?: error("class ${T::class} has no protokt generated message annotation")
)

@Suppress("DEPRECATION")
private fun fullTypeName(klass: KClass<*>) =
klass.java.getAnnotation(KtGeneratedMessage::class.java)?.fullTypeName
?: klass.java.getAnnotation(com.toasttab.protokt.rt.KtGeneratedMessage::class.java)?.fullTypeName
?: error("class $klass has no protokt generated message annotation")
65 changes: 65 additions & 0 deletions protokt-reflect/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2023 Toast, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import com.google.protobuf.gradle.proto

/*
* Copyright (c) 2023 Toast, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

plugins {
id("protokt.multiplatform-conventions")
}

localProtokt(false)
enablePublishing()
compatibleWithAndroid()
trackKotlinApiCompatibility()

kotlin {
sourceSets {
val jvmMain by getting {
dependencies {
api(project(":protokt-core"))
api(libs.protobuf.java)

implementation(kotlin("reflect"))
implementation(libs.guava)
implementation(libs.classgraph)
}
}
}
}

tasks.withType<JavaCompile> { enabled = true }

sourceSets {
main {
proto {
srcDir("../extensions/protokt-extensions-lite/src/main/proto")
}
}
}
Loading