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

Added field level support for @Deprecated annotation #169

Merged
merged 5 commits into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ open class CodeGenerator(
private fun writeExtension(field: File.Field) {
when (field) {
is File.Field.Numbered -> {
line().line(
line()
addAnnotation(field, "@Deprecated(message = \"Field marked deprecated in ${file.name}\")")
line(
"val ${field.extendeeKotlinType}.${field.kotlinFieldName}: ${
field.kotlinValueType(true)
} "
).indented {
line("get() = getExtension(${file.kotlinPackageName}.${field.kotlinFieldName})")
}.line()
line("@pbandk.Export")
addAnnotation(field, "@Deprecated(message = \"Field marked deprecated in ${file.name}\")")
line("val ${field.kotlinFieldName} = pbandk.FieldDescriptor(").indented {
generateFieldDescriptorConstructorValues(
field,
Expand Down Expand Up @@ -102,7 +105,10 @@ open class CodeGenerator(
val fieldBegin = if (type.mapEntry) "override " else ""
type.fields.forEach { field ->
when (field) {
is File.Field.Numbered -> lineBegin(fieldBegin).writeConstructorField(field, true).lineEnd(",")
is File.Field.Numbered -> {
addAnnotation(field, "@Deprecated(message = \"Field marked deprecated in ${file.name}\")")
lineBegin(fieldBegin).writeConstructorField(field, true).lineEnd(",")
}
is File.Field.OneOf -> line("val ${field.kotlinFieldName}: ${field.kotlinTypeName}<*>? = null,")
}
}
Expand Down Expand Up @@ -150,6 +156,7 @@ open class CodeGenerator(
protected fun writeOneOfType(oneOf: File.Field.OneOf) {
line("sealed class ${oneOf.kotlinTypeName}<V>(value: V) : pbandk.Message.OneOf<V>(value) {").indented {
oneOf.fields.forEach { field ->
addAnnotation(field, "@Deprecated(message = \"Field marked deprecated in ${file.name}\")")
lineBegin("class ${oneOf.kotlinFieldTypeNames[field.name]}(")
lineMid("${field.kotlinFieldName}: ${field.kotlinValueType(false)}")
if (field.type != File.Field.Type.MESSAGE) lineMid(" = ${field.defaultValue}")
Expand All @@ -158,6 +165,7 @@ open class CodeGenerator(
}.line("}").line()

oneOf.fields.forEach { field ->
addAnnotation(field, "@Deprecated(message = \"Field marked deprecated in ${file.name}\")")
line("val ${field.kotlinFieldName}: ${field.kotlinValueType(false)}?").indented {
lineBegin("get() = ")
lineMid("(${oneOf.kotlinFieldName} as? ${oneOf.kotlinTypeName}.${oneOf.kotlinFieldTypeNames[field.name]})")
Expand Down Expand Up @@ -648,4 +656,10 @@ open class CodeGenerator(
generateFieldOptions(field.options)
line("value = $fullTypeName::${field.kotlinFieldName}")
}

private fun addAnnotation(field: File.Field, annotation: String) {
rahulreddym marked this conversation as resolved.
Show resolved Hide resolved
when (field) {
is File.Field.Numbered -> if(field.options.deprecated == true) line(annotation)
}
}
}
1 change: 1 addition & 0 deletions runtime/src/commonMain/kotlin/pbandk/wkt/descriptor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,7 @@ data class FileOptions(
val javaPackage: String? = null,
val javaOuterClassname: String? = null,
val javaMultipleFiles: Boolean? = null,
@Deprecated(message = "Field marked deprecated in google/protobuf/descriptor.proto")
val javaGenerateEqualsAndHash: Boolean? = null,
val javaStringCheckUtf8: Boolean? = null,
val optimizeFor: pbandk.wkt.FileOptions.OptimizeMode? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ data class MultipleCustomOptions(

@pbandk.Export
data class MultipleCustomOptionsPlusDeprecated(
@Deprecated(message = "Field marked deprecated in pbandk/testpb/custom_options.proto")
val multipleDeprecated: String = "",
override val unknownFields: Map<Int, pbandk.UnknownField> = emptyMap()
) : pbandk.Message {
Expand Down