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

Remove importresolver package #5

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
7beadd7
Use 4 spaces instead of 2 for default indent
pranjalvachaspati-toast Oct 12, 2021
5665e73
Remove message.stg
pranjalvachaspati-toast Oct 13, 2021
2cc5b3a
Remove Descriptor template
pranjalvachaspati-toast Oct 13, 2021
7ecc79f
Remove Descriptor template
pranjalvachaspati-toast Oct 13, 2021
14bd064
Remove dead code in header template and most of services template.
pranjalvachaspati-toast Oct 13, 2021
0861dcd
Set locale for decapitalize to get rid of compiler warning.
pranjalvachaspati-toast Oct 13, 2021
1cca8d0
Remove unused templates
pranjalvachaspati-toast Oct 13, 2021
151e25c
Create subpackage for annotators.
pranjalvachaspati-toast Oct 13, 2021
945b990
Merge branch 'kotlinpoet' of https://github.com/andrewparmet/protokt …
pranjalvachaspati-toast Oct 13, 2021
e24fb1d
Move methodType to kotlinpoet and get rid of services.stg
pranjalvachaspati-toast Oct 13, 2021
0369624
Remove Oneof stringtemplates.
pranjalvachaspati-toast Oct 13, 2021
5964ab4
Get rid of a couple renderers
pranjalvachaspati-toast Oct 13, 2021
46839ce
Migrate boxing & default value renderers away from StringTemplate.
pranjalvachaspati-toast Oct 14, 2021
f12ca3c
Remove sizeof stringtemplates.
pranjalvachaspati-toast Oct 15, 2021
9981c53
Add non-stringtemplate serializer generation
pranjalvachaspati-toast Oct 15, 2021
ec526a1
Get rid of renderers stringtemplate
pranjalvachaspati-toast Oct 15, 2021
91accd6
Remove options stringtemplate
pranjalvachaspati-toast Oct 15, 2021
8aec02e
--amend
pranjalvachaspati-toast Oct 15, 2021
8223782
Merge branch 'kotlinpoet' of https://github.com/andrewparmet/protokt …
pranjalvachaspati-toast Oct 15, 2021
4f4d40a
Convert deserializer to use kotlinpoet
pranjalvachaspati-toast Oct 16, 2021
d4af5e5
Create packages for import resolution and descriptor stuff
pranjalvachaspati-toast Oct 16, 2021
f38ffed
Create packages for import resolution and descriptor stuff
pranjalvachaspati-toast Oct 16, 2021
2125158
This merge required some additional changes - mostly changing Strings…
pranjalvachaspati-toast Oct 18, 2021
9b264a3
Additional changes from Strings to CodeBlocks
pranjalvachaspati-toast Oct 18, 2021
8bb76ce
Disable non-kotlinpoet import resolution
pranjalvachaspati-toast Oct 18, 2021
db9d992
Remove importresolution package
pranjalvachaspati-toast Oct 18, 2021
764782c
Merge branch 'kotlinpoet' of https://github.com/andrewparmet/protokt …
pranjalvachaspati-toast Oct 18, 2021
1f53989
Make fixes for PR comments
pranjalvachaspati-toast Oct 19, 2021
e84eae0
Use DSL typename in DeserializerAnnotator
pranjalvachaspati-toast Oct 19, 2021
a4193df
Remove dependency on stringtemplate
pranjalvachaspati-toast Oct 19, 2021
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
Prev Previous commit
Next Next commit
Merge branch 'kotlinpoet' of https://github.com/andrewparmet/protokt
…into remove-renderers

# Conflicts:
#	protokt-codegen/src/main/kotlin/com/toasttab/protokt/codegen/annotators/MessageAnnotator.kt
#	protokt-codegen/src/main/kotlin/com/toasttab/protokt/codegen/annotators/PropertyAnnotator.kt
#	protokt-codegen/src/main/kotlin/com/toasttab/protokt/codegen/template/Renderers.kt
#	protokt-codegen/src/main/resources/renderers.stg
  • Loading branch information
pranjalvachaspati-toast committed Oct 15, 2021
commit 822378210c60576be356b035961132c450a50261
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,18 @@ private constructor(
|var unknownFields: UnknownFieldSet.Builder? = null
|
|while (true) {
| when (deserializer.readTag()) {
| 0 ->
| return ${msg.name}(
| ${constructorLines(properties)}
| )
| when (deserializer.readTag()) {
| 0 ->
| return ${msg.name}(
| ${constructorLines(properties)}
| )
|${assignmentLines(deserializerInfo)}
| else ->
| unknownFields =
| (unknownFields ?: UnknownFieldSet.Builder()).also {
| it.add(deserializer.readUnknown())
| }
| }
| else ->
| unknownFields =
| (unknownFields ?: UnknownFieldSet.Builder()).also {
| it.add(deserializer.readUnknown())
| }
| }
|}
""".trimMargin()
)
Expand All @@ -131,19 +131,19 @@ private constructor(

private fun constructorLines(properties: List<PropertyInfo>) =
properties.joinToString("") {
deserializeWrapper(it) + ",\n "
deserializeWrapper(it) + ",\n "
} + "UnknownFieldSet.from(unknownFields)"

private fun assignmentLines(deserializerInfo: List<DeserializerInfo>) =
deserializerInfo.joinToString("\n") {
if (it.repeated) {
"""
| ${it.tag} ->
| ${it.assignment.fieldName} =
| ${it.assignment.value}
| ${it.tag} ->
| ${it.assignment.fieldName} =
| ${it.assignment.value}
""".trimMargin()
} else {
" ${it.tag} -> ${it.assignment.fieldName} = ${it.assignment.value}"
" ${it.tag} -> ${it.assignment.fieldName} = ${it.assignment.value}"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ class DslAnnotator(
dslLines() + "\n"
} +
"""
| unknownFields = this@${msg.name}.unknownFields
| dsl()
|}
""".trimMargin()
| unknownFields = this@${msg.name}.unknownFields
| dsl()
|}
""".trimMargin()
).replace(" ", "·")
)
.build()
Expand All @@ -63,7 +63,7 @@ class DslAnnotator(
TypeSpec.classBuilder(msg.name + "Dsl")
.addProperties(
properties.map {
PropertySpec.builder(it.name.removePrefix("`").removeSuffix("`"), TypeVariableName(it.dslPropertyType).copy(nullable = it.nullable && !it.dslPropertyType.endsWith("?")))
PropertySpec.builder(it.name.removePrefix("`").removeSuffix("`"), it.dslPropertyType)
.mutable(true)
.apply {
if (it.deprecation != null) {
Expand Down Expand Up @@ -124,7 +124,7 @@ class DslAnnotator(
"""
|return ${msg.name}(
|${buildLines()}
| unknownFields
| unknownFields
|)
""".trimMargin()
}
Expand All @@ -137,12 +137,12 @@ class DslAnnotator(

private fun dslLines() =
properties.joinToString("\n") {
" ${it.name} = this@${msg.name}.${it.name}"
" ${it.name} = this@${msg.name}.${it.name}"
}

private fun buildLines() =
properties.joinToString("\n") {
" ${deserializeWrapper(it)},"
" ${deserializeWrapper(it)},"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ import com.toasttab.protokt.codegen.annotators.OneofAnnotator.Companion.annotate
import com.toasttab.protokt.codegen.annotators.PropertyAnnotator.Companion.annotateProperties
import com.toasttab.protokt.codegen.annotators.SerializerAnnotator.Companion.annotateSerializerNew
import com.toasttab.protokt.codegen.impl.Deprecation.enclosingDeprecation
import com.toasttab.protokt.codegen.impl.Deprecation.handleDeprecation
import com.toasttab.protokt.codegen.impl.Deprecation.hasDeprecation
import com.toasttab.protokt.codegen.impl.Deprecation.renderOptions
import com.toasttab.protokt.codegen.impl.Implements.doesImplement
import com.toasttab.protokt.codegen.impl.Implements.implements
import com.toasttab.protokt.codegen.model.PPackage
import com.toasttab.protokt.codegen.protoc.Message
import com.toasttab.protokt.codegen.template.Message.Message.MessageInfo
import com.toasttab.protokt.codegen.template.Message.Message.Options
import com.toasttab.protokt.codegen.template.Message.Message.PropertyInfo
import com.toasttab.protokt.rt.KtGeneratedMessage
import com.toasttab.protokt.rt.KtMessage
Expand Down Expand Up @@ -98,19 +98,7 @@ private constructor(
.addMember("\"" + msg.fullProtobufTypeName + "\"")
.build()
)
if (messageInfo.deprecation != null) {
addAnnotation(
AnnotationSpec.builder(Deprecated::class)
.apply {
if (messageInfo.deprecation.message != null) {
addMember("\"" + messageInfo.deprecation.message + "\"")
} else {
addMember("\"deprecated in proto\"")
}
}
.build()
)
}
handleDeprecation(messageInfo.deprecation)
}

private fun TypeSpec.Builder.handleConstructor(
Expand All @@ -119,7 +107,7 @@ private constructor(
addSuperinterface(KtMessage::class)
addProperties(
properties.map {
PropertySpec.builder(it.name.removePrefix("`").removeSuffix("`"), TypeVariableName(it.propertyType))
PropertySpec.builder(it.name.removePrefix("`").removeSuffix("`"), it.propertyType)
.initializer(it.name)
.apply {
if (it.overrides) {
Expand All @@ -131,21 +119,7 @@ private constructor(
addKdoc(formatDoc(it.documentation))
}
}
.apply {
if (it.deprecation != null) {
addAnnotation(
AnnotationSpec.builder(Deprecated::class)
.apply {
if (it.deprecation.message != null) {
addMember("\"" + it.deprecation.message + "\"")
} else {
addMember("\"deprecated in proto\"")
}
}
.build()
)
}
}
.handleDeprecation(it.deprecation)
.build()
}
)
Expand All @@ -159,7 +133,7 @@ private constructor(
.addModifiers(KModifier.PRIVATE)
.addParameters(
properties.map {
ParameterSpec(it.name.removePrefix("`").removeSuffix("`"), TypeVariableName(it.propertyType))
ParameterSpec(it.name.removePrefix("`").removeSuffix("`"), it.propertyType)
}
)
.addParameter(
Expand Down Expand Up @@ -204,15 +178,15 @@ private constructor(
"""
|return other is ${msg.name} &&·
|${equalsLines(properties)}
| other.unknownFields == unknownFields
| other.unknownFields == unknownFields
""".trimMargin()
}
)
.build()
)

private fun equalsLines(properties: List<PropertyInfo>) =
properties.joinToString("\n") { " other.${it.name} == ${it.name} &&" }
properties.joinToString("\n") { " other.${it.name} == ${it.name} &&" }

private fun TypeSpec.Builder.handleHashCode(
properties: List<PropertyInfo>
Expand Down Expand Up @@ -254,7 +228,7 @@ private constructor(
"""
|return "${msg.name}(" +
|${toStringLines(properties)}
| "unknownFields=${"$"}unknownFields)"
| "unknownFields=${"$"}unknownFields)"
""".trimMargin()
}
)
Expand All @@ -263,7 +237,7 @@ private constructor(

private fun toStringLines(properties: List<PropertyInfo>) =
properties.joinToString("\n") {
" \"${it.name}=\$${it.name}\" +"
" \"${it.name}=\$${it.name}\" +"
}

private fun messageInfo() =
Expand Down Expand Up @@ -292,20 +266,6 @@ private constructor(
private fun messageIsTopLevel() =
ctx.enclosing.firstOrNone().fold({ false }, { it == msg })

private fun options(): Options {
val lengthAsOneLine =
ctx.enclosing.size * 4 +
4 + // companion indentation
63 + // `override fun deserialize(deserializer: KtMessageDeserializer): `
msg.name.length +
2 // ` {`

return Options(
wellKnownType = ctx.pkg == PPackage.PROTOKT,
longDeserializer = lengthAsOneLine > IDEAL_MAX_WIDTH
)
}

companion object {
const val IDEAL_MAX_WIDTH = 100

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private constructor(
if (!it.hasNonNullOption) {
"""
|if ${it.nonDefault(ctx)} {
| $resultVarName += ${sizeOfString(it)}
| $resultVarName += ${sizeOfString(it)}
|}
""".trimMargin()
} else {
Expand Down Expand Up @@ -95,8 +95,8 @@ private constructor(
f.fields
.sortedBy { it.number }.joinToString("\n") {
"""
| is ${condition(f, it, msg.name)} ->
| ${oneofSizeOfString(f, it)}
| is ${condition(f, it, msg.name)} ->
| ${oneofSizeOfString(f, it)}
""".trimMargin()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

package com.toasttab.protokt.codegen.annotators

import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.TypeVariableName
import com.squareup.kotlinpoet.asTypeName
import com.toasttab.protokt.codegen.annotators.Annotator.Context
import com.toasttab.protokt.codegen.annotators.PropertyDocumentationAnnotator.Companion.annotatePropertyDocumentation
import com.toasttab.protokt.codegen.impl.Deprecation.renderOptions
Expand Down Expand Up @@ -94,12 +98,35 @@ private constructor(
null
}

private fun annotateStandard(f: StandardField) = when {
f.map -> "Map<${resolveMapEntryTypes(f, ctx).kType}, ${resolveMapEntryTypes(f, ctx).vType}>"
f.repeated -> "List<${interceptTypeName(f, f.typePClass.renderName(ctx.pkg), ctx)}>"
interceptTypeName(f, f.typePClass.renderName(ctx.pkg), ctx) == "Any" -> f.typePClass.qualifiedName
else -> interceptTypeName(f, f.typePClass.renderName(ctx.pkg), ctx)
}
private fun annotateStandard(f: StandardField): TypeName =
if (f.map) {
val mapTypes = resolveMapEntryTypes(f, ctx)
Map::class
.asTypeName()
.parameterizedBy(
TypeVariableName(mapTypes.kType),
TypeVariableName(mapTypes.vType)
)
} else {
val parameter =
TypeVariableName(
interceptTypeName(
f,
f.typePClass.renderName(ctx.pkg),
ctx
)
)

if (f.repeated) {
List::class.asTypeName().parameterizedBy(parameter)
} else {
if (parameter.name == "Any") {
TypeVariableName(f.typePClass.qualifiedName)
} else {
parameter
}
}
}

private fun Field.defaultValue(ctx: Context) =
when (this) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package com.toasttab.protokt.codegen.annotators

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.ParameterizedTypeName
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.toasttab.protokt.codegen.template.Message.Message.PropertyInfo

fun deserializeType(p: PropertyInfo) =
if (p.repeated || p.map) {
"Mutable"
p.deserializeType as ParameterizedTypeName
ClassName(p.deserializeType.rawType.packageName, "Mutable" + p.deserializeType.rawType.simpleName)
.parameterizedBy(p.deserializeType.typeArguments)
.copy(nullable = true)
} else {
""
} + p.deserializeType
p.deserializeType
}

fun deserializeValue(p: PropertyInfo) =
if (p.repeated || p.wrapped || p.nullable || p.fieldType == "MESSAGE") {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Copyright (c) 2019 Toast Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -43,7 +43,7 @@ private constructor(
if (!it.hasNonNullOption) {
"""
|if ${it.nonDefault(ctx)} {
| ${serializeString(it)}
| ${serializeString(it)}
|}
""".trimMargin()
} else {
Expand Down Expand Up @@ -78,8 +78,8 @@ private constructor(
f.fields
.sortedBy { it.number }.joinToString("\n") {
"""
| is ${oneOfSer(f, it, msg.name).condition} ->
| ${serializeString(it, Some(f.fieldName))}
| is ${oneOfSer(f, it, msg.name).condition} ->
| ${serializeString(it, Some(f.fieldName))}
""".trimMargin()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.toasttab.protokt.codegen.impl

import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeSpec
import com.toasttab.protokt.codegen.annotators.Annotator.Context
import com.toasttab.protokt.codegen.annotators.deprecated
Expand Down Expand Up @@ -64,6 +65,28 @@ object Deprecation {
val message: String?
)

fun PropertySpec.Builder.handleDeprecation(renderOptions: RenderOptions?) =
apply {
if (renderOptions != null) {
addAnnotation(
AnnotationSpec.builder(Deprecated::class)
.handleDeprecationMessage(renderOptions.message.orEmpty())
.build()
)
}
}

fun TypeSpec.Builder.handleDeprecation(renderOptions: RenderOptions?) =
apply {
if (renderOptions != null) {
addAnnotation(
AnnotationSpec.builder(Deprecated::class)
.handleDeprecationMessage(renderOptions.message.orEmpty())
.build()
)
}
}

fun TypeSpec.Builder.handleDeprecation(deprecated: Boolean, message: String) {
if (deprecated) {
addAnnotation(
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.