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

Avoid deprecation warnings in generated code #1589

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 1 addition & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ lazy val runtime = (projectMatrix in file("scalapb-runtime"))
Compile / unmanagedResourceDirectories += (LocalRootProject / baseDirectory).value / "protobuf",
scalacOptions ++= (if (!isScala3.value)
Seq(
"-P:silencer:globalFilters=avaGenerateEqualsAndHash in class .* is deprecated",
"-P:silencer:lineContentFilters=import scala.collection.compat._"
)
else Nil),
Expand Down Expand Up @@ -431,8 +430,7 @@ lazy val e2e = (projectMatrix in file("e2e"))
.settings(
scalacOptions ++= (if (!isScala3.value)
Seq(
"-P:silencer:globalFilters=value deprecatedInt32 in class TestDeprecatedFields is deprecated",
"-P:silencer:pathFilters=custom_options_use;CustomAnnotationProto.scala;TestDeprecatedFields.scala",
"-P:silencer:pathFilters=custom_options_use;CustomAnnotationProto.scala",
"-P:silencer:lineContentFilters=import com.thesamet.pb.MisplacedMapper.weatherMapper"
)
else Nil),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,19 @@ class DescriptorImplicits private[compiler] (
Nil
}

def containsADeprecatedField = message.fields.exists(_.getOptions.getDeprecated)

private[this] def ignoreDeprecatedFieldsAnnotation: Option[String] = {
if (containsADeprecatedField)
Some(ProtobufGenerator.ignoreDeprecatedAnnotation)
else
None
}

def annotationList: Seq[String] = {
deprecatedAnnotation ++ messageOptions.getAnnotationsList().asScala
deprecatedAnnotation ++ ignoreDeprecatedFieldsAnnotation.toList ++ messageOptions
.getAnnotationsList()
.asScala
}

def companionAnnotationList: Seq[String] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ class ProtobufGenerator(
def generateToJavaProto(message: Descriptor)(printer: FunctionalPrinter): FunctionalPrinter = {
val myFullScalaName = message.scalaType.fullName
printer
.when(message.containsADeprecatedField)(_.add(ProtobufGenerator.ignoreDeprecatedAnnotation))
.add(s"def toJavaProto(scalaPbSource: $myFullScalaName): ${message.javaTypeName} = {")
.indent
.add(s"val javaPbOut = ${message.javaTypeName}.newBuilder")
Expand All @@ -780,6 +781,7 @@ class ProtobufGenerator(
def generateFromJavaProto(message: Descriptor)(printer: FunctionalPrinter): FunctionalPrinter = {
val myFullScalaName = message.scalaType.fullName
printer
.when(message.containsADeprecatedField)(_.add(ProtobufGenerator.ignoreDeprecatedAnnotation))
.add(
s"def fromJavaProto(javaPbSource: ${message.javaTypeName}): $myFullScalaName = $myFullScalaName("
)
Expand Down Expand Up @@ -961,27 +963,36 @@ class ProtobufGenerator(
.indent
.print(message.fields) { case (printer, field) =>
val fieldName = field.scalaName.asSymbol

val deprecatedAnnotation: String = {
if (field.getOptions.getDeprecated) {
ProtobufGenerator.deprecatedAnnotation + " "
} else {
""
}
}

if (!field.isInOneof) {
if (field.supportsPresence) {
val optionLensName = "optional" + field.upperScalaName
printer
.add(
s"""def $fieldName: ${lensType(
s"""${deprecatedAnnotation}def $fieldName: ${lensType(
field.singleScalaTypeName
)} = field(_.${field.getMethod})((c_, f_) => c_.copy($fieldName = _root_.scala.Option(f_)))
|def ${optionLensName}: ${lensType(
|${deprecatedAnnotation}def ${optionLensName}: ${lensType(
field.scalaTypeName
)} = field(_.$fieldName)((c_, f_) => c_.copy($fieldName = f_))""".stripMargin
)
} else
printer.add(
s"def $fieldName: ${lensType(field.scalaTypeName)} = field(_.$fieldName)((c_, f_) => c_.copy($fieldName = f_))"
s"${deprecatedAnnotation}def $fieldName: ${lensType(field.scalaTypeName)} = field(_.$fieldName)((c_, f_) => c_.copy($fieldName = f_))"
)
} else {
val oneofName = field.getContainingOneof.scalaName.nameSymbol
printer
.add(
s"def $fieldName: ${lensType(field.scalaTypeName)} = field(_.${field.getMethod})((c_, f_) => c_.copy($oneofName = ${field.oneOfTypeName
s"${deprecatedAnnotation}def $fieldName: ${lensType(field.scalaTypeName)} = field(_.${field.getMethod})((c_, f_) => c_.copy($oneofName = ${field.oneOfTypeName
.fullNameWithMaybeRoot(message)}(f_)))"
)
}
Expand Down Expand Up @@ -1772,6 +1783,9 @@ object ProtobufGenerator {
val deprecatedAnnotation: String =
"""@scala.deprecated(message="Marked as deprecated in proto file", "")"""

val ignoreDeprecatedAnnotation: String =
"""@scala.annotation.nowarn("cat=deprecation")"""

private[scalapb] def escapeScalaString(raw: String): String =
raw
.map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ package com.google.protobuf.descriptor
* See the documentation for the "Options" section above.
*/
@SerialVersionUID(0L)
@scala.annotation.nowarn("cat=deprecation")
final case class FileOptions(
javaPackage: _root_.scala.Option[_root_.scala.Predef.String] = _root_.scala.None,
javaOuterClassname: _root_.scala.Option[_root_.scala.Predef.String] = _root_.scala.None,
Expand Down Expand Up @@ -655,8 +656,8 @@ object FileOptions extends scalapb.GeneratedMessageCompanion[com.google.protobuf
def optionalJavaOuterClassname: _root_.scalapb.lenses.Lens[UpperPB, _root_.scala.Option[_root_.scala.Predef.String]] = field(_.javaOuterClassname)((c_, f_) => c_.copy(javaOuterClassname = f_))
def javaMultipleFiles: _root_.scalapb.lenses.Lens[UpperPB, _root_.scala.Boolean] = field(_.getJavaMultipleFiles)((c_, f_) => c_.copy(javaMultipleFiles = _root_.scala.Option(f_)))
def optionalJavaMultipleFiles: _root_.scalapb.lenses.Lens[UpperPB, _root_.scala.Option[_root_.scala.Boolean]] = field(_.javaMultipleFiles)((c_, f_) => c_.copy(javaMultipleFiles = f_))
def javaGenerateEqualsAndHash: _root_.scalapb.lenses.Lens[UpperPB, _root_.scala.Boolean] = field(_.getJavaGenerateEqualsAndHash)((c_, f_) => c_.copy(javaGenerateEqualsAndHash = _root_.scala.Option(f_)))
def optionalJavaGenerateEqualsAndHash: _root_.scalapb.lenses.Lens[UpperPB, _root_.scala.Option[_root_.scala.Boolean]] = field(_.javaGenerateEqualsAndHash)((c_, f_) => c_.copy(javaGenerateEqualsAndHash = f_))
@scala.deprecated(message="Marked as deprecated in proto file", "") def javaGenerateEqualsAndHash: _root_.scalapb.lenses.Lens[UpperPB, _root_.scala.Boolean] = field(_.getJavaGenerateEqualsAndHash)((c_, f_) => c_.copy(javaGenerateEqualsAndHash = _root_.scala.Option(f_)))
@scala.deprecated(message="Marked as deprecated in proto file", "") def optionalJavaGenerateEqualsAndHash: _root_.scalapb.lenses.Lens[UpperPB, _root_.scala.Option[_root_.scala.Boolean]] = field(_.javaGenerateEqualsAndHash)((c_, f_) => c_.copy(javaGenerateEqualsAndHash = f_))
def javaStringCheckUtf8: _root_.scalapb.lenses.Lens[UpperPB, _root_.scala.Boolean] = field(_.getJavaStringCheckUtf8)((c_, f_) => c_.copy(javaStringCheckUtf8 = _root_.scala.Option(f_)))
def optionalJavaStringCheckUtf8: _root_.scalapb.lenses.Lens[UpperPB, _root_.scala.Option[_root_.scala.Boolean]] = field(_.javaStringCheckUtf8)((c_, f_) => c_.copy(javaStringCheckUtf8 = f_))
def optimizeFor: _root_.scalapb.lenses.Lens[UpperPB, com.google.protobuf.descriptor.FileOptions.OptimizeMode] = field(_.getOptimizeFor)((c_, f_) => c_.copy(optimizeFor = _root_.scala.Option(f_)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ import _root_.scalapb.internal.compat.JavaConverters._
* See the documentation for the "Options" section above.
*/
@SerialVersionUID(0L)
@scala.annotation.nowarn("cat=deprecation")
final case class FileOptions(
javaPackage: _root_.scala.Option[_root_.scala.Predef.String] = _root_.scala.None,
javaOuterClassname: _root_.scala.Option[_root_.scala.Predef.String] = _root_.scala.None,
Expand Down Expand Up @@ -425,6 +426,7 @@ final case class FileOptions(

object FileOptions extends scalapb.GeneratedMessageCompanion[com.google.protobuf.descriptor.FileOptions] with scalapb.JavaProtoSupport[com.google.protobuf.descriptor.FileOptions, com.google.protobuf.DescriptorProtos.FileOptions] {
implicit def messageCompanion: scalapb.GeneratedMessageCompanion[com.google.protobuf.descriptor.FileOptions] with scalapb.JavaProtoSupport[com.google.protobuf.descriptor.FileOptions, com.google.protobuf.DescriptorProtos.FileOptions] = this
@scala.annotation.nowarn("cat=deprecation")
def toJavaProto(scalaPbSource: com.google.protobuf.descriptor.FileOptions): com.google.protobuf.DescriptorProtos.FileOptions = {
val javaPbOut = com.google.protobuf.DescriptorProtos.FileOptions.newBuilder
scalaPbSource.javaPackage.foreach(javaPbOut.setJavaPackage)
Expand All @@ -450,6 +452,7 @@ object FileOptions extends scalapb.GeneratedMessageCompanion[com.google.protobuf
javaPbOut.addAllUninterpretedOption(_root_.scalapb.internal.compat.toIterable(scalaPbSource.uninterpretedOption.iterator.map(com.google.protobuf.descriptor.UninterpretedOption.toJavaProto(_))).asJava)
javaPbOut.build
}
@scala.annotation.nowarn("cat=deprecation")
def fromJavaProto(javaPbSource: com.google.protobuf.DescriptorProtos.FileOptions): com.google.protobuf.descriptor.FileOptions = com.google.protobuf.descriptor.FileOptions(
javaPackage = if (javaPbSource.hasJavaPackage) Some(javaPbSource.getJavaPackage) else _root_.scala.None,
javaOuterClassname = if (javaPbSource.hasJavaOuterClassname) Some(javaPbSource.getJavaOuterClassname) else _root_.scala.None,
Expand Down Expand Up @@ -709,8 +712,8 @@ object FileOptions extends scalapb.GeneratedMessageCompanion[com.google.protobuf
def optionalJavaOuterClassname: _root_.scalapb.lenses.Lens[UpperPB, _root_.scala.Option[_root_.scala.Predef.String]] = field(_.javaOuterClassname)((c_, f_) => c_.copy(javaOuterClassname = f_))
def javaMultipleFiles: _root_.scalapb.lenses.Lens[UpperPB, _root_.scala.Boolean] = field(_.getJavaMultipleFiles)((c_, f_) => c_.copy(javaMultipleFiles = _root_.scala.Option(f_)))
def optionalJavaMultipleFiles: _root_.scalapb.lenses.Lens[UpperPB, _root_.scala.Option[_root_.scala.Boolean]] = field(_.javaMultipleFiles)((c_, f_) => c_.copy(javaMultipleFiles = f_))
def javaGenerateEqualsAndHash: _root_.scalapb.lenses.Lens[UpperPB, _root_.scala.Boolean] = field(_.getJavaGenerateEqualsAndHash)((c_, f_) => c_.copy(javaGenerateEqualsAndHash = _root_.scala.Option(f_)))
def optionalJavaGenerateEqualsAndHash: _root_.scalapb.lenses.Lens[UpperPB, _root_.scala.Option[_root_.scala.Boolean]] = field(_.javaGenerateEqualsAndHash)((c_, f_) => c_.copy(javaGenerateEqualsAndHash = f_))
@scala.deprecated(message="Marked as deprecated in proto file", "") def javaGenerateEqualsAndHash: _root_.scalapb.lenses.Lens[UpperPB, _root_.scala.Boolean] = field(_.getJavaGenerateEqualsAndHash)((c_, f_) => c_.copy(javaGenerateEqualsAndHash = _root_.scala.Option(f_)))
@scala.deprecated(message="Marked as deprecated in proto file", "") def optionalJavaGenerateEqualsAndHash: _root_.scalapb.lenses.Lens[UpperPB, _root_.scala.Option[_root_.scala.Boolean]] = field(_.javaGenerateEqualsAndHash)((c_, f_) => c_.copy(javaGenerateEqualsAndHash = f_))
def javaStringCheckUtf8: _root_.scalapb.lenses.Lens[UpperPB, _root_.scala.Boolean] = field(_.getJavaStringCheckUtf8)((c_, f_) => c_.copy(javaStringCheckUtf8 = _root_.scala.Option(f_)))
def optionalJavaStringCheckUtf8: _root_.scalapb.lenses.Lens[UpperPB, _root_.scala.Option[_root_.scala.Boolean]] = field(_.javaStringCheckUtf8)((c_, f_) => c_.copy(javaStringCheckUtf8 = f_))
def optimizeFor: _root_.scalapb.lenses.Lens[UpperPB, com.google.protobuf.descriptor.FileOptions.OptimizeMode] = field(_.getOptimizeFor)((c_, f_) => c_.copy(optimizeFor = _root_.scala.Option(f_)))
Expand Down