Skip to content

Commit 6f09c19

Browse files
authored
Generate code using fully qualified type names (#1971)
* Generate code using fully qualified type names * Format generated sources
1 parent 37b8cc6 commit 6f09c19

File tree

167 files changed

+5133
-5159
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

167 files changed

+5133
-5159
lines changed

compiler-plugin/src/main/scala/scalapb/compiler/DescriptorImplicits.scala

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import scalapb.options.Scalapb.ScalaPbOptions.EnumValueNaming
1414
import scalapb.options.Scalapb._
1515

1616
import scala.jdk.CollectionConverters._
17-
import scala.collection.immutable.IndexedSeq
1817
import protocgen.CodeGenRequest
1918

2019
class DescriptorImplicits private[compiler] (
@@ -37,23 +36,12 @@ class DescriptorImplicits private[compiler] (
3736

3837
def nameRelative(levels: Int) = xs.takeRight(levels + 1).map(_.asSymbol).mkString(".")
3938

40-
def fullName = xs.map(_.asSymbol).mkString(".")
39+
private def fullFullName = xs.map(_.asSymbol).mkString(".")
4140

42-
def fullNameWithMaybeRoot: String = {
41+
def fullName: String = {
4342
if (!emptyPackage)
44-
s"_root_.${fullName}"
45-
else fullName
46-
}
47-
48-
def fullNameWithMaybeRoot(context: Descriptor): String = {
49-
fullNameWithMaybeRoot(context.fields.map(_.scalaName))
50-
}
51-
52-
def fullNameWithMaybeRoot(contextNames: Seq[String]): String = {
53-
val topLevelPackage = xs.head
54-
if (contextNames.contains(topLevelPackage) && !emptyPackage)
55-
s"_root_.${fullName}"
56-
else fullName
43+
s"_root_.${fullFullName}"
44+
else fullFullName
5745
}
5846

5947
def /(name: String) = ScalaName(emptyPackage, xs :+ name)
@@ -87,7 +75,7 @@ class DescriptorImplicits private[compiler] (
8775
DescriptorImplicits.primitiveWrapperType(descriptor)
8876
else None
8977

90-
def baseScalaType = descriptor.scalaType.fullNameWithMaybeRoot(Seq("build"))
78+
def baseScalaType = descriptor.scalaType.fullName
9179

9280
def scalaType: String = customScalaType.getOrElse(baseScalaType)
9381
}
@@ -386,14 +374,8 @@ class DescriptorImplicits private[compiler] (
386374
case FieldDescriptor.JavaType.BOOLEAN => "_root_.scala.Boolean"
387375
case FieldDescriptor.JavaType.BYTE_STRING => "_root_.com.google.protobuf.ByteString"
388376
case FieldDescriptor.JavaType.STRING => "_root_.scala.Predef.String"
389-
case FieldDescriptor.JavaType.MESSAGE =>
390-
val contextNames = fd.getContainingType.fields.map(_.scalaName) ++
391-
fd.getContainingType.getRealOneofs.asScala.map(_.scalaName.nameSymbol)
392-
fd.getMessageType.scalaType.fullNameWithMaybeRoot(contextNames)
393-
case FieldDescriptor.JavaType.ENUM =>
394-
val contextNames = fd.getContainingType.fields.map(_.scalaName) ++
395-
fd.getContainingType.getRealOneofs.asScala.map(_.scalaName.nameSymbol)
396-
fd.getEnumType.scalaType.fullNameWithMaybeRoot(contextNames)
377+
case FieldDescriptor.JavaType.MESSAGE => fd.getMessageType.scalaType.fullName
378+
case FieldDescriptor.JavaType.ENUM => fd.getEnumType.scalaType.fullName
397379
}
398380

399381
def singleScalaTypeName = customSingleScalaTypeName.getOrElse(baseSingleScalaTypeName)
@@ -583,7 +565,7 @@ class DescriptorImplicits private[compiler] (
583565
parent.fold(message.getFile().scalaPackage)(_.scalaType) / name
584566
}
585567

586-
def sealedOneofScalaType = {
568+
def sealedOneofScalaType: String = {
587569
sealedOneofStyle match {
588570
case SealedOneofStyle.Optional =>
589571
s"_root_.scala.Option[${sealedOneofTraitScalaType.fullName}]"

compiler-plugin/src/main/scala/scalapb/compiler/ParseFromGenerator.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ private[compiler] class ParseFromGenerator(
113113
val requiredFieldMap: Map[FieldDescriptor, Int] =
114114
message.fields.filter(fd => fd.isRequired || fd.noBoxRequired).zipWithIndex.toMap
115115

116-
val myFullScalaName = message.scalaType.fullNameWithMaybeRoot(message)
116+
val myFullScalaName = message.scalaType.fullName
117117

118118
def generateParseFrom(printer: FunctionalPrinter): FunctionalPrinter = {
119119
printer
@@ -167,7 +167,7 @@ private[compiler] class ParseFromGenerator(
167167
}
168168
read
169169
} else if (field.isEnum)
170-
s"${field.getEnumType.scalaType.fullNameWithMaybeRoot(message)}.fromValue(_input__.readEnum())"
170+
s"${field.getEnumType.scalaType.fullName}.fromValue(_input__.readEnum())"
171171
else if (field.getType == Type.STRING) s"_input__.readStringRequireUtf8()"
172172
else s"_input__.read${Types.capitalizedType(field.getType)}()"
173173

compiler-plugin/src/main/scala/scalapb/compiler/ProtobufGenerator.scala

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,9 @@ class ProtobufGenerator(
188188
.mkString("_root_.com.google.protobuf.ByteString.copyFrom(Array[Byte](", ", ", "))")
189189
case FieldDescriptor.JavaType.STRING => escapeScalaString(defaultValue.asInstanceOf[String])
190190
case FieldDescriptor.JavaType.MESSAGE =>
191-
val contextNames = field.getContainingType.fields.map(_.scalaName) ++
192-
field.getContainingType.getRealOneofs.asScala.map(_.scalaName.nameSymbol)
193-
field.getMessageType.scalaType
194-
.fullNameWithMaybeRoot(contextNames) + ".defaultInstance"
191+
field.getMessageType.scalaType.fullName + ".defaultInstance"
195192
case FieldDescriptor.JavaType.ENUM =>
196-
val contextNames = field.getContainingType.fields.map(_.scalaName) ++
197-
field.getContainingType.getRealOneofs.asScala.map(_.scalaName.nameSymbol)
198-
field.getEnumType.scalaType
199-
.fullNameWithMaybeRoot(contextNames) + "." + defaultValue
193+
field.getEnumType.scalaType.fullName + "." + defaultValue
200194
.asInstanceOf[EnumValueDescriptor]
201195
.scalaName
202196
.asSymbol
@@ -221,21 +215,17 @@ class ProtobufGenerator(
221215
case FieldDescriptor.JavaType.BYTE_STRING => Identity
222216
case FieldDescriptor.JavaType.STRING => Identity
223217
case FieldDescriptor.JavaType.MESSAGE =>
224-
val contextNames = field.getContainingType.fields.map(_.scalaName) ++
225-
field.getContainingType.getRealOneofs.asScala.map(_.scalaName.nameSymbol)
226218
FunctionApplication(
227-
field.getMessageType.scalaType.fullNameWithMaybeRoot(contextNames) + ".fromJavaProto"
219+
field.getMessageType.scalaType.fullName + ".fromJavaProto"
228220
)
229221
case FieldDescriptor.JavaType.ENUM =>
230-
val contextNames = field.getContainingType.fields.map(_.scalaName) ++
231-
field.getContainingType.getRealOneofs.asScala.map(_.scalaName.nameSymbol)
232222
if (!field.legacyEnumFieldTreatedAsClosed())
233223
MethodApplication("intValue") andThen FunctionApplication(
234-
field.getEnumType.scalaType.fullNameWithMaybeRoot(contextNames) + ".fromValue"
224+
field.getEnumType.scalaType.fullName + ".fromValue"
235225
)
236226
else
237227
FunctionApplication(
238-
field.getEnumType.scalaType.fullNameWithMaybeRoot(contextNames) + ".fromJavaValue"
228+
field.getEnumType.scalaType.fullName + ".fromJavaValue"
239229
)
240230
}
241231
baseValueConversion andThen toCustomTypeExpr(field)
@@ -302,19 +292,15 @@ class ProtobufGenerator(
302292
case FieldDescriptor.JavaType.BYTE_STRING => Identity
303293
case FieldDescriptor.JavaType.STRING => Identity
304294
case FieldDescriptor.JavaType.MESSAGE =>
305-
val contextNames = field.getContainingType.fields.map(_.scalaName) ++
306-
field.getContainingType.getRealOneofs.asScala.map(_.scalaName.nameSymbol)
307295
FunctionApplication(
308-
field.getMessageType.scalaType.fullNameWithMaybeRoot(contextNames) + ".toJavaProto"
296+
field.getMessageType.scalaType.fullName + ".toJavaProto"
309297
)
310298
case FieldDescriptor.JavaType.ENUM =>
311-
val contextNames = field.getContainingType.fields.map(_.scalaName) ++
312-
field.getContainingType.getRealOneofs.asScala.map(_.scalaName.nameSymbol)
313299
if (!field.legacyEnumFieldTreatedAsClosed())
314300
(MethodApplication("value") andThen maybeBox("_root_.scala.Int.box"))
315301
else
316302
FunctionApplication(
317-
field.getEnumType.scalaType.fullNameWithMaybeRoot(contextNames) + ".toJavaValue"
303+
field.getEnumType.scalaType.fullName + ".toJavaValue"
318304
)
319305
}
320306
}
@@ -772,11 +758,11 @@ class ProtobufGenerator(
772758
val oneOfFields = message.getRealOneofs.asScala.map { oneOf =>
773759
val ctorDefaultValue: Option[String] =
774760
if (message.getFile.noDefaultValuesInConstructor) None
775-
else Some(oneOf.empty.fullNameWithMaybeRoot(message))
761+
else Some(oneOf.empty.fullName)
776762

777763
ConstructorField(
778764
name = oneOf.scalaName.nameSymbol,
779-
typeName = oneOf.scalaType.fullNameWithMaybeRoot(message),
765+
typeName = oneOf.scalaType.fullName,
780766
default = ctorDefaultValue,
781767
index = oneOf.getField(0).getIndex
782768
)
@@ -852,7 +838,7 @@ class ProtobufGenerator(
852838
_.addWithDelimiter(",")(fields.map(_.nameAndType))
853839
)
854840
.add(
855-
s"): ${message.scalaType.fullNameWithMaybeRoot} = ${message.scalaType.fullNameWithMaybeRoot}("
841+
s"): ${message.scalaType.fullName} = ${message.scalaType.fullName}("
856842
)
857843
.indented(
858844
_.addWithDelimiter(",")(fields.map(_.name))
@@ -1014,16 +1000,15 @@ class ProtobufGenerator(
10141000
val oneofName = field.getContainingOneof.scalaName.nameSymbol
10151001
printer
10161002
.add(
1017-
s"def $fieldName: ${lensType(field.scalaTypeName)} = field(_.${field.getMethod})((c_, f_) => c_.copy($oneofName = ${field.oneOfTypeName
1018-
.fullNameWithMaybeRoot(message)}(f_)))"
1003+
s"def $fieldName: ${lensType(field.scalaTypeName)} = field(_.${field.getMethod})((c_, f_) => c_.copy($oneofName = ${field.oneOfTypeName.fullName}(f_)))"
10191004
)
10201005
}
10211006
}
10221007
.print(message.getRealOneofs.asScala) { case (printer, oneof) =>
10231008
val oneofName = oneof.scalaName.nameSymbol
10241009
printer
10251010
.add(
1026-
s"def $oneofName: ${lensType(oneof.scalaType.fullNameWithMaybeRoot(message))} = field(_.$oneofName)((c_, f_) => c_.copy($oneofName = f_))"
1011+
s"def $oneofName: ${lensType(oneof.scalaType.fullName)} = field(_.$oneofName)((c_, f_) => c_.copy($oneofName = f_))"
10271012
)
10281013
}
10291014
.outdent
@@ -1160,7 +1145,7 @@ class ProtobufGenerator(
11601145
.indent
11611146
.add(message.V.CompSeqType + "(")
11621147
.indent
1163-
.addWithDelimiter(",")(message.nestedTypes.map(m => m.scalaType.fullNameWithMaybeRoot))
1148+
.addWithDelimiter(",")(message.nestedTypes.map(m => m.scalaType.fullName))
11641149
.outdent
11651150
.add(")")
11661151
.outdent
@@ -1397,7 +1382,7 @@ class ProtobufGenerator(
13971382
}
13981383

13991384
def printMessage(printer: FunctionalPrinter, message: Descriptor): FunctionalPrinter = {
1400-
val fullName = message.scalaType.fullNameWithMaybeRoot(message)
1385+
val fullName = message.scalaType.fullName
14011386
val derives =
14021387
if (message.derives.nonEmpty) message.derives.mkString(" derives ", ", ", "") else ""
14031388
printer
@@ -1435,8 +1420,7 @@ class ProtobufGenerator(
14351420
}
14361421
.when(field.isInOneof) { p =>
14371422
p.add(
1438-
s"""def $withMethod(__v: ${singleType}): ${message.scalaType.nameSymbol} = copy(${field.getContainingOneof.scalaName.nameSymbol} = ${field.oneOfTypeName
1439-
.fullNameWithMaybeRoot(message)}(__v))"""
1423+
s"""def $withMethod(__v: ${singleType}): ${message.scalaType.nameSymbol} = copy(${field.getContainingOneof.scalaName.nameSymbol} = ${field.oneOfTypeName.fullName}(__v))"""
14401424
)
14411425
}
14421426
.when(field.isRepeated) { p =>
@@ -1458,11 +1442,8 @@ class ProtobufGenerator(
14581442
}
14591443
.print(message.getRealOneofs.asScala) { case (printer, oneof) =>
14601444
printer.add(
1461-
s"""def clear${oneof.scalaType.name}: ${message.scalaType.nameSymbol} = copy(${oneof.scalaName.nameSymbol} = ${oneof.empty
1462-
.fullNameWithMaybeRoot(message)})
1463-
|def with${oneof.scalaType.name}(__v: ${oneof.scalaType.fullNameWithMaybeRoot(
1464-
message
1465-
)}): ${message.scalaType.nameSymbol} = copy(${oneof.scalaName.nameSymbol} = __v)""".stripMargin
1445+
s"""def clear${oneof.scalaType.name}: ${message.scalaType.nameSymbol} = copy(${oneof.scalaName.nameSymbol} = ${oneof.empty.fullName})
1446+
|def with${oneof.scalaType.name}(__v: ${oneof.scalaType.fullName}): ${message.scalaType.nameSymbol} = copy(${oneof.scalaName.nameSymbol} = __v)""".stripMargin
14661447
)
14671448
}
14681449
.when(message.preservesUnknownFields)(
@@ -1523,7 +1504,7 @@ class ProtobufGenerator(
15231504
|// Do not edit!
15241505
|""".stripMargin)
15251506
.when(file.scalaPackage.fullName.nonEmpty)(
1526-
_.add("package " + file.scalaPackage.fullName).add()
1507+
_.add("package " + file.scalaPackage.fullName.stripPrefix("_root_.")).add()
15271508
)
15281509
.when(javaConverterImport)(
15291510
_.add(s"import _root_.scalapb.internal.compat.JavaConverters.${file.V.WildcardImport}")

compiler-plugin/src/test/scala/scalapb/compiler/DescriptorImplicitsSpec.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,19 @@ class DescriptorImplicitsSpec extends AnyFlatSpec with Matchers with ProtocInvoc
5555
.get
5656
.findMessageTypeByName("A")
5757
.scalaType
58-
.fullName must be("disable_flat.inside_disable_flat.A")
58+
.fullName must be("_root_.disable_flat.inside_disable_flat.A")
5959
files
6060
.find(_.getFullName() == "inside_enable_flat.proto")
6161
.get
6262
.findMessageTypeByName("B")
6363
.scalaType
64-
.fullName must be("enable_flat.B")
64+
.fullName must be("_root_.enable_flat.B")
6565
files
6666
.find(_.getFullName() == "outside.proto")
6767
.get
6868
.findMessageTypeByName("C")
6969
.scalaType
70-
.fullName must be("outside.C")
70+
.fullName must be("_root_.outside.C")
7171
}
7272

7373
"flat package" should "be overridable when not set as generator parameter" in {
@@ -84,19 +84,19 @@ class DescriptorImplicitsSpec extends AnyFlatSpec with Matchers with ProtocInvoc
8484
.get
8585
.findMessageTypeByName("A")
8686
.scalaType
87-
.fullName must be("disable_flat.inside_disable_flat.A")
87+
.fullName must be("_root_.disable_flat.inside_disable_flat.A")
8888
files
8989
.find(_.getFullName() == "inside_enable_flat.proto")
9090
.get
9191
.findMessageTypeByName("B")
9292
.scalaType
93-
.fullName must be("enable_flat.B")
93+
.fullName must be("_root_.enable_flat.B")
9494
files
9595
.find(_.getFullName() == "outside.proto")
9696
.get
9797
.findMessageTypeByName("C")
9898
.scalaType
99-
.fullName must be("outside.outside.C")
99+
.fullName must be("_root_.outside.outside.C")
100100
}
101101

102102
"disableOutput" should "be set for package option files" in {

docs/src/main/scala/generated/com/thesamet/docs/json/JsonProto.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ package com.thesamet.docs.json
55

66
object JsonProto extends _root_.scalapb.GeneratedFileObject {
77
lazy val dependencies: Seq[_root_.scalapb.GeneratedFileObject] = Seq(
8-
com.google.protobuf.any.AnyProto
8+
_root_.com.google.protobuf.any.AnyProto
99
)
1010
lazy val messagesCompanions: Seq[_root_.scalapb.GeneratedMessageCompanion[_ <: _root_.scalapb.GeneratedMessage]] =
1111
Seq[_root_.scalapb.GeneratedMessageCompanion[_ <: _root_.scalapb.GeneratedMessage]](
12-
com.thesamet.docs.json.MyMessage,
13-
com.thesamet.docs.json.MyContainer
12+
_root_.com.thesamet.docs.json.MyMessage,
13+
_root_.com.thesamet.docs.json.MyContainer
1414
)
1515
private lazy val ProtoBytes: _root_.scala.Array[Byte] =
1616
scalapb.Encoding.fromBase64(scala.collection.immutable.Seq(
@@ -25,7 +25,7 @@ object JsonProto extends _root_.scalapb.GeneratedFileObject {
2525
lazy val javaDescriptor: com.google.protobuf.Descriptors.FileDescriptor = {
2626
val javaProto = com.google.protobuf.DescriptorProtos.FileDescriptorProto.parseFrom(ProtoBytes)
2727
com.google.protobuf.Descriptors.FileDescriptor.buildFrom(javaProto, _root_.scala.Array(
28-
com.google.protobuf.any.AnyProto.javaDescriptor
28+
_root_.com.google.protobuf.any.AnyProto.javaDescriptor
2929
))
3030
}
3131
@deprecated("Use javaDescriptor instead. In a future version this will refer to scalaDescriptor.", "ScalaPB 0.5.47")

0 commit comments

Comments
 (0)