Skip to content

Commit

Permalink
Add field level support for @deprecated annotation (#169)
Browse files Browse the repository at this point in the history
Fixes #1
  • Loading branch information
rahulreddym authored Oct 29, 2021
1 parent 9d13de3 commit 3a3b7a5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
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()
addDeprecatedAnnotation(field)
line(
"val ${field.extendeeKotlinType}.${field.kotlinFieldName}: ${
field.kotlinValueType(true)
} "
).indented {
line("get() = getExtension(${file.kotlinPackageName}.${field.kotlinFieldName})")
}.line()
line("@pbandk.Export")
addDeprecatedAnnotation(field)
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 -> {
addDeprecatedAnnotation(field)
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 ->
addDeprecatedAnnotation(field)
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 ->
addDeprecatedAnnotation(field)
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 addDeprecatedAnnotation(field: File.Field) {
when (field) {
is File.Field.Numbered -> if(field.options.deprecated == true) line("@Deprecated(message = \"Field marked deprecated in ${file.name}\")")
}
}
}
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

0 comments on commit 3a3b7a5

Please sign in to comment.