Skip to content

Commit f113c57

Browse files
committed
Backtick enum identifiers.
Refactored with this command ``` find . -type f -name "*.scala" -exec sed -i'' 's/\benum\b/`enum`/g' {} + ```
1 parent 03a7c5b commit f113c57

File tree

24 files changed

+53
-53
lines changed

24 files changed

+53
-53
lines changed

compiler-plugin/src/main/scala/com/trueaccord/scalapb/compiler/DescriptorPimps.scala

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -372,10 +372,10 @@ trait DescriptorPimps {
372372
}
373373
}
374374

375-
implicit class EnumDescriptorPimp(val enum: EnumDescriptor) {
376-
def parentMessage: Option[Descriptor] = Option(enum.getContainingType)
375+
implicit class EnumDescriptorPimp(val `enum`: EnumDescriptor) {
376+
def parentMessage: Option[Descriptor] = Option(`enum`.getContainingType)
377377

378-
def name: String = enum.getName match {
378+
def name: String = `enum`.getName match {
379379
case "Option" => "OptionEnum"
380380
case n => n
381381
}
@@ -384,25 +384,25 @@ trait DescriptorPimps {
384384

385385
lazy val scalaTypeName: String = parentMessage match {
386386
case Some(p) => p.scalaTypeName + "." + nameSymbol
387-
case None => (enum.getFile.scalaPackagePartsAsSymbols :+ nameSymbol).mkString(".")
387+
case None => (`enum`.getFile.scalaPackagePartsAsSymbols :+ nameSymbol).mkString(".")
388388
}
389389

390-
def isTopLevel = enum.getContainingType == null
390+
def isTopLevel = `enum`.getContainingType == null
391391

392-
def javaTypeName = enum.getFile.fullJavaName(enum.getFullName)
392+
def javaTypeName = `enum`.getFile.fullJavaName(`enum`.getFullName)
393393

394-
def javaConversions = enum.getFile.javaConversions
394+
def javaConversions = `enum`.getFile.javaConversions
395395

396-
def valuesWithNoDuplicates = enum.getValues.asScala.groupBy(_.getNumber)
396+
def valuesWithNoDuplicates = `enum`.getValues.asScala.groupBy(_.getNumber)
397397
.mapValues(_.head).values.toVector.sortBy(_.getNumber)
398398

399-
def javaDescriptorSource: String = if (enum.isTopLevel)
400-
s"${enum.getFile.fileDescriptorObjectName}.javaDescriptor.getEnumTypes.get(${enum.getIndex})"
401-
else s"${enum.getContainingType.scalaTypeName}.javaDescriptor.getEnumTypes.get(${enum.getIndex})"
399+
def javaDescriptorSource: String = if (`enum`.isTopLevel)
400+
s"${`enum`.getFile.fileDescriptorObjectName}.javaDescriptor.getEnumTypes.get(${`enum`.getIndex})"
401+
else s"${`enum`.getContainingType.scalaTypeName}.javaDescriptor.getEnumTypes.get(${`enum`.getIndex})"
402402

403-
def scalaDescriptorSource: String = if (enum.isTopLevel)
404-
s"${enum.getFile.fileDescriptorObjectName}.scalaDescriptor.enums(${enum.getIndex})"
405-
else s"${enum.getContainingType.scalaTypeName}.scalaDescriptor.enums(${enum.getIndex})"
403+
def scalaDescriptorSource: String = if (`enum`.isTopLevel)
404+
s"${`enum`.getFile.fileDescriptorObjectName}.scalaDescriptor.enums(${`enum`.getIndex})"
405+
else s"${`enum`.getContainingType.scalaTypeName}.scalaDescriptor.enums(${`enum`.getIndex})"
406406
}
407407

408408
implicit class EnumValueDescriptorPimp(val enumValue: EnumValueDescriptor) {

compiler-plugin/src/main/scala/com/trueaccord/scalapb/compiler/ProtoValidation.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ProtoValidation(val params: GeneratorParams) extends DescriptorPimps {
1919
def validateEnum(e: EnumDescriptor): Unit = {
2020
if (e.getValues.asScala.exists(_.getName.toUpperCase == "UNRECOGNIZED")) {
2121
throw new GeneratorException(
22-
s"The enum value 'UNRECOGNIZED' in ${e.getName} is not allowed due to conflict with the catch-all " +
22+
s"The `enum` value 'UNRECOGNIZED' in ${e.getName} is not allowed due to conflict with the catch-all " +
2323
"Unrecognized(v: Int) value.")
2424
}
2525
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,13 +1282,13 @@ class ProtobufGenerator(val params: GeneratorParams) extends DescriptorPimps {
12821282
val serviceFiles = generateServiceFiles(file)
12831283

12841284
val enumFiles = for {
1285-
enum <- file.getEnumTypes.asScala
1285+
`enum` <- file.getEnumTypes.asScala
12861286
} yield {
12871287
val b = CodeGeneratorResponse.File.newBuilder()
1288-
b.setName(file.scalaDirectory + "/" + enum.getName + ".scala")
1288+
b.setName(file.scalaDirectory + "/" + `enum`.getName + ".scala")
12891289
b.setContent(
12901290
scalaFileHeader(file)
1291-
.call(printEnum(_, enum)).result())
1291+
.call(printEnum(_, `enum`)).result())
12921292
b.build
12931293
}
12941294

e2e/src/test/scala/AnySpec.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import com.trueaccord.proto.any._
22
import com.google.protobuf.any.Any
33
import org.scalatest._
44
import org.scalatest.prop._
5-
import com.trueaccord.proto.e2e.enum.EnumTest
6-
import com.trueaccord.proto.e2e.enum.Color
5+
import com.trueaccord.proto.e2e.`enum`.EnumTest
6+
import com.trueaccord.proto.e2e.`enum`.Color
77

88
class AnySpec extends FlatSpec with MustMatchers {
99
val green = EnumTest(color = Some(Color.GREEN))

e2e/src/test/scala/EnumSpec.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import com.trueaccord.proto.e2e.enum._
1+
import com.trueaccord.proto.e2e.`enum`._
22
import com.trueaccord.proto.e2e.enum3._
33
import com.trueaccord.scalapb.GeneratedEnumCompanion
44
import org.scalatest._
@@ -90,20 +90,20 @@ class EnumSpec extends FlatSpec with MustMatchers with OptionValues {
9090
innerEnum = Some(EnumTest.InnerEnum.OtherCase)))
9191
}
9292

93-
"missing enum values in proto3" should "be preserved in parsing" in {
93+
"missing `enum` values in proto3" should "be preserved in parsing" in {
9494
val like = EnumTestLike(color = 18) // same field number as `color` in EnumTest3.
9595
val e3 = EnumTest3.parseFrom(like.toByteArray)
9696
e3.color must be (Color3.Unrecognized(18))
9797
e3.color must not be (Color3.Unrecognized(19))
9898
e3.toByteArray must be (like.toByteArray)
9999
}
100100

101-
"missing enum values in proto3 seq" should "be preserved in parsing" in {
101+
"missing `enum` values in proto3 seq" should "be preserved in parsing" in {
102102
val e3 = EnumTest3(colorVector = Seq(Color3.C3_RED, Color3.Unrecognized(15), Color3.C3_BLUE))
103103
EnumTest3.parseFrom(e3.toByteArray) must be (e3)
104104
}
105105

106-
"missing enum values in proto2" should "be preserved in parsing" in {
106+
"missing `enum` values in proto2" should "be preserved in parsing" in {
107107
val like = EnumTestLike(color = 18) // same field number as `color` in EnumTest3.
108108
val e3 = EnumTest.parseFrom(like.toByteArray)
109109
e3.getColor must be (Color.Unrecognized(18))

proptest/src/test/scala/GraphGen.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,18 @@ object GraphGen {
8484
.retryUntil {
8585
case (names, _) =>
8686
// In protoc there is a check that says:
87-
// When enum name is stripped and label is PascalCased (X), this value label conflicts
87+
// When `enum` name is stripped and label is PascalCased (X), this value label conflicts
8888
// with abc_bar_x. This will make the proto fail to compile for some languages, such as C#.
8989
//
9090
// To eliminate any posssibility of triggering it, we don't allow labels (lower case, underscores removed)
91-
// to start with the enum names (lower case, underscores removed)
91+
// to start with the `enum` names (lower case, underscores removed)
9292
val enumNameCanon = enumName.toLowerCase.replaceAll("_", "")
9393
names.forall(n => !n.toLowerCase.replaceAll("_", "").startsWith(enumNameCanon)) &&
9494
namesAreUniqueAfterCamelCase(names)
9595
}
9696
values <- GenUtils.genListOfDistinctPositiveNumbers(names.size).map {
9797
v =>
98-
// in proto3 the first enum value must be zero.
98+
// in proto3 the first `enum` value must be zero.
9999
if (zeroDefined) v.updated(0, 0) else v
100100
}
101101
} yield (EnumNode(myId, enumName, names zip values,

proptest/src/test/scala/Nodes.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ object Nodes {
184184
.add(fileReferences(rootNode).collect({
185185
case f if f != baseFileName => s"""import "${f}.proto";"""
186186
}).toSeq: _*)
187-
.print(enums)((enum, p) => p.print(enum))
187+
.print(enums)((`enum`, p) => p.print(`enum`))
188188
.print(messages)((message, p) => p.print(rootNode, this, message))
189189
.print(services)((service, p) => p.print(service))
190190

@@ -253,7 +253,7 @@ object Nodes {
253253
printer
254254
.add(s"message $name { // message $id")
255255
.indent
256-
.print(enums)((enum, p) => p.print(enum))
256+
.print(enums)((`enum`, p) => p.print(`enum`))
257257
.print(messages)((message, p) => p.print(rootNode, fileNode, message))
258258
.print(makeList(fields)) {
259259
case (printer, OneofOpener(name)) =>
@@ -296,7 +296,7 @@ object Nodes {
296296

297297
case class EnumNode(id: Int, name: String, values: Seq[(String, Int)], parentMessageId: Option[Int], fileId: Int) {
298298
def print(printer: compiler.FunctionalPrinter): compiler.FunctionalPrinter = {
299-
printer.add(s"enum $name { // enum $id")
299+
printer.add(s"`enum` $name { // `enum` $id")
300300
.indent
301301
.add(values.map { case (s, v) => s"$s = $v;"}: _*)
302302
.outdent

proptest/src/test/scala/SchemaGenerators.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ object SchemaGenerators {
2525
"boolean", "do", "if", "private", "this",
2626
"break", "double", "implements", "protected", "throw",
2727
"byte", "else", "import", "public", "throws",
28-
"case", "enum", "instanceof", "return", "transient",
28+
"case", "`enum`", "instanceof", "return", "transient",
2929
"catch", "extends", "int", "short", "try", "char", "final",
3030
"interface", "static", "void", "class", "finally",
3131
"long", "strictfp", "volatile", "const", "float",

scalapb-runtime/js/src/main/scala/com/google/protobuf/descriptor/EnumDescriptorProto.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package com.google.protobuf.descriptor
77

88

99

10-
/** Describes an enum type.
10+
/** Describes an `enum` type.
1111
*/
1212
@SerialVersionUID(0L)
1313
final case class EnumDescriptorProto(

scalapb-runtime/js/src/main/scala/com/google/protobuf/descriptor/EnumOptions.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ package com.google.protobuf.descriptor
1111
* Set this option to true to allow mapping different tag names to the same
1212
* value.
1313
* @param deprecated
14-
* Is this enum deprecated?
14+
* Is this `enum` deprecated?
1515
* Depending on the target platform, this can emit Deprecated annotations
16-
* for the enum, or it will be completely ignored; in the very least, this
16+
* for the `enum`, or it will be completely ignored; in the very least, this
1717
* is a formalization for deprecating enums.
1818
* @param uninterpretedOption
1919
* The parser stores options it doesn't recognize here. See above.

scalapb-runtime/js/src/main/scala/com/google/protobuf/descriptor/EnumValueDescriptorProto.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package com.google.protobuf.descriptor
77

88

99

10-
/** Describes a value within an enum.
10+
/** Describes a value within an `enum`.
1111
*/
1212
@SerialVersionUID(0L)
1313
final case class EnumValueDescriptorProto(

scalapb-runtime/js/src/main/scala/com/google/protobuf/descriptor/EnumValueOptions.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ package com.google.protobuf.descriptor
88

99

1010
/** @param deprecated
11-
* Is this enum value deprecated?
11+
* Is this `enum` value deprecated?
1212
* Depending on the target platform, this can emit Deprecated annotations
13-
* for the enum value, or it will be completely ignored; in the very least,
14-
* this is a formalization for deprecating enum values.
13+
* for the `enum` value, or it will be completely ignored; in the very least,
14+
* this is a formalization for deprecating `enum` values.
1515
* @param uninterpretedOption
1616
* The parser stores options it doesn't recognize here. See above.
1717
*/

scalapb-runtime/js/src/main/scala/com/google/protobuf/descriptor/FieldDescriptorProto.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ package com.google.protobuf.descriptor
1313
* If type_name is set, this need not be set. If both this and type_name
1414
* are set, this must be one of TYPE_ENUM, TYPE_MESSAGE or TYPE_GROUP.
1515
* @param typeName
16-
* For message and enum types, this is the name of the type. If the name
16+
* For message and `enum` types, this is the name of the type. If the name
1717
* starts with a '.', it is fully-qualified. Otherwise, C++-like scoping
1818
* rules are used to find the type (i.e. first the nested types within this
1919
* message are searched, then within the parent, on up to the root

scalapb-runtime/js/src/main/scala/com/google/protobuf/descriptor/FieldOptions.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ package com.google.protobuf.descriptor
2626
* happen when a large value is converted to a floating point JavaScript
2727
* numbers. Specifying JS_NUMBER for the jstype causes the generated
2828
* JavaScript code to use the JavaScript "number" type instead of strings.
29-
* This option is an enum to permit additional types to be added,
29+
* This option is an `enum` to permit additional types to be added,
3030
* e.g. goog.math.Integer.
3131
* @param lazy
3232
* Should this field be parsed lazily? Lazy applies only to message-type

scalapb-runtime/js/src/main/scala/com/google/protobuf/descriptor/FileOptions.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ package com.google.protobuf.descriptor
2020
* explicitly choose the class name).
2121
* @param javaMultipleFiles
2222
* If set true, then the Java code generator will generate a separate .java
23-
* file for each top-level message, enum, and service defined in the .proto
23+
* file for each top-level message, `enum`, and service defined in the .proto
2424
* file. Thus, these types will *not* be nested inside the outer class
2525
* named by java_outer_classname. However, the outer class will still be
2626
* generated to contain the file's getDescriptor() method as well as any

scalapb-runtime/jvm/src/main/scala/com/google/protobuf/descriptor/EnumDescriptorProto.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package com.google.protobuf.descriptor
77

88
import scala.collection.JavaConverters._
99

10-
/** Describes an enum type.
10+
/** Describes an `enum` type.
1111
*/
1212
@SerialVersionUID(0L)
1313
final case class EnumDescriptorProto(

scalapb-runtime/jvm/src/main/scala/com/google/protobuf/descriptor/EnumOptions.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import scala.collection.JavaConverters._
1111
* Set this option to true to allow mapping different tag names to the same
1212
* value.
1313
* @param deprecated
14-
* Is this enum deprecated?
14+
* Is this `enum` deprecated?
1515
* Depending on the target platform, this can emit Deprecated annotations
16-
* for the enum, or it will be completely ignored; in the very least, this
16+
* for the `enum`, or it will be completely ignored; in the very least, this
1717
* is a formalization for deprecating enums.
1818
* @param uninterpretedOption
1919
* The parser stores options it doesn't recognize here. See above.

scalapb-runtime/jvm/src/main/scala/com/google/protobuf/descriptor/EnumValueDescriptorProto.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package com.google.protobuf.descriptor
77

88
import scala.collection.JavaConverters._
99

10-
/** Describes a value within an enum.
10+
/** Describes a value within an `enum`.
1111
*/
1212
@SerialVersionUID(0L)
1313
final case class EnumValueDescriptorProto(

scalapb-runtime/jvm/src/main/scala/com/google/protobuf/descriptor/EnumValueOptions.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ package com.google.protobuf.descriptor
88
import scala.collection.JavaConverters._
99

1010
/** @param deprecated
11-
* Is this enum value deprecated?
11+
* Is this `enum` value deprecated?
1212
* Depending on the target platform, this can emit Deprecated annotations
13-
* for the enum value, or it will be completely ignored; in the very least,
14-
* this is a formalization for deprecating enum values.
13+
* for the `enum` value, or it will be completely ignored; in the very least,
14+
* this is a formalization for deprecating `enum` values.
1515
* @param uninterpretedOption
1616
* The parser stores options it doesn't recognize here. See above.
1717
*/

scalapb-runtime/jvm/src/main/scala/com/google/protobuf/descriptor/FieldDescriptorProto.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import scala.collection.JavaConverters._
1313
* If type_name is set, this need not be set. If both this and type_name
1414
* are set, this must be one of TYPE_ENUM, TYPE_MESSAGE or TYPE_GROUP.
1515
* @param typeName
16-
* For message and enum types, this is the name of the type. If the name
16+
* For message and `enum` types, this is the name of the type. If the name
1717
* starts with a '.', it is fully-qualified. Otherwise, C++-like scoping
1818
* rules are used to find the type (i.e. first the nested types within this
1919
* message are searched, then within the parent, on up to the root

scalapb-runtime/jvm/src/main/scala/com/google/protobuf/descriptor/FieldOptions.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import scala.collection.JavaConverters._
2626
* happen when a large value is converted to a floating point JavaScript
2727
* numbers. Specifying JS_NUMBER for the jstype causes the generated
2828
* JavaScript code to use the JavaScript "number" type instead of strings.
29-
* This option is an enum to permit additional types to be added,
29+
* This option is an `enum` to permit additional types to be added,
3030
* e.g. goog.math.Integer.
3131
* @param lazy
3232
* Should this field be parsed lazily? Lazy applies only to message-type

scalapb-runtime/jvm/src/main/scala/com/google/protobuf/descriptor/FileOptions.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import scala.collection.JavaConverters._
2020
* explicitly choose the class name).
2121
* @param javaMultipleFiles
2222
* If set true, then the Java code generator will generate a separate .java
23-
* file for each top-level message, enum, and service defined in the .proto
23+
* file for each top-level message, `enum`, and service defined in the .proto
2424
* file. Thus, these types will *not* be nested inside the outer class
2525
* named by java_outer_classname. However, the outer class will still be
2626
* generated to contain the file's getDescriptor() method as well as any

scalapb-runtime/shared/src/main/scala/scalapb/descriptors/Descriptor.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ object FieldDescriptor {
189189
case Some(e: EnumDescriptor) =>
190190
ScalaType.Enum(e)
191191
case None =>
192-
throw new DescriptorValidationException(m, s"Could not find enum ${field.getTypeName} for field ${field.getName}")
192+
throw new DescriptorValidationException(m, s"Could not find `enum` ${field.getTypeName} for field ${field.getName}")
193193
case Some(_) =>
194194
throw new DescriptorValidationException(m, s"Invalid type ${field.getTypeName} for field ${field.getName}")
195195
}

scalapb-runtime/shared/src/test/scala/scalapb/descriptors/FileDescriptorSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class FileDescriptorSpec extends FlatSpec with MustMatchers with OptionValues {
188188
}.getMessage must be ("mypkg.Msg1: Invalid type TheEnum for field ff")
189189
}
190190

191-
"buildFrom" should "fail when enum ref type is not an enum" in {
191+
"buildFrom" should "fail when `enum` ref type is not an `enum`" in {
192192
val fdp = FileDescriptorProto.fromAscii(
193193
"""package: "mypkg"
194194
|message_type {

0 commit comments

Comments
 (0)